Prevent changing port settings on already opened serial port on Linux using advisory locks, default serial ports to flow control off

This commit is contained in:
Adam Honse 2025-01-19 18:34:45 -06:00
parent 116a031c6d
commit a18ef1ad3a
2 changed files with 22 additions and 10 deletions

View file

@ -23,11 +23,11 @@ serial_port::serial_port()
/*-----------------------------------------------------*\
| Set default port configuration but do not open |
\*-----------------------------------------------------*/
baud_rate = 9600;
parity = SERIAL_PORT_PARITY_NONE;
size = SERIAL_PORT_SIZE_8;
stop_bits = SERIAL_PORT_STOP_BITS_1;
flow_control = true;
this->baud_rate = 9600;
this->parity = SERIAL_PORT_PARITY_NONE;
this->size = SERIAL_PORT_SIZE_8;
this->stop_bits = SERIAL_PORT_STOP_BITS_1;
this->flow_control = false;
}
/*---------------------------------------------------------*\
@ -40,11 +40,11 @@ serial_port::serial_port(const char * name, unsigned int baud)
/*-----------------------------------------------------*\
| Set default port configuration and open |
\*-----------------------------------------------------*/
baud_rate = baud;
parity = SERIAL_PORT_PARITY_NONE;
size = SERIAL_PORT_SIZE_8;
stop_bits = SERIAL_PORT_STOP_BITS_1;
flow_control = true;
this->baud_rate = baud;
this->parity = SERIAL_PORT_PARITY_NONE;
this->size = SERIAL_PORT_SIZE_8;
this->stop_bits = SERIAL_PORT_STOP_BITS_1;
this->flow_control = false;
serial_open(name);
}
@ -230,6 +230,16 @@ bool serial_port::serial_open()
return false;
}
/*-----------------------------------------*\
| Set an advisory lock on the port and |
| abort port setup if already locked |
\*-----------------------------------------*/
if(flock(file_descriptor, LOCK_EX | LOCK_NB) < 0)
{
close(file_descriptor);
return false;
}
/*-----------------------------------------*\
| Get the port configuration options |
\*-----------------------------------------*/
@ -496,6 +506,7 @@ void serial_port::serial_close()
| Linux-specific code path for serial close |
\*-----------------------------------------------------*/
#ifdef __linux__
flock(file_descriptor, LOCK_UN | LOCK_NB);
close(file_descriptor);
#endif