Changed the ports/linux RS485_Set_Baud_Rate() function so that the MS/TP AutoBaud process succeeds. (#1022)

This commit is contained in:
Ryan Mulder
2025-06-17 15:28:25 -04:00
committed by GitHub
parent c8f18cfaca
commit c2c987f11a
+36 -29
View File
@@ -126,7 +126,40 @@ uint32_t RS485_Get_Port_Baud_Rate(struct mstp_port_struct_t *mstp_port)
*****************************************************************************/ *****************************************************************************/
bool RS485_Set_Baud_Rate(uint32_t baud) bool RS485_Set_Baud_Rate(uint32_t baud)
{ {
struct termios2 newtio;
RS485_Baud = baud; RS485_Baud = baud;
if (RS485_Handle >= 0) {
/* clear struct for new port settings */
memset(&newtio, 0, sizeof(newtio));
/*
BOTHER: Set bps rate.
https://man7.org/linux/man-pages/man2/TCSETS.2const.html
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem control
CREAD : enable receiving characters
*/
newtio.c_cflag =
CS8 | CLOCAL | CREAD | RS485MOD | BOTHER | (BOTHER << IBSHIFT);
newtio.c_ispeed = RS485_Baud;
newtio.c_ospeed = RS485_Baud;
/* Raw input */
newtio.c_iflag = 0;
/* Raw output */
newtio.c_oflag = 0;
/* no processing */
newtio.c_lflag = 0;
/* activate the settings for the port after flushing I/O */
termios2_tcsetattr(RS485_Handle, TCSAFLUSH, &newtio);
#if PRINT_ENABLED
fprintf(stdout, "RS485 Baud Rate %u\n", RS485_Baud);
fflush(stdout);
#endif
}
return true; return true;
} }
@@ -265,8 +298,6 @@ void RS485_Cleanup(void)
void RS485_Initialize(void) void RS485_Initialize(void)
{ {
struct termios2 newtio;
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stdout, "RS485 Interface: %s\n", RS485_Port_Name); fprintf(stdout, "RS485 Interface: %s\n", RS485_Port_Name);
#endif #endif
@@ -288,33 +319,9 @@ void RS485_Initialize(void)
#endif #endif
/* save current serial port settings */ /* save current serial port settings */
termios2_tcgetattr(RS485_Handle, &RS485_oldtio2); termios2_tcgetattr(RS485_Handle, &RS485_oldtio2);
/* clear struct for new port settings */
memset(&newtio, 0, sizeof(newtio)); RS485_Set_Baud_Rate(RS485_Baud);
/*
BOTHER: Set bps rate.
https://man7.org/linux/man-pages/man2/TCSETS.2const.html
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem control
CREAD : enable receiving characters
*/
newtio.c_cflag =
CS8 | CLOCAL | CREAD | RS485MOD | BOTHER | (BOTHER << IBSHIFT);
newtio.c_ispeed = RS485_Baud;
newtio.c_ospeed = RS485_Baud;
/* Raw input */
newtio.c_iflag = 0;
/* Raw output */
newtio.c_oflag = 0;
/* no processing */
newtio.c_lflag = 0;
/* activate the settings for the port after flushing I/O */
termios2_tcsetattr(RS485_Handle, TCSAFLUSH, &newtio);
#if PRINT_ENABLED
fprintf(stdout, "RS485 Baud Rate %u\n", RS485_Get_Baud_Rate());
fflush(stdout);
#endif
/* destructor */ /* destructor */
atexit(RS485_Cleanup); atexit(RS485_Cleanup);
/* flush any data waiting */ /* flush any data waiting */