From d928314abaf528ff39166fd4eb701efeadb85108 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Mon, 21 Oct 2013 12:42:04 +0200 Subject: [PATCH] Check debug flag in RTU code --- src/modbus-rtu.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index bae99b2..51f45aa 100644 --- a/src/modbus-rtu.c +++ b/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)); -- libgit2 0.21.4