Commit d928314abaf528ff39166fd4eb701efeadb85108

Authored by Stéphane Raimbault
1 parent 3b522097

Check debug flag in RTU code

Showing 1 changed file with 24 additions and 12 deletions
src/modbus-rtu.c
@@ -437,16 +437,20 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -437,16 +437,20 @@ static int _modbus_rtu_connect(modbus_t *ctx)
437 437
438 /* Error checking */ 438 /* Error checking */
439 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) { 439 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) {
440 - fprintf(stderr, "ERROR Can't open the device %s (LastError %d)\n",  
441 - ctx_rtu->device, (int)GetLastError()); 440 + if (ctx->debug) {
  441 + fprintf(stderr, "ERROR Can't open the device %s (LastError %d)\n",
  442 + ctx_rtu->device, (int)GetLastError());
  443 + }
442 return -1; 444 return -1;
443 } 445 }
444 446
445 /* Save params */ 447 /* Save params */
446 ctx_rtu->old_dcb.DCBlength = sizeof(DCB); 448 ctx_rtu->old_dcb.DCBlength = sizeof(DCB);
447 if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) { 449 if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
448 - fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",  
449 - (int)GetLastError()); 450 + if (ctx->debug) {
  451 + fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",
  452 + (int)GetLastError());
  453 + }
450 CloseHandle(ctx_rtu->w_ser.fd); 454 CloseHandle(ctx_rtu->w_ser.fd);
451 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; 455 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
452 return -1; 456 return -1;
@@ -514,8 +518,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -514,8 +518,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
514 break; 518 break;
515 default: 519 default:
516 dcb.BaudRate = CBR_9600; 520 dcb.BaudRate = CBR_9600;
517 - printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",  
518 - ctx_rtu->baud, ctx_rtu->device); 521 + if (ctx->debug) {
  522 + fprintf(stderr, "WARNING Unknown baud rate %d for %s (B9600 used)\n",
  523 + ctx_rtu->baud, ctx_rtu->device);
  524 + }
519 } 525 }
520 526
521 /* Data bits */ 527 /* Data bits */
@@ -569,8 +575,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -569,8 +575,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
569 575
570 /* Setup port */ 576 /* Setup port */
571 if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) { 577 if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
572 - fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",  
573 - (int)GetLastError()); 578 + if (ctx->debug) {
  579 + fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
  580 + (int)GetLastError());
  581 + }
574 CloseHandle(ctx_rtu->w_ser.fd); 582 CloseHandle(ctx_rtu->w_ser.fd);
575 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; 583 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
576 return -1; 584 return -1;
@@ -590,8 +598,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -590,8 +598,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
590 598
591 ctx->s = open(ctx_rtu->device, flags); 599 ctx->s = open(ctx_rtu->device, flags);
592 if (ctx->s == -1) { 600 if (ctx->s == -1) {
593 - fprintf(stderr, "ERROR Can't open the device %s (%s)\n",  
594 - ctx_rtu->device, strerror(errno)); 601 + if (ctx->debug) {
  602 + fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
  603 + ctx_rtu->device, strerror(errno));
  604 + }
595 return -1; 605 return -1;
596 } 606 }
597 607
@@ -1033,13 +1043,15 @@ static void _modbus_rtu_close(modbus_t *ctx) @@ -1033,13 +1043,15 @@ static void _modbus_rtu_close(modbus_t *ctx)
1033 1043
1034 #if defined(_WIN32) 1044 #if defined(_WIN32)
1035 /* Revert settings */ 1045 /* Revert settings */
1036 - if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) 1046 + if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb) && ctx->debug) {
1037 fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n", 1047 fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n",
1038 (int)GetLastError()); 1048 (int)GetLastError());
  1049 + }
1039 1050
1040 - if (!CloseHandle(ctx_rtu->w_ser.fd)) 1051 + if (!CloseHandle(ctx_rtu->w_ser.fd) && ctx->debug) {
1041 fprintf(stderr, "ERROR Error while closing handle (LastError %d)\n", 1052 fprintf(stderr, "ERROR Error while closing handle (LastError %d)\n",
1042 (int)GetLastError()); 1053 (int)GetLastError());
  1054 + }
1043 #else 1055 #else
1044 if (ctx->s != -1) { 1056 if (ctx->s != -1) {
1045 tcsetattr(ctx->s, TCSANOW, &(ctx_rtu->old_tios)); 1057 tcsetattr(ctx->s, TCSANOW, &(ctx_rtu->old_tios));