فهرست منبع

Check debug flag in RTU code

Stéphane Raimbault 11 سال پیش
والد
کامیت
d928314aba
1فایلهای تغییر یافته به همراه24 افزوده شده و 12 حذف شده
  1. 24 12
      src/modbus-rtu.c

+ 24 - 12
src/modbus-rtu.c

@@ -437,16 +437,20 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 
     /* Error checking */
     if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) {
-        fprintf(stderr, "ERROR Can't open the device %s (LastError %d)\n",
-                ctx_rtu->device, (int)GetLastError());
+        if (ctx->debug) {
+            fprintf(stderr, "ERROR Can't open the device %s (LastError %d)\n",
+                    ctx_rtu->device, (int)GetLastError());
+        }
         return -1;
     }
 
     /* Save params */
     ctx_rtu->old_dcb.DCBlength = sizeof(DCB);
     if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
-        fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",
-                (int)GetLastError());
+        if (ctx->debug) {
+            fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",
+                    (int)GetLastError());
+        }
         CloseHandle(ctx_rtu->w_ser.fd);
         ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
         return -1;
@@ -514,8 +518,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
         break;
     default:
         dcb.BaudRate = CBR_9600;
-        printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",
-               ctx_rtu->baud, ctx_rtu->device);
+        if (ctx->debug) {
+            fprintf(stderr, "WARNING Unknown baud rate %d for %s (B9600 used)\n",
+                    ctx_rtu->baud, ctx_rtu->device);
+        }
     }
 
     /* Data bits */
@@ -569,8 +575,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 
     /* Setup port */
     if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
-        fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
-                (int)GetLastError());
+        if (ctx->debug) {
+            fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
+                    (int)GetLastError());
+        }
         CloseHandle(ctx_rtu->w_ser.fd);
         ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
         return -1;
@@ -590,8 +598,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 
     ctx->s = open(ctx_rtu->device, flags);
     if (ctx->s == -1) {
-        fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
-                ctx_rtu->device, strerror(errno));
+        if (ctx->debug) {
+            fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
+                    ctx_rtu->device, strerror(errno));
+        }
         return -1;
     }
 
@@ -1033,13 +1043,15 @@ static void _modbus_rtu_close(modbus_t *ctx)
 
 #if defined(_WIN32)
     /* Revert settings */
-    if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb))
+    if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb) && ctx->debug) {
         fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n",
                 (int)GetLastError());
+    }
 
-    if (!CloseHandle(ctx_rtu->w_ser.fd))
+    if (!CloseHandle(ctx_rtu->w_ser.fd) && ctx->debug) {
         fprintf(stderr, "ERROR Error while closing handle (LastError %d)\n",
                 (int)GetLastError());
+    }
 #else
     if (ctx->s != -1) {
         tcsetattr(ctx->s, TCSANOW, &(ctx_rtu->old_tios));