Commit 23ac32815fda100704f51f0062fa2e5b53bd7157
1 parent
f5cd5962
- Rename MAX_PACKET_SIZE to MAX_MESSAGE_LENGTH
- Reduce the max message length to 256 (PI_M300) - s/size/length/
Showing
4 changed files
with
179 additions
and
188 deletions
TODO
| ... | ... | @@ -6,11 +6,7 @@ Features |
| 6 | 6 | |
| 7 | 7 | Cleanups |
| 8 | 8 | * see the max length of a message and define size var accordingly (uint8_t) |
| 9 | -* split compute_response_size to reuse | |
| 10 | -* split modbus.c (tcp/rtu, query/response) | |
| 11 | 9 | * t_id in param_msqg |
| 12 | -* only one build_header_message function to replace build_query_packet/response_packet | |
| 13 | -* avoid copies (use ptr) | |
| 14 | 10 | |
| 15 | 11 | Documentation |
| 16 | 12 | * README with a example to test the library | ... | ... |
modbus/modbus.c
| ... | ... | @@ -27,6 +27,7 @@ |
| 27 | 27 | |
| 28 | 28 | Documentation: |
| 29 | 29 | http://www.easysw.com/~mike/serial/serial.html |
| 30 | + http://copyleft.free.fr/wordpress/index.php/libmodbus/ | |
| 30 | 31 | */ |
| 31 | 32 | |
| 32 | 33 | #include <stdio.h> |
| ... | ... | @@ -155,11 +156,11 @@ static void error_treat(int code, const char *string, modbus_param_t *mb_param) |
| 155 | 156 | } |
| 156 | 157 | } |
| 157 | 158 | |
| 158 | -/* Computes the size of the expected response */ | |
| 159 | -static unsigned int compute_response_size(modbus_param_t *mb_param, | |
| 160 | - uint8_t *query) | |
| 159 | +/* Computes the length of the expected response */ | |
| 160 | +static unsigned int compute_response_length(modbus_param_t *mb_param, | |
| 161 | + uint8_t *query) | |
| 161 | 162 | { |
| 162 | - int response_size_computed; | |
| 163 | + int resp_length; | |
| 163 | 164 | int offset; |
| 164 | 165 | |
| 165 | 166 | offset = mb_param->header_length; |
| ... | ... | @@ -169,29 +170,27 @@ static unsigned int compute_response_size(modbus_param_t *mb_param, |
| 169 | 170 | case FC_READ_INPUT_STATUS: { |
| 170 | 171 | /* Header + nb values (code from force_multiple_coils) */ |
| 171 | 172 | int nb_points = (query[offset + 4] << 8) | query[offset + 5]; |
| 172 | - response_size_computed = 3 + | |
| 173 | - (nb_points / 8) + ((nb_points % 8) ? 1 : 0); | |
| 174 | - } | |
| 173 | + resp_length = 3 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0); | |
| 174 | + } | |
| 175 | 175 | break; |
| 176 | 176 | case FC_READ_HOLDING_REGISTERS: |
| 177 | 177 | case FC_READ_INPUT_REGISTERS: |
| 178 | 178 | /* Header + 2 * nb values */ |
| 179 | - response_size_computed = 3 + | |
| 180 | - 2 * (query[offset + 4] << 8 | query[offset + 5]); | |
| 179 | + resp_length = 3 + 2 * (query[offset + 4] << 8 | query[offset + 5]); | |
| 181 | 180 | break; |
| 182 | 181 | case FC_READ_EXCEPTION_STATUS: |
| 183 | - response_size_computed = 4; | |
| 182 | + resp_length = 4; | |
| 184 | 183 | break; |
| 185 | 184 | default: |
| 186 | - response_size_computed = 6; | |
| 185 | + resp_length = 6; | |
| 187 | 186 | } |
| 188 | 187 | |
| 189 | - response_size_computed += offset + mb_param->checksum_size; | |
| 188 | + resp_length += offset + mb_param->checksum_length; | |
| 190 | 189 | |
| 191 | - return response_size_computed; | |
| 190 | + return resp_length; | |
| 192 | 191 | } |
| 193 | 192 | |
| 194 | -/* Buils a RTU header */ | |
| 193 | +/* Builds a RTU query header */ | |
| 195 | 194 | static int build_query_basis_rtu(uint8_t slave, uint8_t function, |
| 196 | 195 | uint16_t start_addr, uint16_t count, |
| 197 | 196 | uint8_t *query) |
| ... | ... | @@ -203,10 +202,10 @@ static int build_query_basis_rtu(uint8_t slave, uint8_t function, |
| 203 | 202 | query[4] = count >> 8; |
| 204 | 203 | query[5] = count & 0x00ff; |
| 205 | 204 | |
| 206 | - return PRESET_QUERY_SIZE_RTU; | |
| 205 | + return PRESET_QUERY_LENGTH_RTU; | |
| 207 | 206 | } |
| 208 | 207 | |
| 209 | -/* Builds a TCP header */ | |
| 208 | +/* Builds a TCP query header */ | |
| 210 | 209 | static int build_query_basis_tcp(uint8_t slave, uint8_t function, |
| 211 | 210 | uint16_t start_addr, uint16_t count, |
| 212 | 211 | uint8_t *query) |
| ... | ... | @@ -234,7 +233,7 @@ static int build_query_basis_tcp(uint8_t slave, uint8_t function, |
| 234 | 233 | query[10] = count >> 8; |
| 235 | 234 | query[11] = count & 0x00ff; |
| 236 | 235 | |
| 237 | - return PRESET_QUERY_SIZE_TCP; | |
| 236 | + return PRESET_QUERY_LENGTH_TCP; | |
| 238 | 237 | } |
| 239 | 238 | |
| 240 | 239 | static int build_query_basis(modbus_param_t *mb_param, uint8_t slave, |
| ... | ... | @@ -249,14 +248,16 @@ static int build_query_basis(modbus_param_t *mb_param, uint8_t slave, |
| 249 | 248 | count, query); |
| 250 | 249 | } |
| 251 | 250 | |
| 251 | +/* Builds a RTU response header */ | |
| 252 | 252 | static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *response) |
| 253 | 253 | { |
| 254 | 254 | response[0] = slave; |
| 255 | 255 | response[1] = function; |
| 256 | 256 | |
| 257 | - return PRESET_RESPONSE_SIZE_RTU; | |
| 257 | + return PRESET_RESPONSE_LENGTH_RTU; | |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | +/* Builds a TCP response header */ | |
| 260 | 261 | static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *response) |
| 261 | 262 | { |
| 262 | 263 | static uint16_t t_id = 0; |
| ... | ... | @@ -278,7 +279,7 @@ static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *re |
| 278 | 279 | response[6] = slave; |
| 279 | 280 | response[7] = function; |
| 280 | 281 | |
| 281 | - return PRESET_RESPONSE_SIZE_TCP; | |
| 282 | + return PRESET_RESPONSE_LENGTH_TCP; | |
| 282 | 283 | } |
| 283 | 284 | |
| 284 | 285 | static int build_response_basis(modbus_param_t *mb_param, uint8_t slave, |
| ... | ... | @@ -291,12 +292,12 @@ static int build_response_basis(modbus_param_t *mb_param, uint8_t slave, |
| 291 | 292 | } |
| 292 | 293 | |
| 293 | 294 | /* Sets the length of TCP message in the message (query and response) */ |
| 294 | -void set_packet_length_tcp(uint8_t *packet, size_t packet_size) | |
| 295 | +void set_packet_length_tcp(uint8_t *packet, size_t packet_length) | |
| 295 | 296 | { |
| 296 | 297 | uint16_t mbap_length; |
| 297 | 298 | |
| 298 | 299 | /* Substract MBAP header length */ |
| 299 | - mbap_length = packet_size - 6; | |
| 300 | + mbap_length = packet_length - 6; | |
| 300 | 301 | |
| 301 | 302 | packet[4] = mbap_length >> 8; |
| 302 | 303 | packet[5] = mbap_length & 0x00FF; |
| ... | ... | @@ -322,7 +323,7 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length) |
| 322 | 323 | /* If CRC is correct returns 0 else returns INVALID_CRC */ |
| 323 | 324 | int check_crc16(modbus_param_t *mb_param, |
| 324 | 325 | uint8_t *msg, |
| 325 | - const int msg_size) | |
| 326 | + const int msg_length) | |
| 326 | 327 | { |
| 327 | 328 | int ret; |
| 328 | 329 | |
| ... | ... | @@ -330,8 +331,8 @@ int check_crc16(modbus_param_t *mb_param, |
| 330 | 331 | uint16_t crc_calc; |
| 331 | 332 | uint16_t crc_received; |
| 332 | 333 | |
| 333 | - crc_calc = crc16(msg, msg_size - 2); | |
| 334 | - crc_received = (msg[msg_size - 2] << 8) | msg[msg_size - 1]; | |
| 334 | + crc_calc = crc16(msg, msg_length - 2); | |
| 335 | + crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1]; | |
| 335 | 336 | |
| 336 | 337 | /* Check CRC of msg */ |
| 337 | 338 | if (crc_calc == crc_received) { |
| ... | ... | @@ -353,36 +354,36 @@ int check_crc16(modbus_param_t *mb_param, |
| 353 | 354 | |
| 354 | 355 | /* Sends a query/response over a serial or a TCP communication */ |
| 355 | 356 | static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 356 | - size_t query_size) | |
| 357 | + size_t query_length) | |
| 357 | 358 | { |
| 358 | 359 | int ret; |
| 359 | 360 | uint16_t s_crc; |
| 360 | 361 | int i; |
| 361 | 362 | |
| 362 | 363 | if (mb_param->type_com == RTU) { |
| 363 | - s_crc = crc16(query, query_size); | |
| 364 | - query[query_size++] = s_crc >> 8; | |
| 365 | - query[query_size++] = s_crc & 0x00FF; | |
| 364 | + s_crc = crc16(query, query_length); | |
| 365 | + query[query_length++] = s_crc >> 8; | |
| 366 | + query[query_length++] = s_crc & 0x00FF; | |
| 366 | 367 | } else { |
| 367 | - set_packet_length_tcp(query, query_size); | |
| 368 | + set_packet_length_tcp(query, query_length); | |
| 368 | 369 | } |
| 369 | 370 | |
| 370 | 371 | if (mb_param->debug) { |
| 371 | 372 | printf("\n"); |
| 372 | - for (i = 0; i < query_size; i++) | |
| 373 | + for (i = 0; i < query_length; i++) | |
| 373 | 374 | printf("[%.2X]", query[i]); |
| 374 | 375 | |
| 375 | 376 | printf("\n"); |
| 376 | 377 | } |
| 377 | 378 | |
| 378 | 379 | if (mb_param->type_com == RTU) |
| 379 | - ret = write(mb_param->fd, query, query_size); | |
| 380 | + ret = write(mb_param->fd, query, query_length); | |
| 380 | 381 | else |
| 381 | - ret = send(mb_param->fd, query, query_size, 0); | |
| 382 | + ret = send(mb_param->fd, query, query_length, 0); | |
| 382 | 383 | |
| 383 | 384 | /* Return the number of bytes written (0 to n) |
| 384 | 385 | or PORT_SOCKET_FAILURE on error */ |
| 385 | - if ((ret == -1) || (ret != query_size)) { | |
| 386 | + if ((ret == -1) || (ret != query_length)) { | |
| 386 | 387 | ret = PORT_SOCKET_FAILURE; |
| 387 | 388 | error_treat(ret, "Write port/socket failure", mb_param); |
| 388 | 389 | } |
| ... | ... | @@ -390,8 +391,8 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 390 | 391 | return ret; |
| 391 | 392 | } |
| 392 | 393 | |
| 393 | -/* Computes the size of the header following the function code */ | |
| 394 | -static uint8_t compute_query_size_header(uint8_t function) | |
| 394 | +/* Computes the length of the header following the function code */ | |
| 395 | +static uint8_t compute_query_length_header(uint8_t function) | |
| 395 | 396 | { |
| 396 | 397 | uint8_t byte; |
| 397 | 398 | |
| ... | ... | @@ -406,13 +407,11 @@ static uint8_t compute_query_size_header(uint8_t function) |
| 406 | 407 | else |
| 407 | 408 | byte = 0; |
| 408 | 409 | |
| 409 | -// printf("compute_query_size_header FC %d, B %d\n", function, byte); | |
| 410 | - | |
| 411 | 410 | return byte; |
| 412 | 411 | } |
| 413 | 412 | |
| 414 | -/* Computes the size of the data to write in the query */ | |
| 415 | -static uint8_t compute_query_size_data(modbus_param_t *mb_param, uint8_t *msg) | |
| 413 | +/* Computes the length of the data to write in the query */ | |
| 414 | +static uint8_t compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) | |
| 416 | 415 | { |
| 417 | 416 | uint8_t function = msg[mb_param->header_length + 1]; |
| 418 | 417 | uint8_t byte; |
| ... | ... | @@ -423,76 +422,75 @@ static uint8_t compute_query_size_data(modbus_param_t *mb_param, uint8_t *msg) |
| 423 | 422 | else |
| 424 | 423 | byte = 0; |
| 425 | 424 | |
| 426 | - byte += mb_param->checksum_size; | |
| 427 | -// printf("compute_query_size_data FC %d, B %d\n", function, byte); | |
| 425 | + byte += mb_param->checksum_length; | |
| 428 | 426 | |
| 429 | 427 | return byte; |
| 430 | 428 | } |
| 431 | 429 | |
| 432 | -#define WAIT_DATA() \ | |
| 433 | -{ \ | |
| 434 | - while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \ | |
| 435 | - if (errno == EINTR) { \ | |
| 436 | - printf("A non blocked signal was caught\n"); \ | |
| 437 | - /* Necessary after an error */ \ | |
| 438 | - FD_ZERO(&rfds); \ | |
| 439 | - FD_SET(mb_param->fd, &rfds); \ | |
| 440 | - } else { \ | |
| 441 | - error_treat(SELECT_FAILURE, "Select failure", mb_param); \ | |
| 442 | - return SELECT_FAILURE; \ | |
| 443 | - } \ | |
| 444 | - } \ | |
| 445 | - \ | |
| 446 | - if (select_ret == 0) { \ | |
| 447 | - /* Call to error_treat is done later to manage exceptions */ \ | |
| 448 | - return COMM_TIME_OUT; \ | |
| 449 | - } \ | |
| 450 | -} | |
| 430 | +#define WAIT_DATA() \ | |
| 431 | + { \ | |
| 432 | + while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \ | |
| 433 | + if (errno == EINTR) { \ | |
| 434 | + printf("A non blocked signal was caught\n"); \ | |
| 435 | + /* Necessary after an error */ \ | |
| 436 | + FD_ZERO(&rfds); \ | |
| 437 | + FD_SET(mb_param->fd, &rfds); \ | |
| 438 | + } else { \ | |
| 439 | + error_treat(SELECT_FAILURE, "Select failure", mb_param); \ | |
| 440 | + return SELECT_FAILURE; \ | |
| 441 | + } \ | |
| 442 | + } \ | |
| 443 | + \ | |
| 444 | + if (select_ret == 0) { \ | |
| 445 | + /* Call to error_treat is done later to manage exceptions */ \ | |
| 446 | + return COMM_TIME_OUT; \ | |
| 447 | + } \ | |
| 448 | + } | |
| 451 | 449 | |
| 452 | 450 | /* Monitors for the reply from the modbus slave or to receive query |
| 453 | 451 | from a modbus master. |
| 454 | 452 | This function blocks for timeout seconds if there is no reply. |
| 455 | 453 | |
| 456 | - msg_size_computed must be set to MSG_SIZE_COMPUTED if undefined | |
| 454 | + msg_length_computed must be set to MSG_LENGTH_COMPUTED if undefined | |
| 457 | 455 | |
| 458 | 456 | Returns: |
| 459 | 457 | - 0: OK, <0: error |
| 460 | - - msg_size: number of characters received. */ | |
| 458 | + - msg_length: number of characters received. */ | |
| 461 | 459 | int receive_msg(modbus_param_t *mb_param, |
| 462 | - int msg_size_computed, | |
| 460 | + int msg_length_computed, | |
| 463 | 461 | uint8_t *msg, |
| 464 | - int *msg_size) | |
| 462 | + int *msg_length) | |
| 465 | 463 | { |
| 466 | 464 | int select_ret; |
| 467 | 465 | int read_ret; |
| 468 | 466 | fd_set rfds; |
| 469 | 467 | struct timeval tv; |
| 470 | - int size_to_read; | |
| 468 | + int length_to_read; | |
| 471 | 469 | uint8_t *p_msg; |
| 472 | 470 | enum { FUNCTION, BYTE, COMPLETE }; |
| 473 | 471 | int state; |
| 474 | 472 | |
| 475 | 473 | if (mb_param->debug) { |
| 476 | - if (msg_size_computed == MSG_SIZE_UNDEFINED) | |
| 474 | + if (msg_length_computed == MSG_LENGTH_UNDEFINED) | |
| 477 | 475 | printf("Waiting for a message...\n"); |
| 478 | 476 | else |
| 479 | - printf("Waiting for a message (%d bytes)...\n", msg_size_computed); | |
| 477 | + printf("Waiting for a message (%d bytes)...\n", msg_length_computed); | |
| 480 | 478 | } |
| 481 | 479 | |
| 482 | 480 | /* Add a file descriptor to the set */ |
| 483 | 481 | FD_ZERO(&rfds); |
| 484 | 482 | FD_SET(mb_param->fd, &rfds); |
| 485 | 483 | |
| 486 | - if (msg_size_computed == MSG_SIZE_UNDEFINED) { | |
| 484 | + if (msg_length_computed == MSG_LENGTH_UNDEFINED) { | |
| 487 | 485 | /* Wait for a message */ |
| 488 | 486 | tv.tv_sec = 60; |
| 489 | 487 | tv.tv_usec = 0; |
| 490 | 488 | |
| 491 | - /* The message size is undefined (query receiving) so | |
| 489 | + /* The message length is undefined (query receiving) so | |
| 492 | 490 | * we need to analyse the message step by step. |
| 493 | 491 | * At the first step, we want to reach the function |
| 494 | 492 | * code because all packets have this information. */ |
| 495 | - msg_size_computed = mb_param->header_length + 2; | |
| 493 | + msg_length_computed = mb_param->header_length + 2; | |
| 496 | 494 | state = FUNCTION; |
| 497 | 495 | } else { |
| 498 | 496 | tv.tv_sec = 0; |
| ... | ... | @@ -500,20 +498,20 @@ int receive_msg(modbus_param_t *mb_param, |
| 500 | 498 | state = COMPLETE; |
| 501 | 499 | } |
| 502 | 500 | |
| 503 | - size_to_read = msg_size_computed; | |
| 501 | + length_to_read = msg_length_computed; | |
| 504 | 502 | |
| 505 | 503 | select_ret = 0; |
| 506 | 504 | WAIT_DATA(); |
| 507 | 505 | |
| 508 | 506 | /* Read the msg */ |
| 509 | - (*msg_size) = 0; | |
| 507 | + (*msg_length) = 0; | |
| 510 | 508 | p_msg = msg; |
| 511 | 509 | |
| 512 | 510 | while (select_ret) { |
| 513 | 511 | if (mb_param->type_com == RTU) |
| 514 | - read_ret = read(mb_param->fd, p_msg, size_to_read); | |
| 512 | + read_ret = read(mb_param->fd, p_msg, length_to_read); | |
| 515 | 513 | else |
| 516 | - read_ret = recv(mb_param->fd, p_msg, size_to_read, 0); | |
| 514 | + read_ret = recv(mb_param->fd, p_msg, length_to_read, 0); | |
| 517 | 515 | |
| 518 | 516 | if (read_ret == -1) { |
| 519 | 517 | error_treat(PORT_SOCKET_FAILURE, "Read port/socket failure", mb_param); |
| ... | ... | @@ -524,8 +522,8 @@ int receive_msg(modbus_param_t *mb_param, |
| 524 | 522 | } |
| 525 | 523 | |
| 526 | 524 | /* Sums bytes received */ |
| 527 | - (*msg_size) += read_ret; | |
| 528 | - if ((*msg_size) > MAX_PACKET_SIZE) { | |
| 525 | + (*msg_length) += read_ret; | |
| 526 | + if ((*msg_length) > MAX_MESSAGE_LENGTH) { | |
| 529 | 527 | error_treat(TOO_MANY_DATAS, "Too many datas", mb_param); |
| 530 | 528 | return TOO_MANY_DATAS; |
| 531 | 529 | } |
| ... | ... | @@ -537,34 +535,34 @@ int receive_msg(modbus_param_t *mb_param, |
| 537 | 535 | printf("<%.2X>", p_msg[i]); |
| 538 | 536 | } |
| 539 | 537 | |
| 540 | - if ((*msg_size) < msg_size_computed) { | |
| 538 | + if ((*msg_length) < msg_length_computed) { | |
| 541 | 539 | /* Message incomplete */ |
| 542 | - size_to_read = msg_size_computed - (*msg_size); | |
| 540 | + length_to_read = msg_length_computed - (*msg_length); | |
| 543 | 541 | } else { |
| 544 | 542 | switch (state) { |
| 545 | 543 | case FUNCTION: |
| 546 | 544 | /* Function code position */ |
| 547 | - size_to_read = compute_query_size_header(msg[mb_param->header_length + 1]); | |
| 548 | - msg_size_computed += size_to_read; | |
| 545 | + length_to_read = compute_query_length_header(msg[mb_param->header_length + 1]); | |
| 546 | + msg_length_computed += length_to_read; | |
| 549 | 547 | state = BYTE; |
| 550 | 548 | break; |
| 551 | 549 | case BYTE: |
| 552 | - size_to_read = compute_query_size_data(mb_param, msg); | |
| 553 | - msg_size_computed += size_to_read; | |
| 550 | + length_to_read = compute_query_length_data(mb_param, msg); | |
| 551 | + msg_length_computed += length_to_read; | |
| 554 | 552 | state = COMPLETE; |
| 555 | 553 | break; |
| 556 | 554 | case COMPLETE: |
| 557 | - size_to_read = 0; | |
| 555 | + length_to_read = 0; | |
| 558 | 556 | break; |
| 559 | 557 | } |
| 560 | 558 | } |
| 561 | 559 | if (mb_param->debug) |
| 562 | - printf("\nsize_to_read: %d\n", size_to_read); | |
| 560 | + printf("\nlength_to_read: %d\n", length_to_read); | |
| 563 | 561 | |
| 564 | 562 | /* Moves the pointer to receive other datas */ |
| 565 | 563 | p_msg = &(p_msg[read_ret]); |
| 566 | 564 | |
| 567 | - if (size_to_read > 0) { | |
| 565 | + if (length_to_read > 0) { | |
| 568 | 566 | /* If no character at the buffer wait |
| 569 | 567 | TIME_OUT_END_OF_TRAME before to generate an error. */ |
| 570 | 568 | tv.tv_sec = 0; |
| ... | ... | @@ -597,16 +595,16 @@ static int modbus_check_response(modbus_param_t *mb_param, |
| 597 | 595 | uint8_t *query, |
| 598 | 596 | uint8_t *response) |
| 599 | 597 | { |
| 600 | - int response_size; | |
| 601 | - int response_size_computed; | |
| 598 | + int response_length; | |
| 599 | + int response_length_computed; | |
| 602 | 600 | int offset = mb_param->header_length; |
| 603 | 601 | int ret; |
| 604 | 602 | |
| 605 | - response_size_computed = compute_response_size(mb_param, query); | |
| 606 | - ret = receive_msg(mb_param, response_size_computed, response, &response_size); | |
| 603 | + response_length_computed = compute_response_length(mb_param, query); | |
| 604 | + ret = receive_msg(mb_param, response_length_computed, response, &response_length); | |
| 607 | 605 | if (ret == 0) { |
| 608 | 606 | /* Check message */ |
| 609 | - ret = check_crc16(mb_param, response, response_size); | |
| 607 | + ret = check_crc16(mb_param, response, response_length); | |
| 610 | 608 | if (ret != 0) |
| 611 | 609 | return ret; |
| 612 | 610 | |
| ... | ... | @@ -615,17 +613,17 @@ static int modbus_check_response(modbus_param_t *mb_param, |
| 615 | 613 | case FC_READ_COIL_STATUS: |
| 616 | 614 | case FC_READ_INPUT_STATUS: |
| 617 | 615 | /* Read functions 1 value = 1 byte */ |
| 618 | - response_size = response[offset + 2]; | |
| 616 | + response_length = response[offset + 2]; | |
| 619 | 617 | break; |
| 620 | 618 | case FC_READ_HOLDING_REGISTERS: |
| 621 | 619 | case FC_READ_INPUT_REGISTERS: |
| 622 | 620 | /* Read functions 1 value = 2 bytes */ |
| 623 | - response_size = response[offset + 2] / 2; | |
| 621 | + response_length = response[offset + 2] / 2; | |
| 624 | 622 | break; |
| 625 | 623 | case FC_FORCE_MULTIPLE_COILS: |
| 626 | 624 | case FC_PRESET_MULTIPLE_REGISTERS: |
| 627 | 625 | /* N Write functions */ |
| 628 | - response_size = response[offset + 4] << 8 | | |
| 626 | + response_length = response[offset + 4] << 8 | | |
| 629 | 627 | response[offset + 5]; |
| 630 | 628 | break; |
| 631 | 629 | case FC_REPORT_SLAVE_ID: |
| ... | ... | @@ -633,16 +631,16 @@ static int modbus_check_response(modbus_param_t *mb_param, |
| 633 | 631 | break; |
| 634 | 632 | default: |
| 635 | 633 | /* 1 Write functions & others */ |
| 636 | - response_size = 1; | |
| 634 | + response_length = 1; | |
| 637 | 635 | } |
| 638 | 636 | |
| 639 | 637 | } else if (ret == COMM_TIME_OUT && |
| 640 | - response_size == offset + 3 + mb_param->checksum_size) { | |
| 638 | + response_length == offset + 3 + mb_param->checksum_length) { | |
| 641 | 639 | /* Optimisation allowed because exception response is |
| 642 | 640 | the smallest trame in modbus protocol (3) so always |
| 643 | 641 | raise an timeout error */ |
| 644 | 642 | /* CRC */ |
| 645 | - ret = check_crc16(mb_param, response, response_size); | |
| 643 | + ret = check_crc16(mb_param, response, response_length); | |
| 646 | 644 | if (ret != 0) |
| 647 | 645 | return ret; |
| 648 | 646 | |
| ... | ... | @@ -677,7 +675,7 @@ static int modbus_check_response(modbus_param_t *mb_param, |
| 677 | 675 | return ret; |
| 678 | 676 | } |
| 679 | 677 | |
| 680 | - return response_size; | |
| 678 | + return response_length; | |
| 681 | 679 | } |
| 682 | 680 | |
| 683 | 681 | static int response_io_status(uint16_t address, uint16_t count, |
| ... | ... | @@ -710,14 +708,14 @@ static int response_exception(modbus_param_t *mb_param, int slave, |
| 710 | 708 | int function, int exception_code, |
| 711 | 709 | uint8_t *response) |
| 712 | 710 | { |
| 713 | - int response_size; | |
| 711 | + int response_length; | |
| 714 | 712 | |
| 715 | - response_size = build_response_basis(mb_param, slave, | |
| 716 | - function + 0x80, response); | |
| 713 | + response_length = build_response_basis(mb_param, slave, | |
| 714 | + function + 0x80, response); | |
| 717 | 715 | /* Positive exception code */ |
| 718 | - response[response_size++] = -exception_code; | |
| 716 | + response[response_length++] = -exception_code; | |
| 719 | 717 | |
| 720 | - return response_size; | |
| 718 | + return response_length; | |
| 721 | 719 | } |
| 722 | 720 | |
| 723 | 721 | /* Manages the received query. |
| ... | ... | @@ -726,13 +724,13 @@ static int response_exception(modbus_param_t *mb_param, int slave, |
| 726 | 724 | accordingly. |
| 727 | 725 | */ |
| 728 | 726 | void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 729 | - int query_size, modbus_mapping_t *mb_mapping) | |
| 727 | + int query_length, modbus_mapping_t *mb_mapping) | |
| 730 | 728 | { |
| 731 | 729 | int offset = mb_param->header_length; |
| 732 | 730 | int slave = query[offset]; |
| 733 | 731 | int function = query[offset+1]; |
| 734 | 732 | uint16_t address = (query[offset+2] << 8) + query[offset+3]; |
| 735 | - uint8_t response[MAX_PACKET_SIZE]; | |
| 733 | + uint8_t response[MAX_MESSAGE_LENGTH]; | |
| 736 | 734 | int resp_length = 0; |
| 737 | 735 | |
| 738 | 736 | switch (function) { |
| ... | ... | @@ -825,8 +823,8 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 825 | 823 | |
| 826 | 824 | /* In RTU mode, the CRC is computed |
| 827 | 825 | and added to the query by modbus_send */ |
| 828 | - memcpy(response, query, query_size - mb_param->checksum_size); | |
| 829 | - resp_length = query_size; | |
| 826 | + memcpy(response, query, query_length - mb_param->checksum_length); | |
| 827 | + resp_length = query_length; | |
| 830 | 828 | } else { |
| 831 | 829 | printf("Illegal data value %0X in force_single_coil request at address %0X\n", |
| 832 | 830 | data, address); |
| ... | ... | @@ -839,13 +837,13 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 839 | 837 | if (address >= mb_mapping->nb_holding_registers) { |
| 840 | 838 | printf("Illegal data address %0X in preset_holding_register\n", address); |
| 841 | 839 | resp_length = response_exception(mb_param, slave, function, |
| 842 | - ILLEGAL_DATA_ADDRESS, response); | |
| 840 | + ILLEGAL_DATA_ADDRESS, response); | |
| 843 | 841 | } else { |
| 844 | 842 | int data = (query[offset+4] << 8) + query[offset+5]; |
| 845 | 843 | |
| 846 | 844 | mb_mapping->tab_holding_registers[address] = data; |
| 847 | - memcpy(response, query, query_size - mb_param->checksum_size); | |
| 848 | - resp_length = query_size; | |
| 845 | + memcpy(response, query, query_length - mb_param->checksum_length); | |
| 846 | + resp_length = query_length; | |
| 849 | 847 | } |
| 850 | 848 | break; |
| 851 | 849 | case FC_FORCE_MULTIPLE_COILS: { |
| ... | ... | @@ -857,7 +855,6 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 857 | 855 | resp_length = response_exception(mb_param, slave, function, |
| 858 | 856 | ILLEGAL_DATA_ADDRESS, response); |
| 859 | 857 | } else { |
| 860 | - /* Similar to build_query_basis! */ | |
| 861 | 858 | resp_length = build_response_basis(mb_param, slave, function, response); |
| 862 | 859 | /* 4 to copy the coil address (2) and the quantity of coils */ |
| 863 | 860 | memcpy(response + resp_length, query + resp_length, 4); |
| ... | ... | @@ -874,7 +871,6 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 874 | 871 | resp_length = response_exception(mb_param, slave, function, |
| 875 | 872 | ILLEGAL_DATA_ADDRESS, response); |
| 876 | 873 | } else { |
| 877 | - /* Similar to build_query_basis! */ | |
| 878 | 874 | resp_length = build_response_basis(mb_param, slave, function, response); |
| 879 | 875 | /* 4 to copy the address (2) and the no. of registers */ |
| 880 | 876 | memcpy(response + resp_length, query + resp_length, 4); |
| ... | ... | @@ -895,14 +891,14 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 895 | 891 | Returns: |
| 896 | 892 | - 0 if OK, or a negative error number if the request fails |
| 897 | 893 | - query, message received |
| 898 | - - query_size, size in bytes of the message */ | |
| 899 | -int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size) | |
| 894 | + - query_length, length in bytes of the message */ | |
| 895 | +int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length) | |
| 900 | 896 | { |
| 901 | 897 | int ret; |
| 902 | 898 | |
| 903 | - ret = receive_msg(mb_param, MSG_SIZE_UNDEFINED, query, query_size); | |
| 899 | + ret = receive_msg(mb_param, MSG_LENGTH_UNDEFINED, query, query_length); | |
| 904 | 900 | if (ret == 0) { |
| 905 | - ret = check_crc16(mb_param, query, *query_size); | |
| 901 | + ret = check_crc16(mb_param, query, *query_length); | |
| 906 | 902 | } |
| 907 | 903 | |
| 908 | 904 | return ret; |
| ... | ... | @@ -912,17 +908,17 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size) |
| 912 | 908 | static int read_io_status(modbus_param_t *mb_param, int slave, int function, |
| 913 | 909 | int start_addr, int count, uint8_t *data_dest) |
| 914 | 910 | { |
| 915 | - int query_size; | |
| 911 | + int query_length; | |
| 916 | 912 | int query_ret; |
| 917 | 913 | int response_ret; |
| 918 | 914 | |
| 919 | - uint8_t query[MIN_QUERY_SIZE]; | |
| 920 | - uint8_t response[MAX_PACKET_SIZE]; | |
| 915 | + uint8_t query[MIN_QUERY_LENGTH]; | |
| 916 | + uint8_t response[MAX_MESSAGE_LENGTH]; | |
| 921 | 917 | |
| 922 | - query_size = build_query_basis(mb_param, slave, function, | |
| 923 | - start_addr, count, query); | |
| 918 | + query_length = build_query_basis(mb_param, slave, function, | |
| 919 | + start_addr, count, query); | |
| 924 | 920 | |
| 925 | - query_ret = modbus_send(mb_param, query, query_size); | |
| 921 | + query_ret = modbus_send(mb_param, query, query_length); | |
| 926 | 922 | if (query_ret > 0) { |
| 927 | 923 | int i, temp, bit; |
| 928 | 924 | int pos = 0; |
| ... | ... | @@ -988,15 +984,15 @@ int read_input_status(modbus_param_t *mb_param, int slave, int start_addr, |
| 988 | 984 | static int read_registers(modbus_param_t *mb_param, int slave, int function, |
| 989 | 985 | int start_addr, int count, uint16_t *data_dest) |
| 990 | 986 | { |
| 991 | - int query_size; | |
| 987 | + int query_length; | |
| 992 | 988 | int status; |
| 993 | 989 | int query_ret; |
| 994 | - uint8_t query[MIN_QUERY_SIZE]; | |
| 990 | + uint8_t query[MIN_QUERY_LENGTH]; | |
| 995 | 991 | |
| 996 | - query_size = build_query_basis(mb_param, slave, function, | |
| 997 | - start_addr, count, query); | |
| 992 | + query_length = build_query_basis(mb_param, slave, function, | |
| 993 | + start_addr, count, query); | |
| 998 | 994 | |
| 999 | - query_ret = modbus_send(mb_param, query, query_size); | |
| 995 | + query_ret = modbus_send(mb_param, query, query_length); | |
| 1000 | 996 | if (query_ret > 0) |
| 1001 | 997 | status = read_reg_response(mb_param, data_dest, query); |
| 1002 | 998 | else |
| ... | ... | @@ -1045,7 +1041,7 @@ int read_input_registers(modbus_param_t *mb_param, int slave, |
| 1045 | 1041 | static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest, |
| 1046 | 1042 | uint8_t *query) |
| 1047 | 1043 | { |
| 1048 | - uint8_t response[MAX_PACKET_SIZE]; | |
| 1044 | + uint8_t response[MAX_MESSAGE_LENGTH]; | |
| 1049 | 1045 | int response_ret; |
| 1050 | 1046 | int offset; |
| 1051 | 1047 | int i; |
| ... | ... | @@ -1068,7 +1064,7 @@ static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest, |
| 1068 | 1064 | static int preset_response(modbus_param_t *mb_param, uint8_t *query) |
| 1069 | 1065 | { |
| 1070 | 1066 | int ret; |
| 1071 | - uint8_t response[MAX_PACKET_SIZE]; | |
| 1067 | + uint8_t response[MAX_MESSAGE_LENGTH]; | |
| 1072 | 1068 | |
| 1073 | 1069 | ret = modbus_check_response(mb_param, query, response); |
| 1074 | 1070 | |
| ... | ... | @@ -1080,14 +1076,14 @@ static int set_single(modbus_param_t *mb_param, int slave, int function, |
| 1080 | 1076 | int addr, int value) |
| 1081 | 1077 | { |
| 1082 | 1078 | int status; |
| 1083 | - int query_size; | |
| 1079 | + int query_length; | |
| 1084 | 1080 | int query_ret; |
| 1085 | - uint8_t query[MAX_PACKET_SIZE]; | |
| 1081 | + uint8_t query[MAX_MESSAGE_LENGTH]; | |
| 1086 | 1082 | |
| 1087 | - query_size = build_query_basis(mb_param, slave, function, | |
| 1088 | - addr, value, query); | |
| 1083 | + query_length = build_query_basis(mb_param, slave, function, | |
| 1084 | + addr, value, query); | |
| 1089 | 1085 | |
| 1090 | - query_ret = modbus_send(mb_param, query, query_size); | |
| 1086 | + query_ret = modbus_send(mb_param, query, query_length); | |
| 1091 | 1087 | if (query_ret > 0) |
| 1092 | 1088 | status = preset_response(mb_param, query); |
| 1093 | 1089 | else |
| ... | ... | @@ -1129,42 +1125,42 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, |
| 1129 | 1125 | { |
| 1130 | 1126 | int i; |
| 1131 | 1127 | int byte_count; |
| 1132 | - int query_size; | |
| 1128 | + int query_length; | |
| 1133 | 1129 | int coil_check = 0; |
| 1134 | 1130 | int status; |
| 1135 | 1131 | int query_ret; |
| 1136 | 1132 | int pos = 0; |
| 1137 | 1133 | |
| 1138 | - uint8_t query[MAX_PACKET_SIZE]; | |
| 1134 | + uint8_t query[MAX_MESSAGE_LENGTH]; | |
| 1139 | 1135 | |
| 1140 | 1136 | if (nb_points > MAX_WRITE_COILS) { |
| 1141 | 1137 | printf("WARNING Writing to too many coils\n"); |
| 1142 | 1138 | nb_points = MAX_WRITE_COILS; |
| 1143 | 1139 | } |
| 1144 | 1140 | |
| 1145 | - query_size = build_query_basis(mb_param, slave, FC_FORCE_MULTIPLE_COILS, | |
| 1146 | - start_addr, nb_points, query); | |
| 1141 | + query_length = build_query_basis(mb_param, slave, FC_FORCE_MULTIPLE_COILS, | |
| 1142 | + start_addr, nb_points, query); | |
| 1147 | 1143 | byte_count = (nb_points / 8) + ((nb_points % 8) ? 1 : 0); |
| 1148 | - query[query_size++] = byte_count; | |
| 1144 | + query[query_length++] = byte_count; | |
| 1149 | 1145 | |
| 1150 | 1146 | for (i = 0; i < byte_count; i++) { |
| 1151 | 1147 | int bit; |
| 1152 | 1148 | |
| 1153 | 1149 | bit = 0x01; |
| 1154 | - query[query_size] = 0; | |
| 1150 | + query[query_length] = 0; | |
| 1155 | 1151 | |
| 1156 | 1152 | while ((bit & 0xFF) && (coil_check++ < nb_points)) { |
| 1157 | 1153 | if (data_src[pos++]) |
| 1158 | - query[query_size] |= bit; | |
| 1154 | + query[query_length] |= bit; | |
| 1159 | 1155 | else |
| 1160 | - query[query_size] &=~ bit; | |
| 1156 | + query[query_length] &=~ bit; | |
| 1161 | 1157 | |
| 1162 | 1158 | bit = bit << 1; |
| 1163 | 1159 | } |
| 1164 | - query_size++; | |
| 1160 | + query_length++; | |
| 1165 | 1161 | } |
| 1166 | 1162 | |
| 1167 | - query_ret = modbus_send(mb_param, query, query_size); | |
| 1163 | + query_ret = modbus_send(mb_param, query, query_length); | |
| 1168 | 1164 | if (query_ret > 0) |
| 1169 | 1165 | status = preset_response(mb_param, query); |
| 1170 | 1166 | else |
| ... | ... | @@ -1178,30 +1174,30 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
| 1178 | 1174 | int start_addr, int nb_points, uint16_t *data_src) |
| 1179 | 1175 | { |
| 1180 | 1176 | int i; |
| 1181 | - int query_size; | |
| 1177 | + int query_length; | |
| 1182 | 1178 | int byte_count; |
| 1183 | 1179 | int status; |
| 1184 | 1180 | int query_ret; |
| 1185 | 1181 | |
| 1186 | - uint8_t query[MAX_PACKET_SIZE]; | |
| 1182 | + uint8_t query[MAX_MESSAGE_LENGTH]; | |
| 1187 | 1183 | |
| 1188 | 1184 | if (nb_points > MAX_WRITE_REGS) { |
| 1189 | 1185 | printf("WARNING Trying to write to too many registers\n"); |
| 1190 | 1186 | nb_points = MAX_WRITE_REGS; |
| 1191 | 1187 | } |
| 1192 | 1188 | |
| 1193 | - query_size = build_query_basis(mb_param, slave, | |
| 1194 | - FC_PRESET_MULTIPLE_REGISTERS, | |
| 1195 | - start_addr, nb_points, query); | |
| 1189 | + query_length = build_query_basis(mb_param, slave, | |
| 1190 | + FC_PRESET_MULTIPLE_REGISTERS, | |
| 1191 | + start_addr, nb_points, query); | |
| 1196 | 1192 | byte_count = nb_points * 2; |
| 1197 | - query[query_size++] = byte_count; | |
| 1193 | + query[query_length++] = byte_count; | |
| 1198 | 1194 | |
| 1199 | 1195 | for (i = 0; i < nb_points; i++) { |
| 1200 | - query[query_size++] = data_src[i] >> 8; | |
| 1201 | - query[query_size++] = data_src[i] & 0x00FF; | |
| 1196 | + query[query_length++] = data_src[i] >> 8; | |
| 1197 | + query[query_length++] = data_src[i] & 0x00FF; | |
| 1202 | 1198 | } |
| 1203 | 1199 | |
| 1204 | - query_ret = modbus_send(mb_param, query, query_size); | |
| 1200 | + query_ret = modbus_send(mb_param, query, query_length); | |
| 1205 | 1201 | if (query_ret > 0) |
| 1206 | 1202 | status = preset_response(mb_param, query); |
| 1207 | 1203 | else |
| ... | ... | @@ -1214,20 +1210,20 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
| 1214 | 1210 | int report_slave_id(modbus_param_t *mb_param, int slave, |
| 1215 | 1211 | uint8_t *data_dest) |
| 1216 | 1212 | { |
| 1217 | - int query_size; | |
| 1213 | + int query_length; | |
| 1218 | 1214 | int query_ret; |
| 1219 | 1215 | int response_ret; |
| 1220 | 1216 | |
| 1221 | - uint8_t query[MIN_QUERY_SIZE]; | |
| 1222 | - uint8_t response[MAX_PACKET_SIZE]; | |
| 1217 | + uint8_t query[MIN_QUERY_LENGTH]; | |
| 1218 | + uint8_t response[MAX_MESSAGE_LENGTH]; | |
| 1223 | 1219 | |
| 1224 | - query_size = build_query_basis(mb_param, slave, FC_REPORT_SLAVE_ID, | |
| 1225 | - 0, 0, query); | |
| 1220 | + query_length = build_query_basis(mb_param, slave, FC_REPORT_SLAVE_ID, | |
| 1221 | + 0, 0, query); | |
| 1226 | 1222 | |
| 1227 | 1223 | /* start_addr and count are not used */ |
| 1228 | - query_size -= 4; | |
| 1224 | + query_length -= 4; | |
| 1229 | 1225 | |
| 1230 | - query_ret = modbus_send(mb_param, query, query_size); | |
| 1226 | + query_ret = modbus_send(mb_param, query, query_length); | |
| 1231 | 1227 | if (query_ret > 0) { |
| 1232 | 1228 | int i; |
| 1233 | 1229 | int offset; |
| ... | ... | @@ -1269,7 +1265,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, |
| 1269 | 1265 | mb_param->stop_bit = stop_bit; |
| 1270 | 1266 | mb_param->type_com = RTU; |
| 1271 | 1267 | mb_param->header_length = HEADER_LENGTH_RTU; |
| 1272 | - mb_param->checksum_size = CHECKSUM_SIZE_RTU; | |
| 1268 | + mb_param->checksum_length = CHECKSUM_LENGTH_RTU; | |
| 1273 | 1269 | } |
| 1274 | 1270 | |
| 1275 | 1271 | /* Initializes the modbus_param_t structure for TCP. |
| ... | ... | @@ -1288,7 +1284,7 @@ void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port) |
| 1288 | 1284 | mb_param->port = port; |
| 1289 | 1285 | mb_param->type_com = TCP; |
| 1290 | 1286 | mb_param->header_length = HEADER_LENGTH_TCP; |
| 1291 | - mb_param->checksum_size = CHECKSUM_SIZE_TCP; | |
| 1287 | + mb_param->checksum_length = CHECKSUM_LENGTH_TCP; | |
| 1292 | 1288 | mb_param->error_handling = FLUSH_OR_RECONNECT_ON_ERROR; |
| 1293 | 1289 | } |
| 1294 | 1290 | |
| ... | ... | @@ -1321,7 +1317,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1321 | 1317 | |
| 1322 | 1318 | if (mb_param->debug) { |
| 1323 | 1319 | printf("Opening %s at %d bauds (%s)\n", |
| 1324 | - mb_param->device, mb_param->baud_i, mb_param->parity); | |
| 1320 | + mb_param->device, mb_param->baud_i, mb_param->parity); | |
| 1325 | 1321 | } |
| 1326 | 1322 | |
| 1327 | 1323 | /* The O_NOCTTY flag tells UNIX that this program doesn't want |
| ... | ... | @@ -1335,7 +1331,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1335 | 1331 | if (mb_param->fd < 0) { |
| 1336 | 1332 | perror("open"); |
| 1337 | 1333 | printf("ERROR Opening device %s (no : %d)\n", |
| 1338 | - mb_param->device, errno); | |
| 1334 | + mb_param->device, errno); | |
| 1339 | 1335 | return -1; |
| 1340 | 1336 | } |
| 1341 | 1337 | |
| ... | ... | @@ -1384,7 +1380,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1384 | 1380 | default: |
| 1385 | 1381 | baud_rate = B9600; |
| 1386 | 1382 | printf("WARNING Unknown baud rate %d for %s (B9600 used)\n", |
| 1387 | - mb_param->baud_i, mb_param->device); | |
| 1383 | + mb_param->baud_i, mb_param->device); | |
| 1388 | 1384 | } |
| 1389 | 1385 | |
| 1390 | 1386 | /* Set the baud rate */ |
| ... | ... | @@ -1456,7 +1452,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1456 | 1452 | ECHOK Echo NL after kill character |
| 1457 | 1453 | ECHONL Echo NL |
| 1458 | 1454 | NOFLSH Disable flushing of input buffers after |
| 1459 | - interrupt or quit characters | |
| 1455 | + interrupt or quit characters | |
| 1460 | 1456 | IEXTEN Enable extended functions |
| 1461 | 1457 | ECHOCTL Echo control characters as ^char and delete as ~? |
| 1462 | 1458 | ECHOPRT Echo erased character as character erased |
| ... | ... | @@ -1667,7 +1663,7 @@ void modbus_set_debug(modbus_param_t *mb_param, int boolean) |
| 1667 | 1663 | holding registers. The pointers are stored in modbus_mapping structure. |
| 1668 | 1664 | |
| 1669 | 1665 | Returns: TRUE if ok, FALSE on failure |
| 1670 | - */ | |
| 1666 | +*/ | |
| 1671 | 1667 | int modbus_mapping_new(modbus_mapping_t *mb_mapping, |
| 1672 | 1668 | int nb_coil_status, int nb_input_status, |
| 1673 | 1669 | int nb_holding_registers, int nb_input_registers) | ... | ... |
modbus/modbus.h
| ... | ... | @@ -26,22 +26,21 @@ |
| 26 | 26 | |
| 27 | 27 | #define MODBUS_TCP_DEFAULT_PORT 502 |
| 28 | 28 | |
| 29 | -#define HEADER_LENGTH_RTU 0 | |
| 30 | -#define PRESET_QUERY_SIZE_RTU 6 | |
| 31 | -#define PRESET_RESPONSE_SIZE_RTU 2 | |
| 29 | +#define HEADER_LENGTH_RTU 0 | |
| 30 | +#define PRESET_QUERY_LENGTH_RTU 6 | |
| 31 | +#define PRESET_RESPONSE_LENGTH_RTU 2 | |
| 32 | 32 | |
| 33 | -#define HEADER_LENGTH_TCP 6 | |
| 34 | -#define PRESET_QUERY_SIZE_TCP 12 | |
| 35 | -#define PRESET_RESPONSE_SIZE_TCP 8 | |
| 33 | +#define HEADER_LENGTH_TCP 6 | |
| 34 | +#define PRESET_QUERY_LENGTH_TCP 12 | |
| 35 | +#define PRESET_RESPONSE_LENGTH_TCP 8 | |
| 36 | 36 | |
| 37 | -#define CHECKSUM_SIZE_RTU 2 | |
| 38 | -#define CHECKSUM_SIZE_TCP 0 | |
| 37 | +#define CHECKSUM_LENGTH_RTU 2 | |
| 38 | +#define CHECKSUM_LENGTH_TCP 0 | |
| 39 | 39 | |
| 40 | 40 | /* 8 + HEADER_LENGTH_TCP */ |
| 41 | -#define MIN_QUERY_SIZE 14 | |
| 41 | +#define MIN_QUERY_LENGTH 14 | |
| 42 | 42 | |
| 43 | -/* MIN_RESPONSE_LENGTH + MAX(MAX*) */ | |
| 44 | -#define MAX_PACKET_SIZE 261 | |
| 43 | +#define MAX_MESSAGE_LENGTH 256 | |
| 45 | 44 | |
| 46 | 45 | #define MAX_READ_STATUS 800 |
| 47 | 46 | #define MAX_READ_HOLD_REGS 100 |
| ... | ... | @@ -49,7 +48,7 @@ |
| 49 | 48 | #define MAX_WRITE_COILS 800 |
| 50 | 49 | #define MAX_WRITE_REGS 100 |
| 51 | 50 | |
| 52 | -#define REPORT_SLAVE_ID_SIZE 75 | |
| 51 | +#define REPORT_SLAVE_ID_LENGTH 75 | |
| 53 | 52 | |
| 54 | 53 | /* Time out between trames in microsecond */ |
| 55 | 54 | #define TIME_OUT_BEGIN_OF_TRAME 500000 |
| ... | ... | @@ -107,7 +106,7 @@ |
| 107 | 106 | #define CONNECTION_CLOSED -0x12 |
| 108 | 107 | |
| 109 | 108 | /* Internal using */ |
| 110 | -#define MSG_SIZE_UNDEFINED -1 | |
| 109 | +#define MSG_LENGTH_UNDEFINED -1 | |
| 111 | 110 | |
| 112 | 111 | typedef enum { RTU, TCP } type_com_t; |
| 113 | 112 | typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t; |
| ... | ... | @@ -149,8 +148,8 @@ typedef struct { |
| 149 | 148 | uint16_t port; |
| 150 | 149 | /* Header length used for offset */ |
| 151 | 150 | int header_length; |
| 152 | - /* Checksum size RTU = 2 and TCP = 0 */ | |
| 153 | - int checksum_size; | |
| 151 | + /* Checksum length RTU = 2 and TCP = 0 */ | |
| 152 | + int checksum_length; | |
| 154 | 153 | /* In error_treat with TCP, do a reconnect or just dump the error */ |
| 155 | 154 | error_handling_t error_handling; |
| 156 | 155 | } modbus_param_t; |
| ... | ... | @@ -274,7 +273,7 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping); |
| 274 | 273 | int modbus_init_listen_tcp(modbus_param_t *mb_param); |
| 275 | 274 | |
| 276 | 275 | /* FIXME */ |
| 277 | -int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size); | |
| 276 | +int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length); | |
| 278 | 277 | |
| 279 | 278 | /* Manages the received query. |
| 280 | 279 | Analyses the query and constructs a response. |
| ... | ... | @@ -283,7 +282,7 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size); |
| 283 | 282 | accordingly. |
| 284 | 283 | */ |
| 285 | 284 | void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 286 | - int query_size, modbus_mapping_t *mb_mapping); | |
| 285 | + int query_length, modbus_mapping_t *mb_mapping); | |
| 287 | 286 | |
| 288 | 287 | /* Not implemented : |
| 289 | 288 | - read_exception_status() | ... | ... |
tests/unit-test-slave.c