Commit d928314abaf528ff39166fd4eb701efeadb85108
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 | 437 | |
| 438 | 438 | /* Error checking */ |
| 439 | 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 | 444 | return -1; |
| 443 | 445 | } |
| 444 | 446 | |
| 445 | 447 | /* Save params */ |
| 446 | 448 | ctx_rtu->old_dcb.DCBlength = sizeof(DCB); |
| 447 | 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 | 454 | CloseHandle(ctx_rtu->w_ser.fd); |
| 451 | 455 | ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; |
| 452 | 456 | return -1; |
| ... | ... | @@ -514,8 +518,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) |
| 514 | 518 | break; |
| 515 | 519 | default: |
| 516 | 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 | 527 | /* Data bits */ |
| ... | ... | @@ -569,8 +575,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) |
| 569 | 575 | |
| 570 | 576 | /* Setup port */ |
| 571 | 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 | 582 | CloseHandle(ctx_rtu->w_ser.fd); |
| 575 | 583 | ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; |
| 576 | 584 | return -1; |
| ... | ... | @@ -590,8 +598,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) |
| 590 | 598 | |
| 591 | 599 | ctx->s = open(ctx_rtu->device, flags); |
| 592 | 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 | 605 | return -1; |
| 596 | 606 | } |
| 597 | 607 | |
| ... | ... | @@ -1033,13 +1043,15 @@ static void _modbus_rtu_close(modbus_t *ctx) |
| 1033 | 1043 | |
| 1034 | 1044 | #if defined(_WIN32) |
| 1035 | 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 | 1047 | fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n", |
| 1038 | 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 | 1052 | fprintf(stderr, "ERROR Error while closing handle (LastError %d)\n", |
| 1042 | 1053 | (int)GetLastError()); |
| 1054 | + } | |
| 1043 | 1055 | #else |
| 1044 | 1056 | if (ctx->s != -1) { |
| 1045 | 1057 | tcsetattr(ctx->s, TCSANOW, &(ctx_rtu->old_tios)); | ... | ... |