Commit 369c055ed2fef75130b3cc3398c5f18bbe4b78cc
1 parent
8f643667
Fix CRC error when a slave RTU send a response.
Thanks to Justin Carroll to have reported and tested my patch.
Showing
1 changed file
with
10 additions
and
6 deletions
modbus/modbus.c
| ... | ... | @@ -773,10 +773,12 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 773 | 773 | |
| 774 | 774 | sft.slave = slave; |
| 775 | 775 | sft.function = function; |
| 776 | - if (mb_param->type_com == TCP) | |
| 776 | + if (mb_param->type_com == TCP) { | |
| 777 | 777 | sft.t_id = (query[0] << 8) + query[1]; |
| 778 | - else | |
| 778 | + } else { | |
| 779 | 779 | sft.t_id = 0; |
| 780 | + query_length -= CHECKSUM_LENGTH_RTU; | |
| 781 | + } | |
| 780 | 782 | |
| 781 | 783 | switch (function) { |
| 782 | 784 | case FC_READ_COIL_STATUS: { |
| ... | ... | @@ -868,9 +870,11 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 868 | 870 | if (data == 0xFF00 || data == 0x0) { |
| 869 | 871 | mb_mapping->tab_coil_status[address] = (data) ? ON : OFF; |
| 870 | 872 | |
| 871 | - /* In RTU mode, the CRC is computed | |
| 872 | - and added to the query by modbus_send */ | |
| 873 | - memcpy(response, query, query_length - mb_param->checksum_length); | |
| 873 | + /* In RTU mode, the CRC is computed and added | |
| 874 | + to the query by modbus_send, the computed | |
| 875 | + CRC will be same and optimisation is | |
| 876 | + possible here (FIXME). */ | |
| 877 | + memcpy(response, query, query_length); | |
| 874 | 878 | resp_length = query_length; |
| 875 | 879 | } else { |
| 876 | 880 | printf("Illegal data value %0X in force_single_coil request at address %0X\n", |
| ... | ... | @@ -889,7 +893,7 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 889 | 893 | int data = (query[offset+4] << 8) + query[offset+5]; |
| 890 | 894 | |
| 891 | 895 | mb_mapping->tab_holding_registers[address] = data; |
| 892 | - memcpy(response, query, query_length - mb_param->checksum_length); | |
| 896 | + memcpy(response, query, query_length); | |
| 893 | 897 | resp_length = query_length; |
| 894 | 898 | } |
| 895 | 899 | break; | ... | ... |