[serial] Fix byte size, flow control, parity, stop bits configuration

This commit is contained in:
Niklas Hauser
2025-11-20 14:31:59 +01:00
committed by Jacob Dahl
parent 25138d0a12
commit 17f3db9231
3 changed files with 199 additions and 39 deletions

View File

@@ -180,8 +180,34 @@ bool SerialImpl::configure()
//
uart_config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
/* no parity, one stop bit, disable flow control */
uart_config.c_cflag &= ~(CSTOPB | PARENB | CRTSCTS);
// Control modes
uart_config.c_cflag = 0;
switch (_bytesize) {
case ByteSize::FiveBits: uart_config.c_cflag |= CS5; break;
case ByteSize::SixBits: uart_config.c_cflag |= CS6; break;
case ByteSize::SevenBits: uart_config.c_cflag |= CS7; break;
case ByteSize::EightBits: uart_config.c_cflag |= CS8; break;
}
if (_flowcontrol == FlowControl::Enabled) {
uart_config.c_cflag |= CRTSCTS;
}
if (_parity != Parity::None) {
uart_config.c_cflag |= PARENB;
}
if (_parity == Parity::Odd) {
uart_config.c_cflag |= PARODD;
}
if (_stopbits == StopBits::Two) {
uart_config.c_cflag |= CSTOPB;
}
/* set baud rate */
if ((termios_state = cfsetispeed(&uart_config, speed)) < 0) {
@@ -486,7 +512,19 @@ ByteSize SerialImpl::getBytesize() const
bool SerialImpl::setBytesize(ByteSize bytesize)
{
return bytesize == ByteSize::EightBits;
// check if already configured
if ((bytesize == _bytesize) && _open) {
return true;
}
_bytesize = bytesize;
// process bytesize change now if port is already open
if (_open) {
return configure();
}
return true;
}
Parity SerialImpl::getParity() const
@@ -496,7 +534,19 @@ Parity SerialImpl::getParity() const
bool SerialImpl::setParity(Parity parity)
{
return parity == Parity::None;
// check if already configured
if ((parity == _parity) && _open) {
return true;
}
_parity = parity;
// process parity change now if port is already open
if (_open) {
return configure();
}
return true;
}
StopBits SerialImpl::getStopbits() const
@@ -506,7 +556,19 @@ StopBits SerialImpl::getStopbits() const
bool SerialImpl::setStopbits(StopBits stopbits)
{
return stopbits == StopBits::One;
// check if already configured
if ((stopbits == _stopbits) && _open) {
return true;
}
_stopbits = stopbits;
// process stopbits change now if port is already open
if (_open) {
return configure();
}
return true;
}
FlowControl SerialImpl::getFlowcontrol() const
@@ -516,7 +578,19 @@ FlowControl SerialImpl::getFlowcontrol() const
bool SerialImpl::setFlowcontrol(FlowControl flowcontrol)
{
return flowcontrol == FlowControl::Disabled;
// check if already configured
if ((flowcontrol == _flowcontrol) && _open) {
return true;
}
_flowcontrol = flowcontrol;
// process flowcontrol change now if port is already open
if (_open) {
return configure();
}
return true;
}
bool SerialImpl::getSingleWireMode() const