Commit 23ac32815fda100704f51f0062fa2e5b53bd7157

Authored by Stéphane Raimbault
1 parent f5cd5962

- Rename MAX_PACKET_SIZE to MAX_MESSAGE_LENGTH

- Reduce the max message length to 256 (PI_M300)
- s/size/length/
@@ -6,11 +6,7 @@ Features @@ -6,11 +6,7 @@ Features
6 6
7 Cleanups 7 Cleanups
8 * see the max length of a message and define size var accordingly (uint8_t) 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 * t_id in param_msqg 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 Documentation 11 Documentation
16 * README with a example to test the library 12 * README with a example to test the library
modbus/modbus.c
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
27 27
28 Documentation: 28 Documentation:
29 http://www.easysw.com/~mike/serial/serial.html 29 http://www.easysw.com/~mike/serial/serial.html
  30 + http://copyleft.free.fr/wordpress/index.php/libmodbus/
30 */ 31 */
31 32
32 #include <stdio.h> 33 #include <stdio.h>
@@ -155,11 +156,11 @@ static void error_treat(int code, const char *string, modbus_param_t *mb_param) @@ -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 int offset; 164 int offset;
164 165
165 offset = mb_param->header_length; 166 offset = mb_param->header_length;
@@ -169,29 +170,27 @@ static unsigned int compute_response_size(modbus_param_t *mb_param, @@ -169,29 +170,27 @@ static unsigned int compute_response_size(modbus_param_t *mb_param,
169 case FC_READ_INPUT_STATUS: { 170 case FC_READ_INPUT_STATUS: {
170 /* Header + nb values (code from force_multiple_coils) */ 171 /* Header + nb values (code from force_multiple_coils) */
171 int nb_points = (query[offset + 4] << 8) | query[offset + 5]; 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 break; 175 break;
176 case FC_READ_HOLDING_REGISTERS: 176 case FC_READ_HOLDING_REGISTERS:
177 case FC_READ_INPUT_REGISTERS: 177 case FC_READ_INPUT_REGISTERS:
178 /* Header + 2 * nb values */ 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 break; 180 break;
182 case FC_READ_EXCEPTION_STATUS: 181 case FC_READ_EXCEPTION_STATUS:
183 - response_size_computed = 4; 182 + resp_length = 4;
184 break; 183 break;
185 default: 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 static int build_query_basis_rtu(uint8_t slave, uint8_t function, 194 static int build_query_basis_rtu(uint8_t slave, uint8_t function,
196 uint16_t start_addr, uint16_t count, 195 uint16_t start_addr, uint16_t count,
197 uint8_t *query) 196 uint8_t *query)
@@ -203,10 +202,10 @@ static int build_query_basis_rtu(uint8_t slave, uint8_t function, @@ -203,10 +202,10 @@ static int build_query_basis_rtu(uint8_t slave, uint8_t function,
203 query[4] = count >> 8; 202 query[4] = count >> 8;
204 query[5] = count & 0x00ff; 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 static int build_query_basis_tcp(uint8_t slave, uint8_t function, 209 static int build_query_basis_tcp(uint8_t slave, uint8_t function,
211 uint16_t start_addr, uint16_t count, 210 uint16_t start_addr, uint16_t count,
212 uint8_t *query) 211 uint8_t *query)
@@ -234,7 +233,7 @@ static int build_query_basis_tcp(uint8_t slave, uint8_t function, @@ -234,7 +233,7 @@ static int build_query_basis_tcp(uint8_t slave, uint8_t function,
234 query[10] = count >> 8; 233 query[10] = count >> 8;
235 query[11] = count & 0x00ff; 234 query[11] = count & 0x00ff;
236 235
237 - return PRESET_QUERY_SIZE_TCP; 236 + return PRESET_QUERY_LENGTH_TCP;
238 } 237 }
239 238
240 static int build_query_basis(modbus_param_t *mb_param, uint8_t slave, 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,14 +248,16 @@ static int build_query_basis(modbus_param_t *mb_param, uint8_t slave,
249 count, query); 248 count, query);
250 } 249 }
251 250
  251 +/* Builds a RTU response header */
252 static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *response) 252 static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *response)
253 { 253 {
254 response[0] = slave; 254 response[0] = slave;
255 response[1] = function; 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 static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *response) 261 static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *response)
261 { 262 {
262 static uint16_t t_id = 0; 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,7 +279,7 @@ static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *re
278 response[6] = slave; 279 response[6] = slave;
279 response[7] = function; 280 response[7] = function;
280 281
281 - return PRESET_RESPONSE_SIZE_TCP; 282 + return PRESET_RESPONSE_LENGTH_TCP;
282 } 283 }
283 284
284 static int build_response_basis(modbus_param_t *mb_param, uint8_t slave, 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,12 +292,12 @@ static int build_response_basis(modbus_param_t *mb_param, uint8_t slave,
291 } 292 }
292 293
293 /* Sets the length of TCP message in the message (query and response) */ 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 uint16_t mbap_length; 297 uint16_t mbap_length;
297 298
298 /* Substract MBAP header length */ 299 /* Substract MBAP header length */
299 - mbap_length = packet_size - 6; 300 + mbap_length = packet_length - 6;
300 301
301 packet[4] = mbap_length >> 8; 302 packet[4] = mbap_length >> 8;
302 packet[5] = mbap_length & 0x00FF; 303 packet[5] = mbap_length & 0x00FF;
@@ -322,7 +323,7 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length) @@ -322,7 +323,7 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length)
322 /* If CRC is correct returns 0 else returns INVALID_CRC */ 323 /* If CRC is correct returns 0 else returns INVALID_CRC */
323 int check_crc16(modbus_param_t *mb_param, 324 int check_crc16(modbus_param_t *mb_param,
324 uint8_t *msg, 325 uint8_t *msg,
325 - const int msg_size) 326 + const int msg_length)
326 { 327 {
327 int ret; 328 int ret;
328 329
@@ -330,8 +331,8 @@ int check_crc16(modbus_param_t *mb_param, @@ -330,8 +331,8 @@ int check_crc16(modbus_param_t *mb_param,
330 uint16_t crc_calc; 331 uint16_t crc_calc;
331 uint16_t crc_received; 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 /* Check CRC of msg */ 337 /* Check CRC of msg */
337 if (crc_calc == crc_received) { 338 if (crc_calc == crc_received) {
@@ -353,36 +354,36 @@ int check_crc16(modbus_param_t *mb_param, @@ -353,36 +354,36 @@ int check_crc16(modbus_param_t *mb_param,
353 354
354 /* Sends a query/response over a serial or a TCP communication */ 355 /* Sends a query/response over a serial or a TCP communication */
355 static int modbus_send(modbus_param_t *mb_param, uint8_t *query, 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 int ret; 359 int ret;
359 uint16_t s_crc; 360 uint16_t s_crc;
360 int i; 361 int i;
361 362
362 if (mb_param->type_com == RTU) { 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 } else { 367 } else {
367 - set_packet_length_tcp(query, query_size); 368 + set_packet_length_tcp(query, query_length);
368 } 369 }
369 370
370 if (mb_param->debug) { 371 if (mb_param->debug) {
371 printf("\n"); 372 printf("\n");
372 - for (i = 0; i < query_size; i++) 373 + for (i = 0; i < query_length; i++)
373 printf("[%.2X]", query[i]); 374 printf("[%.2X]", query[i]);
374 375
375 printf("\n"); 376 printf("\n");
376 } 377 }
377 378
378 if (mb_param->type_com == RTU) 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 else 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 /* Return the number of bytes written (0 to n) 384 /* Return the number of bytes written (0 to n)
384 or PORT_SOCKET_FAILURE on error */ 385 or PORT_SOCKET_FAILURE on error */
385 - if ((ret == -1) || (ret != query_size)) { 386 + if ((ret == -1) || (ret != query_length)) {
386 ret = PORT_SOCKET_FAILURE; 387 ret = PORT_SOCKET_FAILURE;
387 error_treat(ret, "Write port/socket failure", mb_param); 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,8 +391,8 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
390 return ret; 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 uint8_t byte; 397 uint8_t byte;
397 398
@@ -406,13 +407,11 @@ static uint8_t compute_query_size_header(uint8_t function) @@ -406,13 +407,11 @@ static uint8_t compute_query_size_header(uint8_t function)
406 else 407 else
407 byte = 0; 408 byte = 0;
408 409
409 -// printf("compute_query_size_header FC %d, B %d\n", function, byte);  
410 -  
411 return byte; 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 uint8_t function = msg[mb_param->header_length + 1]; 416 uint8_t function = msg[mb_param->header_length + 1];
418 uint8_t byte; 417 uint8_t byte;
@@ -423,76 +422,75 @@ static uint8_t compute_query_size_data(modbus_param_t *mb_param, uint8_t *msg) @@ -423,76 +422,75 @@ static uint8_t compute_query_size_data(modbus_param_t *mb_param, uint8_t *msg)
423 else 422 else
424 byte = 0; 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 return byte; 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 /* Monitors for the reply from the modbus slave or to receive query 450 /* Monitors for the reply from the modbus slave or to receive query
453 from a modbus master. 451 from a modbus master.
454 This function blocks for timeout seconds if there is no reply. 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 Returns: 456 Returns:
459 - 0: OK, <0: error 457 - 0: OK, <0: error
460 - - msg_size: number of characters received. */ 458 + - msg_length: number of characters received. */
461 int receive_msg(modbus_param_t *mb_param, 459 int receive_msg(modbus_param_t *mb_param,
462 - int msg_size_computed, 460 + int msg_length_computed,
463 uint8_t *msg, 461 uint8_t *msg,
464 - int *msg_size) 462 + int *msg_length)
465 { 463 {
466 int select_ret; 464 int select_ret;
467 int read_ret; 465 int read_ret;
468 fd_set rfds; 466 fd_set rfds;
469 struct timeval tv; 467 struct timeval tv;
470 - int size_to_read; 468 + int length_to_read;
471 uint8_t *p_msg; 469 uint8_t *p_msg;
472 enum { FUNCTION, BYTE, COMPLETE }; 470 enum { FUNCTION, BYTE, COMPLETE };
473 int state; 471 int state;
474 472
475 if (mb_param->debug) { 473 if (mb_param->debug) {
476 - if (msg_size_computed == MSG_SIZE_UNDEFINED) 474 + if (msg_length_computed == MSG_LENGTH_UNDEFINED)
477 printf("Waiting for a message...\n"); 475 printf("Waiting for a message...\n");
478 else 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 /* Add a file descriptor to the set */ 480 /* Add a file descriptor to the set */
483 FD_ZERO(&rfds); 481 FD_ZERO(&rfds);
484 FD_SET(mb_param->fd, &rfds); 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 /* Wait for a message */ 485 /* Wait for a message */
488 tv.tv_sec = 60; 486 tv.tv_sec = 60;
489 tv.tv_usec = 0; 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 * we need to analyse the message step by step. 490 * we need to analyse the message step by step.
493 * At the first step, we want to reach the function 491 * At the first step, we want to reach the function
494 * code because all packets have this information. */ 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 state = FUNCTION; 494 state = FUNCTION;
497 } else { 495 } else {
498 tv.tv_sec = 0; 496 tv.tv_sec = 0;
@@ -500,20 +498,20 @@ int receive_msg(modbus_param_t *mb_param, @@ -500,20 +498,20 @@ int receive_msg(modbus_param_t *mb_param,
500 state = COMPLETE; 498 state = COMPLETE;
501 } 499 }
502 500
503 - size_to_read = msg_size_computed; 501 + length_to_read = msg_length_computed;
504 502
505 select_ret = 0; 503 select_ret = 0;
506 WAIT_DATA(); 504 WAIT_DATA();
507 505
508 /* Read the msg */ 506 /* Read the msg */
509 - (*msg_size) = 0; 507 + (*msg_length) = 0;
510 p_msg = msg; 508 p_msg = msg;
511 509
512 while (select_ret) { 510 while (select_ret) {
513 if (mb_param->type_com == RTU) 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 else 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 if (read_ret == -1) { 516 if (read_ret == -1) {
519 error_treat(PORT_SOCKET_FAILURE, "Read port/socket failure", mb_param); 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,8 +522,8 @@ int receive_msg(modbus_param_t *mb_param,
524 } 522 }
525 523
526 /* Sums bytes received */ 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 error_treat(TOO_MANY_DATAS, "Too many datas", mb_param); 527 error_treat(TOO_MANY_DATAS, "Too many datas", mb_param);
530 return TOO_MANY_DATAS; 528 return TOO_MANY_DATAS;
531 } 529 }
@@ -537,34 +535,34 @@ int receive_msg(modbus_param_t *mb_param, @@ -537,34 +535,34 @@ int receive_msg(modbus_param_t *mb_param,
537 printf("<%.2X>", p_msg[i]); 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 /* Message incomplete */ 539 /* Message incomplete */
542 - size_to_read = msg_size_computed - (*msg_size); 540 + length_to_read = msg_length_computed - (*msg_length);
543 } else { 541 } else {
544 switch (state) { 542 switch (state) {
545 case FUNCTION: 543 case FUNCTION:
546 /* Function code position */ 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 state = BYTE; 547 state = BYTE;
550 break; 548 break;
551 case BYTE: 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 state = COMPLETE; 552 state = COMPLETE;
555 break; 553 break;
556 case COMPLETE: 554 case COMPLETE:
557 - size_to_read = 0; 555 + length_to_read = 0;
558 break; 556 break;
559 } 557 }
560 } 558 }
561 if (mb_param->debug) 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 /* Moves the pointer to receive other datas */ 562 /* Moves the pointer to receive other datas */
565 p_msg = &(p_msg[read_ret]); 563 p_msg = &(p_msg[read_ret]);
566 564
567 - if (size_to_read > 0) { 565 + if (length_to_read > 0) {
568 /* If no character at the buffer wait 566 /* If no character at the buffer wait
569 TIME_OUT_END_OF_TRAME before to generate an error. */ 567 TIME_OUT_END_OF_TRAME before to generate an error. */
570 tv.tv_sec = 0; 568 tv.tv_sec = 0;
@@ -597,16 +595,16 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -597,16 +595,16 @@ static int modbus_check_response(modbus_param_t *mb_param,
597 uint8_t *query, 595 uint8_t *query,
598 uint8_t *response) 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 int offset = mb_param->header_length; 600 int offset = mb_param->header_length;
603 int ret; 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 if (ret == 0) { 605 if (ret == 0) {
608 /* Check message */ 606 /* Check message */
609 - ret = check_crc16(mb_param, response, response_size); 607 + ret = check_crc16(mb_param, response, response_length);
610 if (ret != 0) 608 if (ret != 0)
611 return ret; 609 return ret;
612 610
@@ -615,17 +613,17 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -615,17 +613,17 @@ static int modbus_check_response(modbus_param_t *mb_param,
615 case FC_READ_COIL_STATUS: 613 case FC_READ_COIL_STATUS:
616 case FC_READ_INPUT_STATUS: 614 case FC_READ_INPUT_STATUS:
617 /* Read functions 1 value = 1 byte */ 615 /* Read functions 1 value = 1 byte */
618 - response_size = response[offset + 2]; 616 + response_length = response[offset + 2];
619 break; 617 break;
620 case FC_READ_HOLDING_REGISTERS: 618 case FC_READ_HOLDING_REGISTERS:
621 case FC_READ_INPUT_REGISTERS: 619 case FC_READ_INPUT_REGISTERS:
622 /* Read functions 1 value = 2 bytes */ 620 /* Read functions 1 value = 2 bytes */
623 - response_size = response[offset + 2] / 2; 621 + response_length = response[offset + 2] / 2;
624 break; 622 break;
625 case FC_FORCE_MULTIPLE_COILS: 623 case FC_FORCE_MULTIPLE_COILS:
626 case FC_PRESET_MULTIPLE_REGISTERS: 624 case FC_PRESET_MULTIPLE_REGISTERS:
627 /* N Write functions */ 625 /* N Write functions */
628 - response_size = response[offset + 4] << 8 | 626 + response_length = response[offset + 4] << 8 |
629 response[offset + 5]; 627 response[offset + 5];
630 break; 628 break;
631 case FC_REPORT_SLAVE_ID: 629 case FC_REPORT_SLAVE_ID:
@@ -633,16 +631,16 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -633,16 +631,16 @@ static int modbus_check_response(modbus_param_t *mb_param,
633 break; 631 break;
634 default: 632 default:
635 /* 1 Write functions & others */ 633 /* 1 Write functions & others */
636 - response_size = 1; 634 + response_length = 1;
637 } 635 }
638 636
639 } else if (ret == COMM_TIME_OUT && 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 /* Optimisation allowed because exception response is 639 /* Optimisation allowed because exception response is
642 the smallest trame in modbus protocol (3) so always 640 the smallest trame in modbus protocol (3) so always
643 raise an timeout error */ 641 raise an timeout error */
644 /* CRC */ 642 /* CRC */
645 - ret = check_crc16(mb_param, response, response_size); 643 + ret = check_crc16(mb_param, response, response_length);
646 if (ret != 0) 644 if (ret != 0)
647 return ret; 645 return ret;
648 646
@@ -677,7 +675,7 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -677,7 +675,7 @@ static int modbus_check_response(modbus_param_t *mb_param,
677 return ret; 675 return ret;
678 } 676 }
679 677
680 - return response_size; 678 + return response_length;
681 } 679 }
682 680
683 static int response_io_status(uint16_t address, uint16_t count, 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,14 +708,14 @@ static int response_exception(modbus_param_t *mb_param, int slave,
710 int function, int exception_code, 708 int function, int exception_code,
711 uint8_t *response) 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 /* Positive exception code */ 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 /* Manages the received query. 721 /* Manages the received query.
@@ -726,13 +724,13 @@ static int response_exception(modbus_param_t *mb_param, int slave, @@ -726,13 +724,13 @@ static int response_exception(modbus_param_t *mb_param, int slave,
726 accordingly. 724 accordingly.
727 */ 725 */
728 void manage_query(modbus_param_t *mb_param, uint8_t *query, 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 int offset = mb_param->header_length; 729 int offset = mb_param->header_length;
732 int slave = query[offset]; 730 int slave = query[offset];
733 int function = query[offset+1]; 731 int function = query[offset+1];
734 uint16_t address = (query[offset+2] << 8) + query[offset+3]; 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 int resp_length = 0; 734 int resp_length = 0;
737 735
738 switch (function) { 736 switch (function) {
@@ -825,8 +823,8 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, @@ -825,8 +823,8 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
825 823
826 /* In RTU mode, the CRC is computed 824 /* In RTU mode, the CRC is computed
827 and added to the query by modbus_send */ 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 } else { 828 } else {
831 printf("Illegal data value %0X in force_single_coil request at address %0X\n", 829 printf("Illegal data value %0X in force_single_coil request at address %0X\n",
832 data, address); 830 data, address);
@@ -839,13 +837,13 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, @@ -839,13 +837,13 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
839 if (address >= mb_mapping->nb_holding_registers) { 837 if (address >= mb_mapping->nb_holding_registers) {
840 printf("Illegal data address %0X in preset_holding_register\n", address); 838 printf("Illegal data address %0X in preset_holding_register\n", address);
841 resp_length = response_exception(mb_param, slave, function, 839 resp_length = response_exception(mb_param, slave, function,
842 - ILLEGAL_DATA_ADDRESS, response); 840 + ILLEGAL_DATA_ADDRESS, response);
843 } else { 841 } else {
844 int data = (query[offset+4] << 8) + query[offset+5]; 842 int data = (query[offset+4] << 8) + query[offset+5];
845 843
846 mb_mapping->tab_holding_registers[address] = data; 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 break; 848 break;
851 case FC_FORCE_MULTIPLE_COILS: { 849 case FC_FORCE_MULTIPLE_COILS: {
@@ -857,7 +855,6 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, @@ -857,7 +855,6 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
857 resp_length = response_exception(mb_param, slave, function, 855 resp_length = response_exception(mb_param, slave, function,
858 ILLEGAL_DATA_ADDRESS, response); 856 ILLEGAL_DATA_ADDRESS, response);
859 } else { 857 } else {
860 - /* Similar to build_query_basis! */  
861 resp_length = build_response_basis(mb_param, slave, function, response); 858 resp_length = build_response_basis(mb_param, slave, function, response);
862 /* 4 to copy the coil address (2) and the quantity of coils */ 859 /* 4 to copy the coil address (2) and the quantity of coils */
863 memcpy(response + resp_length, query + resp_length, 4); 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,7 +871,6 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
874 resp_length = response_exception(mb_param, slave, function, 871 resp_length = response_exception(mb_param, slave, function,
875 ILLEGAL_DATA_ADDRESS, response); 872 ILLEGAL_DATA_ADDRESS, response);
876 } else { 873 } else {
877 - /* Similar to build_query_basis! */  
878 resp_length = build_response_basis(mb_param, slave, function, response); 874 resp_length = build_response_basis(mb_param, slave, function, response);
879 /* 4 to copy the address (2) and the no. of registers */ 875 /* 4 to copy the address (2) and the no. of registers */
880 memcpy(response + resp_length, query + resp_length, 4); 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,14 +891,14 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
895 Returns: 891 Returns:
896 - 0 if OK, or a negative error number if the request fails 892 - 0 if OK, or a negative error number if the request fails
897 - query, message received 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 int ret; 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 if (ret == 0) { 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 return ret; 904 return ret;
@@ -912,17 +908,17 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size) @@ -912,17 +908,17 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size)
912 static int read_io_status(modbus_param_t *mb_param, int slave, int function, 908 static int read_io_status(modbus_param_t *mb_param, int slave, int function,
913 int start_addr, int count, uint8_t *data_dest) 909 int start_addr, int count, uint8_t *data_dest)
914 { 910 {
915 - int query_size; 911 + int query_length;
916 int query_ret; 912 int query_ret;
917 int response_ret; 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 if (query_ret > 0) { 922 if (query_ret > 0) {
927 int i, temp, bit; 923 int i, temp, bit;
928 int pos = 0; 924 int pos = 0;
@@ -988,15 +984,15 @@ int read_input_status(modbus_param_t *mb_param, int slave, int start_addr, @@ -988,15 +984,15 @@ int read_input_status(modbus_param_t *mb_param, int slave, int start_addr,
988 static int read_registers(modbus_param_t *mb_param, int slave, int function, 984 static int read_registers(modbus_param_t *mb_param, int slave, int function,
989 int start_addr, int count, uint16_t *data_dest) 985 int start_addr, int count, uint16_t *data_dest)
990 { 986 {
991 - int query_size; 987 + int query_length;
992 int status; 988 int status;
993 int query_ret; 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 if (query_ret > 0) 996 if (query_ret > 0)
1001 status = read_reg_response(mb_param, data_dest, query); 997 status = read_reg_response(mb_param, data_dest, query);
1002 else 998 else
@@ -1045,7 +1041,7 @@ int read_input_registers(modbus_param_t *mb_param, int slave, @@ -1045,7 +1041,7 @@ int read_input_registers(modbus_param_t *mb_param, int slave,
1045 static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest, 1041 static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest,
1046 uint8_t *query) 1042 uint8_t *query)
1047 { 1043 {
1048 - uint8_t response[MAX_PACKET_SIZE]; 1044 + uint8_t response[MAX_MESSAGE_LENGTH];
1049 int response_ret; 1045 int response_ret;
1050 int offset; 1046 int offset;
1051 int i; 1047 int i;
@@ -1068,7 +1064,7 @@ static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest, @@ -1068,7 +1064,7 @@ static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest,
1068 static int preset_response(modbus_param_t *mb_param, uint8_t *query) 1064 static int preset_response(modbus_param_t *mb_param, uint8_t *query)
1069 { 1065 {
1070 int ret; 1066 int ret;
1071 - uint8_t response[MAX_PACKET_SIZE]; 1067 + uint8_t response[MAX_MESSAGE_LENGTH];
1072 1068
1073 ret = modbus_check_response(mb_param, query, response); 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,14 +1076,14 @@ static int set_single(modbus_param_t *mb_param, int slave, int function,
1080 int addr, int value) 1076 int addr, int value)
1081 { 1077 {
1082 int status; 1078 int status;
1083 - int query_size; 1079 + int query_length;
1084 int query_ret; 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 if (query_ret > 0) 1087 if (query_ret > 0)
1092 status = preset_response(mb_param, query); 1088 status = preset_response(mb_param, query);
1093 else 1089 else
@@ -1129,42 +1125,42 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, @@ -1129,42 +1125,42 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave,
1129 { 1125 {
1130 int i; 1126 int i;
1131 int byte_count; 1127 int byte_count;
1132 - int query_size; 1128 + int query_length;
1133 int coil_check = 0; 1129 int coil_check = 0;
1134 int status; 1130 int status;
1135 int query_ret; 1131 int query_ret;
1136 int pos = 0; 1132 int pos = 0;
1137 1133
1138 - uint8_t query[MAX_PACKET_SIZE]; 1134 + uint8_t query[MAX_MESSAGE_LENGTH];
1139 1135
1140 if (nb_points > MAX_WRITE_COILS) { 1136 if (nb_points > MAX_WRITE_COILS) {
1141 printf("WARNING Writing to too many coils\n"); 1137 printf("WARNING Writing to too many coils\n");
1142 nb_points = MAX_WRITE_COILS; 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 byte_count = (nb_points / 8) + ((nb_points % 8) ? 1 : 0); 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 for (i = 0; i < byte_count; i++) { 1146 for (i = 0; i < byte_count; i++) {
1151 int bit; 1147 int bit;
1152 1148
1153 bit = 0x01; 1149 bit = 0x01;
1154 - query[query_size] = 0; 1150 + query[query_length] = 0;
1155 1151
1156 while ((bit & 0xFF) && (coil_check++ < nb_points)) { 1152 while ((bit & 0xFF) && (coil_check++ < nb_points)) {
1157 if (data_src[pos++]) 1153 if (data_src[pos++])
1158 - query[query_size] |= bit; 1154 + query[query_length] |= bit;
1159 else 1155 else
1160 - query[query_size] &=~ bit; 1156 + query[query_length] &=~ bit;
1161 1157
1162 bit = bit << 1; 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 if (query_ret > 0) 1164 if (query_ret > 0)
1169 status = preset_response(mb_param, query); 1165 status = preset_response(mb_param, query);
1170 else 1166 else
@@ -1178,30 +1174,30 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, @@ -1178,30 +1174,30 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1178 int start_addr, int nb_points, uint16_t *data_src) 1174 int start_addr, int nb_points, uint16_t *data_src)
1179 { 1175 {
1180 int i; 1176 int i;
1181 - int query_size; 1177 + int query_length;
1182 int byte_count; 1178 int byte_count;
1183 int status; 1179 int status;
1184 int query_ret; 1180 int query_ret;
1185 1181
1186 - uint8_t query[MAX_PACKET_SIZE]; 1182 + uint8_t query[MAX_MESSAGE_LENGTH];
1187 1183
1188 if (nb_points > MAX_WRITE_REGS) { 1184 if (nb_points > MAX_WRITE_REGS) {
1189 printf("WARNING Trying to write to too many registers\n"); 1185 printf("WARNING Trying to write to too many registers\n");
1190 nb_points = MAX_WRITE_REGS; 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 byte_count = nb_points * 2; 1192 byte_count = nb_points * 2;
1197 - query[query_size++] = byte_count; 1193 + query[query_length++] = byte_count;
1198 1194
1199 for (i = 0; i < nb_points; i++) { 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 if (query_ret > 0) 1201 if (query_ret > 0)
1206 status = preset_response(mb_param, query); 1202 status = preset_response(mb_param, query);
1207 else 1203 else
@@ -1214,20 +1210,20 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, @@ -1214,20 +1210,20 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1214 int report_slave_id(modbus_param_t *mb_param, int slave, 1210 int report_slave_id(modbus_param_t *mb_param, int slave,
1215 uint8_t *data_dest) 1211 uint8_t *data_dest)
1216 { 1212 {
1217 - int query_size; 1213 + int query_length;
1218 int query_ret; 1214 int query_ret;
1219 int response_ret; 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 /* start_addr and count are not used */ 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 if (query_ret > 0) { 1227 if (query_ret > 0) {
1232 int i; 1228 int i;
1233 int offset; 1229 int offset;
@@ -1269,7 +1265,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, @@ -1269,7 +1265,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device,
1269 mb_param->stop_bit = stop_bit; 1265 mb_param->stop_bit = stop_bit;
1270 mb_param->type_com = RTU; 1266 mb_param->type_com = RTU;
1271 mb_param->header_length = HEADER_LENGTH_RTU; 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 /* Initializes the modbus_param_t structure for TCP. 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,7 +1284,7 @@ void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port)
1288 mb_param->port = port; 1284 mb_param->port = port;
1289 mb_param->type_com = TCP; 1285 mb_param->type_com = TCP;
1290 mb_param->header_length = HEADER_LENGTH_TCP; 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 mb_param->error_handling = FLUSH_OR_RECONNECT_ON_ERROR; 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,7 +1317,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1321 1317
1322 if (mb_param->debug) { 1318 if (mb_param->debug) {
1323 printf("Opening %s at %d bauds (%s)\n", 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 /* The O_NOCTTY flag tells UNIX that this program doesn't want 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,7 +1331,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1335 if (mb_param->fd < 0) { 1331 if (mb_param->fd < 0) {
1336 perror("open"); 1332 perror("open");
1337 printf("ERROR Opening device %s (no : %d)\n", 1333 printf("ERROR Opening device %s (no : %d)\n",
1338 - mb_param->device, errno); 1334 + mb_param->device, errno);
1339 return -1; 1335 return -1;
1340 } 1336 }
1341 1337
@@ -1384,7 +1380,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1384,7 +1380,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1384 default: 1380 default:
1385 baud_rate = B9600; 1381 baud_rate = B9600;
1386 printf("WARNING Unknown baud rate %d for %s (B9600 used)\n", 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 /* Set the baud rate */ 1386 /* Set the baud rate */
@@ -1456,7 +1452,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1456,7 +1452,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1456 ECHOK Echo NL after kill character 1452 ECHOK Echo NL after kill character
1457 ECHONL Echo NL 1453 ECHONL Echo NL
1458 NOFLSH Disable flushing of input buffers after 1454 NOFLSH Disable flushing of input buffers after
1459 - interrupt or quit characters 1455 + interrupt or quit characters
1460 IEXTEN Enable extended functions 1456 IEXTEN Enable extended functions
1461 ECHOCTL Echo control characters as ^char and delete as ~? 1457 ECHOCTL Echo control characters as ^char and delete as ~?
1462 ECHOPRT Echo erased character as character erased 1458 ECHOPRT Echo erased character as character erased
@@ -1667,7 +1663,7 @@ void modbus_set_debug(modbus_param_t *mb_param, int boolean) @@ -1667,7 +1663,7 @@ void modbus_set_debug(modbus_param_t *mb_param, int boolean)
1667 holding registers. The pointers are stored in modbus_mapping structure. 1663 holding registers. The pointers are stored in modbus_mapping structure.
1668 1664
1669 Returns: TRUE if ok, FALSE on failure 1665 Returns: TRUE if ok, FALSE on failure
1670 - */ 1666 +*/
1671 int modbus_mapping_new(modbus_mapping_t *mb_mapping, 1667 int modbus_mapping_new(modbus_mapping_t *mb_mapping,
1672 int nb_coil_status, int nb_input_status, 1668 int nb_coil_status, int nb_input_status,
1673 int nb_holding_registers, int nb_input_registers) 1669 int nb_holding_registers, int nb_input_registers)
modbus/modbus.h
@@ -26,22 +26,21 @@ @@ -26,22 +26,21 @@
26 26
27 #define MODBUS_TCP_DEFAULT_PORT 502 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 /* 8 + HEADER_LENGTH_TCP */ 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 #define MAX_READ_STATUS 800 45 #define MAX_READ_STATUS 800
47 #define MAX_READ_HOLD_REGS 100 46 #define MAX_READ_HOLD_REGS 100
@@ -49,7 +48,7 @@ @@ -49,7 +48,7 @@
49 #define MAX_WRITE_COILS 800 48 #define MAX_WRITE_COILS 800
50 #define MAX_WRITE_REGS 100 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 /* Time out between trames in microsecond */ 53 /* Time out between trames in microsecond */
55 #define TIME_OUT_BEGIN_OF_TRAME 500000 54 #define TIME_OUT_BEGIN_OF_TRAME 500000
@@ -107,7 +106,7 @@ @@ -107,7 +106,7 @@
107 #define CONNECTION_CLOSED -0x12 106 #define CONNECTION_CLOSED -0x12
108 107
109 /* Internal using */ 108 /* Internal using */
110 -#define MSG_SIZE_UNDEFINED -1 109 +#define MSG_LENGTH_UNDEFINED -1
111 110
112 typedef enum { RTU, TCP } type_com_t; 111 typedef enum { RTU, TCP } type_com_t;
113 typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t; 112 typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
@@ -149,8 +148,8 @@ typedef struct { @@ -149,8 +148,8 @@ typedef struct {
149 uint16_t port; 148 uint16_t port;
150 /* Header length used for offset */ 149 /* Header length used for offset */
151 int header_length; 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 /* In error_treat with TCP, do a reconnect or just dump the error */ 153 /* In error_treat with TCP, do a reconnect or just dump the error */
155 error_handling_t error_handling; 154 error_handling_t error_handling;
156 } modbus_param_t; 155 } modbus_param_t;
@@ -274,7 +273,7 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping); @@ -274,7 +273,7 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping);
274 int modbus_init_listen_tcp(modbus_param_t *mb_param); 273 int modbus_init_listen_tcp(modbus_param_t *mb_param);
275 274
276 /* FIXME */ 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 /* Manages the received query. 278 /* Manages the received query.
280 Analyses the query and constructs a response. 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,7 +282,7 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size);
283 accordingly. 282 accordingly.
284 */ 283 */
285 void manage_query(modbus_param_t *mb_param, uint8_t *query, 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 /* Not implemented : 287 /* Not implemented :
289 - read_exception_status() 288 - read_exception_status()
tests/unit-test-slave.c
@@ -74,7 +74,7 @@ int main(void) @@ -74,7 +74,7 @@ int main(void)
74 socket = modbus_init_listen_tcp(&mb_param); 74 socket = modbus_init_listen_tcp(&mb_param);
75 75
76 while (1) { 76 while (1) {
77 - uint8_t query[MAX_PACKET_SIZE]; 77 + uint8_t query[MAX_MESSAGE_LENGTH];
78 int query_size; 78 int query_size;
79 79
80 ret = modbus_listen(&mb_param, query, &query_size); 80 ret = modbus_listen(&mb_param, query, &query_size);