Commit 7b320fa25a07b591db50677b3ec5370f2b39e70a
1 parent
f4583400
Add preset_multiple_registers in slave side
- add some unit tests - add PRESET_MULTIPLE_REGISTERS support in manage_query()
Showing
2 changed files
with
49 additions
and
15 deletions
modbus/modbus.c
| ... | ... | @@ -857,9 +857,6 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 857 | 857 | resp_length = response_exception(mb_param, slave, function, |
| 858 | 858 | ILLEGAL_DATA_ADDRESS, response); |
| 859 | 859 | } else { |
| 860 | - int i; | |
| 861 | - /* uint8_t byte_count = query[offset+6]; */ | |
| 862 | - | |
| 863 | 860 | /* Similar to build_query_basis! */ |
| 864 | 861 | resp_length = build_response_basis(mb_param, slave, function, response); |
| 865 | 862 | /* 4 to copy the coil address (2) and the quantity of coils */ |
| ... | ... | @@ -868,8 +865,24 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 868 | 865 | } |
| 869 | 866 | } |
| 870 | 867 | break; |
| 868 | + case FC_PRESET_MULTIPLE_REGISTERS: { | |
| 869 | + uint16_t count = (query[offset+4] << 8) + query[offset+5]; | |
| 870 | + | |
| 871 | + if ((address + count) > mb_mapping->nb_holding_registers) { | |
| 872 | + printf("Illegal data address %0X in preset_multiple_registers\n", | |
| 873 | + address + count); | |
| 874 | + resp_length = response_exception(mb_param, slave, function, | |
| 875 | + ILLEGAL_DATA_ADDRESS, response); | |
| 876 | + } else { | |
| 877 | + /* Similar to build_query_basis! */ | |
| 878 | + resp_length = build_response_basis(mb_param, slave, function, response); | |
| 879 | + /* 4 to copy the address (2) and the no. of registers */ | |
| 880 | + memcpy(response + resp_length, query + resp_length, 4); | |
| 881 | + resp_length += 4; | |
| 882 | + } | |
| 883 | + } | |
| 884 | + break; | |
| 871 | 885 | case FC_READ_EXCEPTION_STATUS: |
| 872 | - case FC_PRESET_MULTIPLE_REGISTERS: | |
| 873 | 886 | case FC_REPORT_SLAVE_ID: |
| 874 | 887 | printf("Not implemented\n"); |
| 875 | 888 | break; | ... | ... |
tests/unit-test-master.c
| ... | ... | @@ -45,7 +45,7 @@ int main(void) |
| 45 | 45 | |
| 46 | 46 | /* TCP */ |
| 47 | 47 | modbus_init_tcp(&mb_param, "127.0.0.1", 1502); |
| 48 | - modbus_set_debug(&mb_param, TRUE); | |
| 48 | +/* modbus_set_debug(&mb_param, TRUE);*/ | |
| 49 | 49 | |
| 50 | 50 | modbus_connect(&mb_param); |
| 51 | 51 | |
| ... | ... | @@ -191,14 +191,16 @@ int main(void) |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | { |
| 194 | - uint16_t tab_value[] = { 0x0A, 0x0102 }; | |
| 195 | - ret = preset_multiple_registers(&mb_param, SLAVE, 0x01, 0x02, tab_value); | |
| 196 | - } | |
| 197 | - if (ret == 2) { | |
| 198 | - printf("OK\n"); | |
| 199 | - } else { | |
| 200 | - printf("FAILED\n"); | |
| 201 | - goto close; | |
| 194 | + int nb_points = 2; | |
| 195 | + uint16_t tab_value[] = { 0x000A, 0x0102 }; | |
| 196 | + ret = preset_multiple_registers(&mb_param, SLAVE, 0x01, nb_points, tab_value); | |
| 197 | + printf("* preset_multiple_registers: "); | |
| 198 | + if (ret == nb_points) { | |
| 199 | + printf("OK\n"); | |
| 200 | + } else { | |
| 201 | + printf("FAILED\n"); | |
| 202 | + goto close; | |
| 203 | + } | |
| 202 | 204 | } |
| 203 | 205 | |
| 204 | 206 | /** ILLEGAL DATA ADDRESS */ |
| ... | ... | @@ -243,11 +245,30 @@ int main(void) |
| 243 | 245 | UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS, ON); |
| 244 | 246 | printf("* force single coil: "); |
| 245 | 247 | if (ret == ILLEGAL_DATA_ADDRESS) { |
| 246 | - printf("OK\n"); | |
| 248 | + printf("OK"); | |
| 247 | 249 | } else { |
| 248 | - printf("FAILED\n"); | |
| 250 | + printf("FAILED"); | |
| 251 | + } | |
| 252 | + | |
| 253 | + ret = force_multiple_coils(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, | |
| 254 | + UT_COIL_STATUS_NB_POINTS + 1, tab_rp_status); | |
| 255 | + printf("* force multipls coils: "); | |
| 256 | + if (ret == ILLEGAL_DATA_ADDRESS) { | |
| 257 | + printf("OK"); | |
| 258 | + } else { | |
| 259 | + printf("FAILED"); | |
| 249 | 260 | } |
| 250 | 261 | |
| 262 | + ret = preset_multiple_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS, | |
| 263 | + UT_HOLDING_REGISTERS_NB_POINTS + 1, tab_rp_registers); | |
| 264 | + printf("* preset multiple registers: "); | |
| 265 | + if (ret == ILLEGAL_DATA_ADDRESS) { | |
| 266 | + printf("OK"); | |
| 267 | + } else { | |
| 268 | + printf("FAILED"); | |
| 269 | + } | |
| 270 | + | |
| 271 | + printf("\n"); | |
| 251 | 272 | close: |
| 252 | 273 | /* Free the memory */ |
| 253 | 274 | free(tab_rp_status); | ... | ... |