瀏覽代碼

Split POSIX and Windows RTU connect functions

Easier to read
Stéphane Raimbault 2 年之前
父節點
當前提交
cb692215fa
共有 1 個文件被更改,包括 21 次插入7 次删除
  1. 21 7
      src/modbus-rtu.c

+ 21 - 7
src/modbus-rtu.c

@@ -386,14 +386,10 @@ static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg, const int ms
 }
 }
 
 
 /* Sets up a serial port for RTU communications */
 /* Sets up a serial port for RTU communications */
+#if defined(_WIN32)
 static int _modbus_rtu_connect(modbus_t *ctx)
 static int _modbus_rtu_connect(modbus_t *ctx)
 {
 {
-#if defined(_WIN32)
     DCB dcb;
     DCB dcb;
-#else
-    struct termios tios;
-    int flags;
-#endif
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
 
 
     if (ctx->debug) {
     if (ctx->debug) {
@@ -405,7 +401,6 @@ static int _modbus_rtu_connect(modbus_t *ctx)
                ctx_rtu->stop_bit);
                ctx_rtu->stop_bit);
     }
     }
 
 
-#if defined(_WIN32)
     /* Some references here:
     /* Some references here:
      * http://msdn.microsoft.com/en-us/library/aa450602.aspx
      * http://msdn.microsoft.com/en-us/library/aa450602.aspx
      */
      */
@@ -506,7 +501,26 @@ static int _modbus_rtu_connect(modbus_t *ctx)
         ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
         ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
         return -1;
         return -1;
     }
     }
+
+    return 0;
+}
 #else
 #else
+/* POSIX */
+static int _modbus_rtu_connect(modbus_t *ctx)
+{
+    struct termios tios;
+    int flags;
+    modbus_rtu_t *ctx_rtu = ctx->backend_data;
+
+    if (ctx->debug) {
+        printf("Opening %s at %d bauds (%c, %d, %d)\n",
+               ctx_rtu->device,
+               ctx_rtu->baud,
+               ctx_rtu->parity,
+               ctx_rtu->data_bit,
+               ctx_rtu->stop_bit);
+    }
+
     /* The O_NOCTTY flag tells UNIX that this program doesn't want
     /* The O_NOCTTY flag tells UNIX that this program doesn't want
        to be the "controlling terminal" for that port. If you
        to be the "controlling terminal" for that port. If you
        don't specify this then any input (such as keyboard abort
        don't specify this then any input (such as keyboard abort
@@ -720,10 +734,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
         ctx->s = -1;
         ctx->s = -1;
         return -1;
         return -1;
     }
     }
-#endif
 
 
     return 0;
     return 0;
 }
 }
+#endif
 
 
 // FIXME Temporary solution before rewriting Windows RTU backend
 // FIXME Temporary solution before rewriting Windows RTU backend
 static unsigned int _modbus_rtu_is_connected(modbus_t *ctx)
 static unsigned int _modbus_rtu_is_connected(modbus_t *ctx)