mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
[hal] Check error codes during serial port initialization (#3712)
Throw if serial port init fails. Fixes #2036.
This commit is contained in:
@@ -83,7 +83,7 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
|
||||
|
||||
serialPort->portId = open(portName, O_RDWR | O_NOCTTY);
|
||||
if (serialPort->portId < 0) {
|
||||
*status = errno;
|
||||
*status = -errno;
|
||||
if (*status == EACCES) {
|
||||
*status = HAL_CONSOLE_OUT_ENABLED_ERROR;
|
||||
}
|
||||
@@ -94,8 +94,20 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
|
||||
std::memset(&serialPort->tty, 0, sizeof(serialPort->tty));
|
||||
|
||||
serialPort->baudRate = B9600;
|
||||
cfsetospeed(&serialPort->tty, static_cast<speed_t>(serialPort->baudRate));
|
||||
cfsetispeed(&serialPort->tty, static_cast<speed_t>(serialPort->baudRate));
|
||||
if (cfsetospeed(&serialPort->tty,
|
||||
static_cast<speed_t>(serialPort->baudRate)) != 0) {
|
||||
*status = -errno;
|
||||
close(serialPort->portId);
|
||||
serialPortHandles->Free(handle);
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
if (cfsetispeed(&serialPort->tty,
|
||||
static_cast<speed_t>(serialPort->baudRate)) != 0) {
|
||||
*status = -errno;
|
||||
close(serialPort->portId);
|
||||
serialPortHandles->Free(handle);
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
serialPort->tty.c_cflag &= ~PARENB;
|
||||
serialPort->tty.c_cflag &= ~CSTOPB;
|
||||
@@ -115,9 +127,14 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
|
||||
*/
|
||||
serialPort->tty.c_oflag = ~OPOST;
|
||||
|
||||
tcflush(serialPort->portId, TCIOFLUSH);
|
||||
if (tcflush(serialPort->portId, TCIOFLUSH) != 0) {
|
||||
*status = -errno;
|
||||
close(serialPort->portId);
|
||||
serialPortHandles->Free(handle);
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
if (tcsetattr(serialPort->portId, TCSANOW, &serialPort->tty) != 0) {
|
||||
*status = errno;
|
||||
*status = -errno;
|
||||
close(serialPort->portId);
|
||||
serialPortHandles->Free(handle);
|
||||
return HAL_kInvalidHandle;
|
||||
|
||||
Reference in New Issue
Block a user