Commit 1c5d969f46ccd5333f602dfbe2b0a1295650b9b0
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,9 +909,13 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) | ||
| 909 | #if HAVE_DECL_TIOCSRS485 | 909 | #if HAVE_DECL_TIOCSRS485 |
| 910 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 910 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 911 | struct serial_rs485 rs485conf; | 911 | struct serial_rs485 rs485conf; |
| 912 | - memset(&rs485conf, 0x0, sizeof(struct serial_rs485)); | ||
| 913 | 912 | ||
| 914 | if (mode == MODBUS_RTU_RS485) { | 913 | if (mode == MODBUS_RTU_RS485) { |
| 914 | + // Get | ||
| 915 | + if (ioctl(ctx->s, TIOCGRS485, &rs485conf) < 0) { | ||
| 916 | + return -1; | ||
| 917 | + } | ||
| 918 | + // Set | ||
| 915 | rs485conf.flags = SER_RS485_ENABLED; | 919 | rs485conf.flags = SER_RS485_ENABLED; |
| 916 | if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) { | 920 | if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) { |
| 917 | return -1; | 921 | return -1; |
| @@ -923,6 +927,10 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) | @@ -923,6 +927,10 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) | ||
| 923 | /* Turn off RS485 mode only if required */ | 927 | /* Turn off RS485 mode only if required */ |
| 924 | if (ctx_rtu->serial_mode == MODBUS_RTU_RS485) { | 928 | if (ctx_rtu->serial_mode == MODBUS_RTU_RS485) { |
| 925 | /* The ioctl call is avoided because it can fail on some RS232 ports */ | 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 | if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) { | 934 | if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) { |
| 927 | return -1; | 935 | return -1; |
| 928 | } | 936 | } |