Commit 5ec104ae31f7743abcfcefa09662e435e4a11bc7

Authored by Stéphane Raimbault
1 parent a72d756c

Write real errors to stderr

modbus/modbus.c
@@ -172,7 +172,7 @@ static const int TAB_MAX_ADU_LENGTH[2] = { @@ -172,7 +172,7 @@ static const int TAB_MAX_ADU_LENGTH[2] = {
172 /* Treats errors and flush or close connection if necessary */ 172 /* Treats errors and flush or close connection if necessary */
173 static void error_treat(modbus_param_t *mb_param, int code, const char *string) 173 static void error_treat(modbus_param_t *mb_param, int code, const char *string)
174 { 174 {
175 - printf("\nERROR %s (%0X)\n", string, -code); 175 + fprintf(stderr, "\nERROR %s (%0X)\n", string, -code);
176 176
177 if (mb_param->error_handling == FLUSH_OR_CONNECT_ON_ERROR) { 177 if (mb_param->error_handling == FLUSH_OR_CONNECT_ON_ERROR) {
178 switch (code) { 178 switch (code) {
@@ -508,7 +508,7 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) @@ -508,7 +508,7 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
508 { \ 508 { \
509 while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \ 509 while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \
510 if (errno == EINTR) { \ 510 if (errno == EINTR) { \
511 - printf("A non blocked signal was caught\n"); \ 511 + fprintf(stderr, "A non blocked signal was caught\n"); \
512 /* Necessary after an error */ \ 512 /* Necessary after an error */ \
513 FD_ZERO(&rfds); \ 513 FD_ZERO(&rfds); \
514 FD_SET(mb_param->fd, &rfds); \ 514 FD_SET(mb_param->fd, &rfds); \
@@ -1102,8 +1102,9 @@ int read_coil_status(modbus_param_t *mb_param, int start_addr, @@ -1102,8 +1102,9 @@ int read_coil_status(modbus_param_t *mb_param, int start_addr,
1102 int status; 1102 int status;
1103 1103
1104 if (nb > MAX_STATUS) { 1104 if (nb > MAX_STATUS) {
1105 - printf("ERROR Too many coils status requested (%d > %d)\n",  
1106 - nb, MAX_STATUS); 1105 + fprintf(stderr,
  1106 + "ERROR Too many coils status requested (%d > %d)\n",
  1107 + nb, MAX_STATUS);
1107 return INVALID_DATA; 1108 return INVALID_DATA;
1108 } 1109 }
1109 1110
@@ -1124,7 +1125,8 @@ int read_input_status(modbus_param_t *mb_param, int start_addr, @@ -1124,7 +1125,8 @@ int read_input_status(modbus_param_t *mb_param, int start_addr,
1124 int status; 1125 int status;
1125 1126
1126 if (nb > MAX_STATUS) { 1127 if (nb > MAX_STATUS) {
1127 - printf("ERROR Too many input status requested (%d > %d)\n", 1128 + fprintf(stderr,
  1129 + "ERROR Too many input status requested (%d > %d)\n",
1128 nb, MAX_STATUS); 1130 nb, MAX_STATUS);
1129 return INVALID_DATA; 1131 return INVALID_DATA;
1130 } 1132 }
@@ -1148,8 +1150,9 @@ static int read_registers(modbus_param_t *mb_param, int function, @@ -1148,8 +1150,9 @@ static int read_registers(modbus_param_t *mb_param, int function,
1148 uint8_t response[MAX_MESSAGE_LENGTH]; 1150 uint8_t response[MAX_MESSAGE_LENGTH];
1149 1151
1150 if (nb > MAX_REGISTERS) { 1152 if (nb > MAX_REGISTERS) {
1151 - printf("ERROR Too many holding registers requested (%d > %d)\n",  
1152 - nb, MAX_REGISTERS); 1153 + fprintf(stderr,
  1154 + "ERROR Too many holding registers requested (%d > %d)\n",
  1155 + nb, MAX_REGISTERS);
1153 return INVALID_DATA; 1156 return INVALID_DATA;
1154 } 1157 }
1155 1158
@@ -1184,8 +1187,9 @@ int read_holding_registers(modbus_param_t *mb_param, @@ -1184,8 +1187,9 @@ int read_holding_registers(modbus_param_t *mb_param,
1184 int status; 1187 int status;
1185 1188
1186 if (nb > MAX_REGISTERS) { 1189 if (nb > MAX_REGISTERS) {
1187 - printf("ERROR Too many holding registers requested (%d > %d)\n",  
1188 - nb, MAX_REGISTERS); 1190 + fprintf(stderr,
  1191 + "ERROR Too many holding registers requested (%d > %d)\n",
  1192 + nb, MAX_REGISTERS);
1189 return INVALID_DATA; 1193 return INVALID_DATA;
1190 } 1194 }
1191 1195
@@ -1202,8 +1206,9 @@ int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb, @@ -1202,8 +1206,9 @@ int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb,
1202 int status; 1206 int status;
1203 1207
1204 if (nb > MAX_REGISTERS) { 1208 if (nb > MAX_REGISTERS) {
1205 - printf("ERROR Too many input registers requested (%d > %d)\n",  
1206 - nb, MAX_REGISTERS); 1209 + fprintf(stderr,
  1210 + "ERROR Too many input registers requested (%d > %d)\n",
  1211 + nb, MAX_REGISTERS);
1207 return INVALID_DATA; 1212 return INVALID_DATA;
1208 } 1213 }
1209 1214
@@ -1275,8 +1280,8 @@ int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb, @@ -1275,8 +1280,8 @@ int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb,
1275 uint8_t query[MAX_MESSAGE_LENGTH]; 1280 uint8_t query[MAX_MESSAGE_LENGTH];
1276 1281
1277 if (nb > MAX_STATUS) { 1282 if (nb > MAX_STATUS) {
1278 - printf("ERROR Writing to too many coils (%d > %d)\n",  
1279 - nb, MAX_STATUS); 1283 + fprintf(stderr, "ERROR Writing to too many coils (%d > %d)\n",
  1284 + nb, MAX_STATUS);
1280 return INVALID_DATA; 1285 return INVALID_DATA;
1281 } 1286 }
1282 1287
@@ -1324,8 +1329,9 @@ int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb, @@ -1324,8 +1329,9 @@ int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb,
1324 uint8_t query[MAX_MESSAGE_LENGTH]; 1329 uint8_t query[MAX_MESSAGE_LENGTH];
1325 1330
1326 if (nb > MAX_REGISTERS) { 1331 if (nb > MAX_REGISTERS) {
1327 - printf("ERROR Trying to write to too many registers (%d > %d)\n",  
1328 - nb, MAX_REGISTERS); 1332 + fprintf(stderr,
  1333 + "ERROR Trying to write to too many registers (%d > %d)\n",
  1334 + nb, MAX_REGISTERS);
1329 return INVALID_DATA; 1335 return INVALID_DATA;
1330 } 1336 }
1331 1337
@@ -1448,7 +1454,8 @@ void modbus_set_error_handling(modbus_param_t *mb_param, @@ -1448,7 +1454,8 @@ void modbus_set_error_handling(modbus_param_t *mb_param,
1448 error_handling == NOP_ON_ERROR) { 1454 error_handling == NOP_ON_ERROR) {
1449 mb_param->error_handling = error_handling; 1455 mb_param->error_handling = error_handling;
1450 } else { 1456 } else {
1451 - printf("Invalid setting for error handling (not changed)\n"); 1457 + fprintf(stderr,
  1458 + "Invalid setting for error handling (not changed)\n");
1452 } 1459 }
1453 } 1460 }
1454 1461
@@ -1460,8 +1467,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1460,8 +1467,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1460 speed_t speed; 1467 speed_t speed;
1461 1468
1462 if (mb_param->debug) { 1469 if (mb_param->debug) {
1463 - printf("Opening %s at %d bauds (%s)\n",  
1464 - mb_param->device, mb_param->baud, mb_param->parity); 1470 + fprintf(stderr, "Opening %s at %d bauds (%s)\n",
  1471 + mb_param->device, mb_param->baud, mb_param->parity);
1465 } 1472 }
1466 1473
1467 /* The O_NOCTTY flag tells UNIX that this program doesn't want 1474 /* The O_NOCTTY flag tells UNIX that this program doesn't want
@@ -1473,9 +1480,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1473,9 +1480,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1473 NDELAY option is set on the file via open or fcntl */ 1480 NDELAY option is set on the file via open or fcntl */
1474 mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL); 1481 mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
1475 if (mb_param->fd < 0) { 1482 if (mb_param->fd < 0) {
1476 - perror("open");  
1477 - printf("ERROR Can't open the device %s (%s)\n",  
1478 - mb_param->device, strerror(errno)); 1483 + fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
  1484 + mb_param->device, strerror(errno));
1479 return -1; 1485 return -1;
1480 } 1486 }
1481 1487
@@ -1523,8 +1529,9 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1523,8 +1529,9 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1523 break; 1529 break;
1524 default: 1530 default:
1525 speed = B9600; 1531 speed = B9600;
1526 - printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",  
1527 - mb_param->baud, mb_param->device); 1532 + fprintf(stderr,
  1533 + "WARNING Unknown baud rate %d for %s (B9600 used)\n",
  1534 + mb_param->baud, mb_param->device);
1528 } 1535 }
1529 1536
1530 /* Set the baud rate */ 1537 /* Set the baud rate */
@@ -1979,7 +1986,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) @@ -1979,7 +1986,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits)
1979 uint8_t value = 0; 1986 uint8_t value = 0;
1980 1987
1981 if (nb_bits > 8) { 1988 if (nb_bits > 8) {
1982 - printf("Error: nb_bits is too big\n"); 1989 + fprintf(stderr, "ERROR nb_bits is too big\n");
1983 nb_bits = 8; 1990 nb_bits = 8;
1984 } 1991 }
1985 1992
tests/bandwidth-master.c
@@ -55,7 +55,6 @@ int main(void) @@ -55,7 +55,6 @@ int main(void)
55 /* TCP */ 55 /* TCP */
56 modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE); 56 modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
57 if (modbus_connect(&mb_param) == -1) { 57 if (modbus_connect(&mb_param) == -1) {
58 - printf("ERROR Connection failed\n");  
59 exit(1); 58 exit(1);
60 } 59 }
61 60
tests/bandwidth-slave-many-up.c
@@ -53,7 +53,7 @@ int main(void) @@ -53,7 +53,7 @@ int main(void)
53 53
54 ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0); 54 ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
55 if (ret < 0) { 55 if (ret < 0) {
56 - printf("Memory allocation failure\n"); 56 + fprintf(stderr, "Memory allocation failure\n");
57 exit(1); 57 exit(1);
58 } 58 }
59 59
tests/bandwidth-slave-one.c
@@ -35,7 +35,7 @@ int main(void) @@ -35,7 +35,7 @@ int main(void)
35 35
36 ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0); 36 ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
37 if (ret < 0) { 37 if (ret < 0) {
38 - printf("Memory allocation failed\n"); 38 + fprintf(stderr, "Memory allocation failed\n");
39 exit(1); 39 exit(1);
40 } 40 }
41 41
@@ -52,7 +52,7 @@ int main(void) @@ -52,7 +52,7 @@ int main(void)
52 /* Connection closed by the client, end of server */ 52 /* Connection closed by the client, end of server */
53 break; 53 break;
54 } else { 54 } else {
55 - printf("Error in modbus_listen (%d)\n", ret); 55 + fprintf(stderr, "Error in modbus_listen (%d)\n", ret);
56 } 56 }
57 } 57 }
58 58
tests/random-test-master.c
@@ -63,7 +63,6 @@ int main(void) @@ -63,7 +63,6 @@ int main(void)
63 modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE); 63 modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
64 modbus_set_debug(&mb_param, TRUE); 64 modbus_set_debug(&mb_param, TRUE);
65 if (modbus_connect(&mb_param) == -1) { 65 if (modbus_connect(&mb_param) == -1) {
66 - printf("ERROR Connection failed\n");  
67 exit(1); 66 exit(1);
68 } 67 }
69 68
tests/random-test-slave.c
@@ -35,7 +35,7 @@ int main(void) @@ -35,7 +35,7 @@ int main(void)
35 35
36 ret = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500); 36 ret = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500);
37 if (ret < 0) { 37 if (ret < 0) {
38 - printf("Memory allocation failed\n"); 38 + fprintf(stderr, "Memory allocation failed\n");
39 exit(1); 39 exit(1);
40 } 40 }
41 41
@@ -54,7 +54,7 @@ int main(void) @@ -54,7 +54,7 @@ int main(void)
54 /* Connection closed by the client, end of server */ 54 /* Connection closed by the client, end of server */
55 break; 55 break;
56 } else { 56 } else {
57 - printf("Error in modbus_listen (%d)\n", ret); 57 + fprintf(stderr, "Error in modbus_listen (%d)\n", ret);
58 } 58 }
59 } 59 }
60 60
tests/unit-test-master.c
@@ -47,7 +47,6 @@ int main(void) @@ -47,7 +47,6 @@ int main(void)
47 /* modbus_set_debug(&mb_param, TRUE); */ 47 /* modbus_set_debug(&mb_param, TRUE); */
48 48
49 if (modbus_connect(&mb_param) == -1) { 49 if (modbus_connect(&mb_param) == -1) {
50 - printf("ERROR Connection failed\n");  
51 exit(1); 50 exit(1);
52 } 51 }
53 52
tests/unit-test-slave.c
@@ -79,7 +79,7 @@ int main(void) @@ -79,7 +79,7 @@ int main(void)
79 /* Connection closed by the client, end of server */ 79 /* Connection closed by the client, end of server */
80 break; 80 break;
81 } else { 81 } else {
82 - printf("Error in modbus_listen (%d)\n", ret); 82 + sprintf(stderr, "Error in modbus_listen (%d)\n", ret);
83 } 83 }
84 } 84 }
85 85