diff --git a/modbus/modbus.c b/modbus/modbus.c index d2ec62f..1108c05 100644 --- a/modbus/modbus.c +++ b/modbus/modbus.c @@ -172,7 +172,7 @@ static const int TAB_MAX_ADU_LENGTH[2] = { /* Treats errors and flush or close connection if necessary */ static void error_treat(modbus_param_t *mb_param, int code, const char *string) { - printf("\nERROR %s (%0X)\n", string, -code); + fprintf(stderr, "\nERROR %s (%0X)\n", string, -code); if (mb_param->error_handling == FLUSH_OR_CONNECT_ON_ERROR) { switch (code) { @@ -508,7 +508,7 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) { \ while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \ if (errno == EINTR) { \ - printf("A non blocked signal was caught\n"); \ + fprintf(stderr, "A non blocked signal was caught\n"); \ /* Necessary after an error */ \ FD_ZERO(&rfds); \ FD_SET(mb_param->fd, &rfds); \ @@ -1102,8 +1102,9 @@ int read_coil_status(modbus_param_t *mb_param, int start_addr, int status; if (nb > MAX_STATUS) { - printf("ERROR Too many coils status requested (%d > %d)\n", - nb, MAX_STATUS); + fprintf(stderr, + "ERROR Too many coils status requested (%d > %d)\n", + nb, MAX_STATUS); return INVALID_DATA; } @@ -1124,7 +1125,8 @@ int read_input_status(modbus_param_t *mb_param, int start_addr, int status; if (nb > MAX_STATUS) { - printf("ERROR Too many input status requested (%d > %d)\n", + fprintf(stderr, + "ERROR Too many input status requested (%d > %d)\n", nb, MAX_STATUS); return INVALID_DATA; } @@ -1148,8 +1150,9 @@ static int read_registers(modbus_param_t *mb_param, int function, uint8_t response[MAX_MESSAGE_LENGTH]; if (nb > MAX_REGISTERS) { - printf("ERROR Too many holding registers requested (%d > %d)\n", - nb, MAX_REGISTERS); + fprintf(stderr, + "ERROR Too many holding registers requested (%d > %d)\n", + nb, MAX_REGISTERS); return INVALID_DATA; } @@ -1184,8 +1187,9 @@ int read_holding_registers(modbus_param_t *mb_param, int status; if (nb > MAX_REGISTERS) { - printf("ERROR Too many holding registers requested (%d > %d)\n", - nb, MAX_REGISTERS); + fprintf(stderr, + "ERROR Too many holding registers requested (%d > %d)\n", + nb, MAX_REGISTERS); return INVALID_DATA; } @@ -1202,8 +1206,9 @@ int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb, int status; if (nb > MAX_REGISTERS) { - printf("ERROR Too many input registers requested (%d > %d)\n", - nb, MAX_REGISTERS); + fprintf(stderr, + "ERROR Too many input registers requested (%d > %d)\n", + nb, MAX_REGISTERS); return INVALID_DATA; } @@ -1275,8 +1280,8 @@ int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb, uint8_t query[MAX_MESSAGE_LENGTH]; if (nb > MAX_STATUS) { - printf("ERROR Writing to too many coils (%d > %d)\n", - nb, MAX_STATUS); + fprintf(stderr, "ERROR Writing to too many coils (%d > %d)\n", + nb, MAX_STATUS); return INVALID_DATA; } @@ -1324,8 +1329,9 @@ int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb, uint8_t query[MAX_MESSAGE_LENGTH]; if (nb > MAX_REGISTERS) { - printf("ERROR Trying to write to too many registers (%d > %d)\n", - nb, MAX_REGISTERS); + fprintf(stderr, + "ERROR Trying to write to too many registers (%d > %d)\n", + nb, MAX_REGISTERS); return INVALID_DATA; } @@ -1448,7 +1454,8 @@ void modbus_set_error_handling(modbus_param_t *mb_param, error_handling == NOP_ON_ERROR) { mb_param->error_handling = error_handling; } else { - printf("Invalid setting for error handling (not changed)\n"); + fprintf(stderr, + "Invalid setting for error handling (not changed)\n"); } } @@ -1460,8 +1467,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) speed_t speed; if (mb_param->debug) { - printf("Opening %s at %d bauds (%s)\n", - mb_param->device, mb_param->baud, mb_param->parity); + fprintf(stderr, "Opening %s at %d bauds (%s)\n", + mb_param->device, mb_param->baud, mb_param->parity); } /* 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) NDELAY option is set on the file via open or fcntl */ mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL); if (mb_param->fd < 0) { - perror("open"); - printf("ERROR Can't open the device %s (%s)\n", - mb_param->device, strerror(errno)); + fprintf(stderr, "ERROR Can't open the device %s (%s)\n", + mb_param->device, strerror(errno)); return -1; } @@ -1523,8 +1529,9 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) break; default: speed = B9600; - printf("WARNING Unknown baud rate %d for %s (B9600 used)\n", - mb_param->baud, mb_param->device); + fprintf(stderr, + "WARNING Unknown baud rate %d for %s (B9600 used)\n", + mb_param->baud, mb_param->device); } /* Set the baud rate */ @@ -1979,7 +1986,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) uint8_t value = 0; if (nb_bits > 8) { - printf("Error: nb_bits is too big\n"); + fprintf(stderr, "ERROR nb_bits is too big\n"); nb_bits = 8; } diff --git a/tests/bandwidth-master.c b/tests/bandwidth-master.c index 37d6a92..554dbbc 100644 --- a/tests/bandwidth-master.c +++ b/tests/bandwidth-master.c @@ -55,7 +55,6 @@ int main(void) /* TCP */ modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE); if (modbus_connect(&mb_param) == -1) { - printf("ERROR Connection failed\n"); exit(1); } diff --git a/tests/bandwidth-slave-many-up.c b/tests/bandwidth-slave-many-up.c index eee24e3..b982856 100644 --- a/tests/bandwidth-slave-many-up.c +++ b/tests/bandwidth-slave-many-up.c @@ -53,7 +53,7 @@ int main(void) ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0); if (ret < 0) { - printf("Memory allocation failure\n"); + fprintf(stderr, "Memory allocation failure\n"); exit(1); } diff --git a/tests/bandwidth-slave-one.c b/tests/bandwidth-slave-one.c index a626372..431a79f 100644 --- a/tests/bandwidth-slave-one.c +++ b/tests/bandwidth-slave-one.c @@ -35,7 +35,7 @@ int main(void) ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0); if (ret < 0) { - printf("Memory allocation failed\n"); + fprintf(stderr, "Memory allocation failed\n"); exit(1); } @@ -52,7 +52,7 @@ int main(void) /* Connection closed by the client, end of server */ break; } else { - printf("Error in modbus_listen (%d)\n", ret); + fprintf(stderr, "Error in modbus_listen (%d)\n", ret); } } diff --git a/tests/random-test-master.c b/tests/random-test-master.c index cf2557b..06b55aa 100644 --- a/tests/random-test-master.c +++ b/tests/random-test-master.c @@ -63,7 +63,6 @@ int main(void) modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE); modbus_set_debug(&mb_param, TRUE); if (modbus_connect(&mb_param) == -1) { - printf("ERROR Connection failed\n"); exit(1); } diff --git a/tests/random-test-slave.c b/tests/random-test-slave.c index 032f324..594acf5 100644 --- a/tests/random-test-slave.c +++ b/tests/random-test-slave.c @@ -35,7 +35,7 @@ int main(void) ret = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500); if (ret < 0) { - printf("Memory allocation failed\n"); + fprintf(stderr, "Memory allocation failed\n"); exit(1); } @@ -54,7 +54,7 @@ int main(void) /* Connection closed by the client, end of server */ break; } else { - printf("Error in modbus_listen (%d)\n", ret); + fprintf(stderr, "Error in modbus_listen (%d)\n", ret); } } diff --git a/tests/unit-test-master.c b/tests/unit-test-master.c index 48852e1..0fcc0c6 100644 --- a/tests/unit-test-master.c +++ b/tests/unit-test-master.c @@ -47,7 +47,6 @@ int main(void) /* modbus_set_debug(&mb_param, TRUE); */ if (modbus_connect(&mb_param) == -1) { - printf("ERROR Connection failed\n"); exit(1); } diff --git a/tests/unit-test-slave.c b/tests/unit-test-slave.c index ca8ec2f..3ff0814 100644 --- a/tests/unit-test-slave.c +++ b/tests/unit-test-slave.c @@ -79,7 +79,7 @@ int main(void) /* Connection closed by the client, end of server */ break; } else { - printf("Error in modbus_listen (%d)\n", ret); + sprintf(stderr, "Error in modbus_listen (%d)\n", ret); } }