浏览代码

Avoid ioctl call on non-RS485 ports

Original patch provided by Michael Haberler <git@mah.priv.at>.
Stéphane Raimbault 13 年之前
父节点
当前提交
1fca027f97
共有 1 个文件被更改,包括 6 次插入3 次删除
  1. 6 3
      src/modbus-rtu.c

+ 6 - 3
src/modbus-rtu.c

@@ -829,10 +829,13 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
             ctx_rtu->serial_mode = MODBUS_RTU_RS485;
             return 0;
         } else if (mode == MODBUS_RTU_RS232) {
-            if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
-                return -1;
+            /* Turn off RS485 mode only if required */
+            if (ctx_rtu->serial_mode == MODBUS_RTU_RS485) {
+                /* The ioctl call is avoided because it can fail on some RS232 ports */
+                if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
+                    return -1;
+                }
             }
-
             ctx_rtu->serial_mode = MODBUS_RTU_RS232;
             return 0;
         }