Commit 9be43128074c94a40231d5bd11265025eb61d5bd
1 parent
02f16fd4
Enhance error management.
Showing
1 changed file
with
33 additions
and
24 deletions
modbus/modbus.c
| @@ -136,18 +136,24 @@ static int read_reg_response(modbus_param_t *mb_param, | @@ -136,18 +136,24 @@ static int read_reg_response(modbus_param_t *mb_param, | ||
| 136 | /* Treats errors and flush or close connection if necessary */ | 136 | /* Treats errors and flush or close connection if necessary */ |
| 137 | static void error_treat(int code, const char *string, modbus_param_t *mb_param) | 137 | static void error_treat(int code, const char *string, modbus_param_t *mb_param) |
| 138 | { | 138 | { |
| 139 | - if (code == -1) | ||
| 140 | - perror(string); | ||
| 141 | - printf("\n\nERROR %s\n\n", string); | ||
| 142 | - | ||
| 143 | - // FIXME Filter on code | 139 | + // FIXME restore perror management |
| 140 | + // if (code 0) | ||
| 141 | + // perror(string); | ||
| 142 | + printf("\n\nERROR %s (%.2X)\n\n", string, code); | ||
| 144 | 143 | ||
| 145 | if (mb_param->error_handling == FLUSH_OR_RECONNECT_ON_ERROR) { | 144 | if (mb_param->error_handling == FLUSH_OR_RECONNECT_ON_ERROR) { |
| 146 | - if (mb_param->type_com == RTU) { | ||
| 147 | - tcflush(mb_param->fd, TCIOFLUSH); | ||
| 148 | - } else { | ||
| 149 | - modbus_close(mb_param); | ||
| 150 | - modbus_connect(mb_param); | 145 | + switch (code) { |
| 146 | + case ILLEGAL_DATA_VALUE: | ||
| 147 | + case ILLEGAL_DATA_ADDRESS: | ||
| 148 | + case ILLEGAL_FUNCTION: | ||
| 149 | + break; | ||
| 150 | + default: | ||
| 151 | + if (mb_param->type_com == RTU) { | ||
| 152 | + tcflush(mb_param->fd, TCIOFLUSH); | ||
| 153 | + } else { | ||
| 154 | + modbus_close(mb_param); | ||
| 155 | + modbus_connect(mb_param); | ||
| 156 | + } | ||
| 151 | } | 157 | } |
| 152 | } | 158 | } |
| 153 | } | 159 | } |
| @@ -337,8 +343,8 @@ int check_crc16(modbus_param_t *mb_param, | @@ -337,8 +343,8 @@ int check_crc16(modbus_param_t *mb_param, | ||
| 337 | char s_error[64]; | 343 | char s_error[64]; |
| 338 | sprintf(s_error, "invalid crc received %0X - crc_calc %0X", | 344 | sprintf(s_error, "invalid crc received %0X - crc_calc %0X", |
| 339 | crc_received, crc_calc); | 345 | crc_received, crc_calc); |
| 340 | - error_treat(0, s_error, mb_param); | ||
| 341 | ret = INVALID_CRC; | 346 | ret = INVALID_CRC; |
| 347 | + error_treat(ret, s_error, mb_param); | ||
| 342 | } | 348 | } |
| 343 | } else { | 349 | } else { |
| 344 | /* In TCP, the modbus CRC is not present (see HDLC level) */ | 350 | /* In TCP, the modbus CRC is not present (see HDLC level) */ |
| @@ -380,8 +386,8 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, | @@ -380,8 +386,8 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, | ||
| 380 | /* Return the number of bytes written (0 to n) | 386 | /* Return the number of bytes written (0 to n) |
| 381 | or PORT_SOCKET_FAILURE on error */ | 387 | or PORT_SOCKET_FAILURE on error */ |
| 382 | if ((write_ret == -1) || (write_ret != query_size)) { | 388 | if ((write_ret == -1) || (write_ret != query_size)) { |
| 383 | - error_treat(write_ret, "Write port/socket failure", mb_param); | ||
| 384 | write_ret = PORT_SOCKET_FAILURE; | 389 | write_ret = PORT_SOCKET_FAILURE; |
| 390 | + error_treat(write_ret, "Write port/socket failure", mb_param); | ||
| 385 | } | 391 | } |
| 386 | 392 | ||
| 387 | return write_ret; | 393 | return write_ret; |
| @@ -434,7 +440,7 @@ static uint8_t compute_query_size_data(modbus_param_t *mb_param, uint8_t *msg) | @@ -434,7 +440,7 @@ static uint8_t compute_query_size_data(modbus_param_t *mb_param, uint8_t *msg) | ||
| 434 | FD_ZERO(&rfds); \ | 440 | FD_ZERO(&rfds); \ |
| 435 | FD_SET(mb_param->fd, &rfds); \ | 441 | FD_SET(mb_param->fd, &rfds); \ |
| 436 | } else { \ | 442 | } else { \ |
| 437 | - error_treat(select_ret, "Select failure", mb_param); \ | 443 | + error_treat(SELECT_FAILURE, "Select failure", mb_param); \ |
| 438 | return SELECT_FAILURE; \ | 444 | return SELECT_FAILURE; \ |
| 439 | } \ | 445 | } \ |
| 440 | } \ | 446 | } \ |
| @@ -511,7 +517,7 @@ int receive_msg(modbus_param_t *mb_param, | @@ -511,7 +517,7 @@ int receive_msg(modbus_param_t *mb_param, | ||
| 511 | read_ret = recv(mb_param->fd, p_msg, size_to_read, 0); | 517 | read_ret = recv(mb_param->fd, p_msg, size_to_read, 0); |
| 512 | 518 | ||
| 513 | if (read_ret == -1) { | 519 | if (read_ret == -1) { |
| 514 | - error_treat(read_ret, "Read port/socket failure", mb_param); | 520 | + error_treat(PORT_SOCKET_FAILURE, "Read port/socket failure", mb_param); |
| 515 | return PORT_SOCKET_FAILURE; | 521 | return PORT_SOCKET_FAILURE; |
| 516 | } else if (read_ret == 0) { | 522 | } else if (read_ret == 0) { |
| 517 | printf("Connection closed\n"); | 523 | printf("Connection closed\n"); |
| @@ -521,7 +527,7 @@ int receive_msg(modbus_param_t *mb_param, | @@ -521,7 +527,7 @@ int receive_msg(modbus_param_t *mb_param, | ||
| 521 | /* Sums bytes received */ | 527 | /* Sums bytes received */ |
| 522 | (*msg_size) += read_ret; | 528 | (*msg_size) += read_ret; |
| 523 | if ((*msg_size) > MAX_PACKET_SIZE) { | 529 | if ((*msg_size) > MAX_PACKET_SIZE) { |
| 524 | - error_treat(0, "Too many datas", mb_param); | 530 | + error_treat(TOO_MANY_DATAS, "Too many datas", mb_param); |
| 525 | return TOO_MANY_DATAS; | 531 | return TOO_MANY_DATAS; |
| 526 | } | 532 | } |
| 527 | 533 | ||
| @@ -641,30 +647,33 @@ static int modbus_check_response(modbus_param_t *mb_param, | @@ -641,30 +647,33 @@ static int modbus_check_response(modbus_param_t *mb_param, | ||
| 641 | if (ret != 0) | 647 | if (ret != 0) |
| 642 | return ret; | 648 | return ret; |
| 643 | 649 | ||
| 644 | - /* Check for exception response | ||
| 645 | - 0x80 + function */ | 650 | + /* Check for exception response. |
| 651 | + 0x80 + function is stored in the exception | ||
| 652 | + response. */ | ||
| 646 | if (0x80 + query[offset + 1] == response[offset + 1]) { | 653 | if (0x80 + query[offset + 1] == response[offset + 1]) { |
| 647 | 654 | ||
| 648 | - if (response[offset + 2] < NB_TAB_ERROR_MSG) { | ||
| 649 | - error_treat(0, | 655 | + int exception_code = response[offset + 2]; |
| 656 | + // FIXME check test | ||
| 657 | + if (exception_code < NB_TAB_ERROR_MSG) { | ||
| 658 | + error_treat(-exception_code, | ||
| 650 | TAB_ERROR_MSG[response[offset + 2]], | 659 | TAB_ERROR_MSG[response[offset + 2]], |
| 651 | mb_param); | 660 | mb_param); |
| 652 | - /* Modbus error code (negative) */ | ||
| 653 | - return -response[offset + 2]; | 661 | + /* Modbus error code is negative */ |
| 662 | + return -exception_code; | ||
| 654 | } else { | 663 | } else { |
| 655 | /* The chances are low to hit this | 664 | /* The chances are low to hit this |
| 656 | case but can avoid a vicious | 665 | case but can avoid a vicious |
| 657 | segfault */ | 666 | segfault */ |
| 658 | char s_error[64]; | 667 | char s_error[64]; |
| 659 | sprintf(s_error, "Invalid exception code %d", response[offset + 2]); | 668 | sprintf(s_error, "Invalid exception code %d", response[offset + 2]); |
| 660 | - error_treat(0, s_error, mb_param); | 669 | + error_treat(INVALID_EXCEPTION_CODE, s_error, mb_param); |
| 661 | free(s_error); | 670 | free(s_error); |
| 662 | return INVALID_EXCEPTION_CODE; | 671 | return INVALID_EXCEPTION_CODE; |
| 663 | } | 672 | } |
| 664 | } | 673 | } |
| 665 | } else if (ret == COMM_TIME_OUT) { | 674 | } else if (ret == COMM_TIME_OUT) { |
| 666 | - error_treat(0, "Communication time out", mb_param); | ||
| 667 | - return COMM_TIME_OUT; | 675 | + error_treat(ret, "Communication time out", mb_param); |
| 676 | + return ret; | ||
| 668 | } else { | 677 | } else { |
| 669 | return ret; | 678 | return ret; |
| 670 | } | 679 | } |