Commit 1c5d969f46ccd5333f602dfbe2b0a1295650b9b0

Authored by Stéphane Raimbault
1 parent 44b00830

Only set SER_RS485_ENABLED bit of existing RS485 settings

Thanks to @JCWren
Showing 1 changed file with 9 additions and 1 deletions
src/modbus-rtu.c
... ... @@ -909,9 +909,13 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
909 909 #if HAVE_DECL_TIOCSRS485
910 910 modbus_rtu_t *ctx_rtu = ctx->backend_data;
911 911 struct serial_rs485 rs485conf;
912   - memset(&rs485conf, 0x0, sizeof(struct serial_rs485));
913 912  
914 913 if (mode == MODBUS_RTU_RS485) {
  914 + // Get
  915 + if (ioctl(ctx->s, TIOCGRS485, &rs485conf) < 0) {
  916 + return -1;
  917 + }
  918 + // Set
915 919 rs485conf.flags = SER_RS485_ENABLED;
916 920 if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
917 921 return -1;
... ... @@ -923,6 +927,10 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
923 927 /* Turn off RS485 mode only if required */
924 928 if (ctx_rtu->serial_mode == MODBUS_RTU_RS485) {
925 929 /* The ioctl call is avoided because it can fail on some RS232 ports */
  930 + if (ioctl(ctx->s, TIOCGRS485, &rs485conf) < 0) {
  931 + return -1;
  932 + }
  933 + rs485conf.flags &= ~SER_RS485_ENABLED;
926 934 if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
927 935 return -1;
928 936 }
... ...