2 * Copyright © 2008-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "unit-test.h"
33 int main(int argc
, char *argv
[])
36 uint16_t *tab_rp_registers
;
37 uint16_t *tab_rp_registers_bad
;
45 struct timeval old_response_timeout
;
46 struct timeval response_timeout
;
50 if (strcmp(argv
[1], "tcp") == 0) {
52 } else if (strcmp(argv
[1], "tcppi") == 0) {
54 } else if (strcmp(argv
[1], "rtu") == 0) {
57 printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv
[0]);
65 if (use_backend
== TCP
) {
66 ctx
= modbus_new_tcp("127.0.0.1", 1502);
67 } else if (use_backend
== TCP_PI
) {
68 ctx
= modbus_new_tcp_pi("::1", "1502");
70 ctx
= modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
73 fprintf(stderr
, "Unable to allocate libmodbus context\n");
76 modbus_set_debug(ctx
, TRUE
);
77 modbus_set_error_recovery(ctx
,
78 MODBUS_ERROR_RECOVERY_LINK
|
79 MODBUS_ERROR_RECOVERY_PROTOCOL
);
81 if (use_backend
== RTU
) {
82 modbus_set_slave(ctx
, SERVER_ID
);
85 if (modbus_connect(ctx
) == -1) {
86 fprintf(stderr
, "Connection failed: %s\n", modbus_strerror(errno
));
91 /* Allocate and initialize the memory to store the bits */
92 nb_points
= (UT_BITS_NB
> UT_INPUT_BITS_NB
) ? UT_BITS_NB
: UT_INPUT_BITS_NB
;
93 tab_rp_bits
= (uint8_t *) malloc(nb_points
* sizeof(uint8_t));
94 memset(tab_rp_bits
, 0, nb_points
* sizeof(uint8_t));
96 /* Allocate and initialize the memory to store the registers */
97 nb_points
= (UT_REGISTERS_NB
> UT_INPUT_REGISTERS_NB
) ?
98 UT_REGISTERS_NB
: UT_INPUT_REGISTERS_NB
;
99 tab_rp_registers
= (uint16_t *) malloc(nb_points
* sizeof(uint16_t));
100 memset(tab_rp_registers
, 0, nb_points
* sizeof(uint16_t));
102 printf("** UNIT TESTING **\n");
104 printf("\nTEST WRITE/READ:\n");
109 rc
= modbus_write_bit(ctx
, UT_BITS_ADDRESS
, ON
);
110 printf("1/2 modbus_write_bit: ");
118 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
, 1, tab_rp_bits
);
119 printf("2/2 modbus_read_bits: ");
121 printf("FAILED (nb points %d)\n", rc
);
125 if (tab_rp_bits
[0] != ON
) {
126 printf("FAILED (%0X != %0X)\n", tab_rp_bits
[0], ON
);
134 uint8_t tab_value
[UT_BITS_NB
];
136 modbus_set_bits_from_bytes(tab_value
, 0, UT_BITS_NB
, UT_BITS_TAB
);
137 rc
= modbus_write_bits(ctx
, UT_BITS_ADDRESS
,
138 UT_BITS_NB
, tab_value
);
139 printf("1/2 modbus_write_bits: ");
140 if (rc
== UT_BITS_NB
) {
148 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
, UT_BITS_NB
, tab_rp_bits
);
149 printf("2/2 modbus_read_bits: ");
150 if (rc
!= UT_BITS_NB
) {
151 printf("FAILED (nb points %d)\n", rc
);
156 nb_points
= UT_BITS_NB
;
157 while (nb_points
> 0) {
158 int nb_bits
= (nb_points
> 8) ? 8 : nb_points
;
160 value
= modbus_get_byte_from_bits(tab_rp_bits
, i
*8, nb_bits
);
161 if (value
!= UT_BITS_TAB
[i
]) {
162 printf("FAILED (%0X != %0X)\n", value
, UT_BITS_TAB
[i
]);
166 nb_points
-= nb_bits
;
170 /* End of multiple bits */
172 /** DISCRETE INPUTS **/
173 rc
= modbus_read_input_bits(ctx
, UT_INPUT_BITS_ADDRESS
,
174 UT_INPUT_BITS_NB
, tab_rp_bits
);
175 printf("1/1 modbus_read_input_bits: ");
177 if (rc
!= UT_INPUT_BITS_NB
) {
178 printf("FAILED (nb points %d)\n", rc
);
183 nb_points
= UT_INPUT_BITS_NB
;
184 while (nb_points
> 0) {
185 int nb_bits
= (nb_points
> 8) ? 8 : nb_points
;
187 value
= modbus_get_byte_from_bits(tab_rp_bits
, i
*8, nb_bits
);
188 if (value
!= UT_INPUT_BITS_TAB
[i
]) {
189 printf("FAILED (%0X != %0X)\n", value
, UT_INPUT_BITS_TAB
[i
]);
193 nb_points
-= nb_bits
;
198 /** HOLDING REGISTERS **/
200 /* Single register */
201 rc
= modbus_write_register(ctx
, UT_REGISTERS_ADDRESS
, 0x1234);
202 printf("1/2 modbus_write_register: ");
210 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
211 1, tab_rp_registers
);
212 printf("2/2 modbus_read_registers: ");
214 printf("FAILED (nb points %d)\n", rc
);
218 if (tab_rp_registers
[0] != 0x1234) {
219 printf("FAILED (%0X != %0X)\n",
220 tab_rp_registers
[0], 0x1234);
224 /* End of single register */
227 rc
= modbus_write_registers(ctx
, UT_REGISTERS_ADDRESS
,
228 UT_REGISTERS_NB
, UT_REGISTERS_TAB
);
229 printf("1/5 modbus_write_registers: ");
230 if (rc
== UT_REGISTERS_NB
) {
237 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
238 UT_REGISTERS_NB
, tab_rp_registers
);
239 printf("2/5 modbus_read_registers: ");
240 if (rc
!= UT_REGISTERS_NB
) {
241 printf("FAILED (nb points %d)\n", rc
);
245 for (i
=0; i
< UT_REGISTERS_NB
; i
++) {
246 if (tab_rp_registers
[i
] != UT_REGISTERS_TAB
[i
]) {
247 printf("FAILED (%0X != %0X)\n",
249 UT_REGISTERS_TAB
[i
]);
255 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
256 0, tab_rp_registers
);
257 printf("3/5 modbus_read_registers (0): ");
259 printf("FAILED (nb points %d)\n", rc
);
264 nb_points
= (UT_REGISTERS_NB
>
265 UT_INPUT_REGISTERS_NB
) ?
266 UT_REGISTERS_NB
: UT_INPUT_REGISTERS_NB
;
267 memset(tab_rp_registers
, 0, nb_points
* sizeof(uint16_t));
269 /* Write registers to zero from tab_rp_registers and store read registers
270 into tab_rp_registers. So the read registers must set to 0, except the
271 first one because there is an offset of 1 register on write. */
272 rc
= modbus_write_and_read_registers(ctx
,
273 UT_REGISTERS_ADDRESS
+ 1, UT_REGISTERS_NB
- 1,
275 UT_REGISTERS_ADDRESS
,
278 printf("4/5 modbus_write_and_read_registers: ");
279 if (rc
!= UT_REGISTERS_NB
) {
280 printf("FAILED (nb points %d != %d)\n", rc
, UT_REGISTERS_NB
);
284 if (tab_rp_registers
[0] != UT_REGISTERS_TAB
[0]) {
285 printf("FAILED (%0X != %0X)\n",
286 tab_rp_registers
[0], UT_REGISTERS_TAB
[0]);
289 for (i
=1; i
< UT_REGISTERS_NB
; i
++) {
290 if (tab_rp_registers
[i
] != 0) {
291 printf("FAILED (%0X != %0X)\n",
292 tab_rp_registers
[i
], 0);
298 /* End of many registers */
301 /** INPUT REGISTERS **/
302 rc
= modbus_read_input_registers(ctx
, UT_INPUT_REGISTERS_ADDRESS
,
303 UT_INPUT_REGISTERS_NB
,
305 printf("1/1 modbus_read_input_registers: ");
306 if (rc
!= UT_INPUT_REGISTERS_NB
) {
307 printf("FAILED (nb points %d)\n", rc
);
311 for (i
=0; i
< UT_INPUT_REGISTERS_NB
; i
++) {
312 if (tab_rp_registers
[i
] != UT_INPUT_REGISTERS_TAB
[i
]) {
313 printf("FAILED (%0X != %0X)\n",
314 tab_rp_registers
[i
], UT_INPUT_REGISTERS_TAB
[i
]);
320 printf("\nTEST FLOATS\n");
322 printf("1/4 Set float: ");
323 modbus_set_float(UT_REAL
, tab_rp_registers
);
324 if (tab_rp_registers
[1] == (UT_IREAL
>> 16) &&
325 tab_rp_registers
[0] == (UT_IREAL
& 0xFFFF)) {
328 /* Avoid *((uint32_t *)tab_rp_registers)
329 * https://github.com/stephane/libmodbus/pull/104 */
330 ireal
= (uint32_t) tab_rp_registers
[0] & 0xFFFF;
331 ireal
|= (uint32_t) tab_rp_registers
[1] << 16;
332 printf("FAILED (%x != %x)\n", ireal
, UT_IREAL
);
336 printf("2/4 Get float: ");
337 real
= modbus_get_float(tab_rp_registers
);
338 if (real
== UT_REAL
) {
341 printf("FAILED (%f != %f)\n", real
, UT_REAL
);
345 printf("3/4 Set float in DBCA order: ");
346 modbus_set_float_dcba(UT_REAL
, tab_rp_registers
);
347 if (tab_rp_registers
[1] == (UT_IREAL_DCBA
>> 16) &&
348 tab_rp_registers
[0] == (UT_IREAL_DCBA
& 0xFFFF)) {
351 ireal
= (uint32_t) tab_rp_registers
[0] & 0xFFFF;
352 ireal
|= (uint32_t) tab_rp_registers
[1] << 16;
353 printf("FAILED (%x != %x)\n", ireal
, UT_IREAL_DCBA
);
357 printf("4/4 Get float in DCBA order: ");
358 real
= modbus_get_float_dcba(tab_rp_registers
);
359 if (real
== UT_REAL
) {
362 printf("FAILED (%f != %f)\n", real
, UT_REAL
);
365 printf("\nAt this point, error messages doesn't mean the test has failed\n");
367 /** ILLEGAL DATA ADDRESS **/
368 printf("\nTEST ILLEGAL DATA ADDRESS:\n");
370 /* The mapping begins at 0 and ends at address + nb_points so
371 * the addresses are not valid. */
373 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
,
374 UT_BITS_NB
+ 1, tab_rp_bits
);
375 printf("* modbus_read_bits: ");
376 if (rc
== -1 && errno
== EMBXILADD
) {
383 rc
= modbus_read_input_bits(ctx
, UT_INPUT_BITS_ADDRESS
,
384 UT_INPUT_BITS_NB
+ 1, tab_rp_bits
);
385 printf("* modbus_read_input_bits: ");
386 if (rc
== -1 && errno
== EMBXILADD
)
393 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
394 UT_REGISTERS_NB
+ 1, tab_rp_registers
);
395 printf("* modbus_read_registers: ");
396 if (rc
== -1 && errno
== EMBXILADD
)
403 rc
= modbus_read_input_registers(ctx
, UT_INPUT_REGISTERS_ADDRESS
,
404 UT_INPUT_REGISTERS_NB
+ 1,
406 printf("* modbus_read_input_registers: ");
407 if (rc
== -1 && errno
== EMBXILADD
)
414 rc
= modbus_write_bit(ctx
, UT_BITS_ADDRESS
+ UT_BITS_NB
, ON
);
415 printf("* modbus_write_bit: ");
416 if (rc
== -1 && errno
== EMBXILADD
) {
423 rc
= modbus_write_bits(ctx
, UT_BITS_ADDRESS
+ UT_BITS_NB
,
424 UT_BITS_NB
, tab_rp_bits
);
425 printf("* modbus_write_coils: ");
426 if (rc
== -1 && errno
== EMBXILADD
) {
433 rc
= modbus_write_registers(ctx
, UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB
,
434 UT_REGISTERS_NB
, tab_rp_registers
);
435 printf("* modbus_write_registers: ");
436 if (rc
== -1 && errno
== EMBXILADD
) {
444 /** TOO MANY DATA **/
445 printf("\nTEST TOO MANY DATA ERROR:\n");
447 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
,
448 MODBUS_MAX_READ_BITS
+ 1, tab_rp_bits
);
449 printf("* modbus_read_bits: ");
450 if (rc
== -1 && errno
== EMBMDATA
) {
457 rc
= modbus_read_input_bits(ctx
, UT_INPUT_BITS_ADDRESS
,
458 MODBUS_MAX_READ_BITS
+ 1, tab_rp_bits
);
459 printf("* modbus_read_input_bits: ");
460 if (rc
== -1 && errno
== EMBMDATA
) {
467 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
468 MODBUS_MAX_READ_REGISTERS
+ 1,
470 printf("* modbus_read_registers: ");
471 if (rc
== -1 && errno
== EMBMDATA
) {
478 rc
= modbus_read_input_registers(ctx
, UT_INPUT_REGISTERS_ADDRESS
,
479 MODBUS_MAX_READ_REGISTERS
+ 1,
481 printf("* modbus_read_input_registers: ");
482 if (rc
== -1 && errno
== EMBMDATA
) {
489 rc
= modbus_write_bits(ctx
, UT_BITS_ADDRESS
,
490 MODBUS_MAX_WRITE_BITS
+ 1, tab_rp_bits
);
491 printf("* modbus_write_bits: ");
492 if (rc
== -1 && errno
== EMBMDATA
) {
499 rc
= modbus_write_registers(ctx
, UT_REGISTERS_ADDRESS
,
500 MODBUS_MAX_WRITE_REGISTERS
+ 1,
502 printf("* modbus_write_registers: ");
503 if (rc
== -1 && errno
== EMBMDATA
) {
511 printf("\nTEST SLAVE REPLY:\n");
512 modbus_set_slave(ctx
, INVALID_SERVER_ID
);
513 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
514 UT_REGISTERS_NB
, tab_rp_registers
);
515 if (use_backend
== RTU
) {
516 const int RAW_REQ_LENGTH
= 6;
517 uint8_t raw_req
[] = { INVALID_SERVER_ID
, 0x03, 0x00, 0x01, 0x01, 0x01 };
518 /* Too many points */
519 uint8_t raw_invalid_req
[] = { INVALID_SERVER_ID
, 0x03, 0x00, 0x01, 0xFF, 0xFF };
520 const int RAW_REP_LENGTH
= 7;
521 uint8_t raw_rep
[] = { INVALID_SERVER_ID
, 0x03, 0x04, 0, 0, 0, 0 };
522 uint8_t rsp
[MODBUS_RTU_MAX_ADU_LENGTH
];
524 /* No response in RTU mode */
525 printf("1/5-A No response from slave %d: ", INVALID_SERVER_ID
);
527 if (rc
== -1 && errno
== ETIMEDOUT
) {
534 /* The slave raises a timeout on a confirmation to ignore because if an
535 * indication for another slave is received, a confirmation must follow */
538 /* Send a pair of indication/confimration to the slave with a different
539 * slave ID to simulate a communication on a RS485 bus. At first, the
540 * slave will see the indication message then the confirmation, and it must
542 modbus_send_raw_request(ctx
, raw_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
543 modbus_send_raw_request(ctx
, raw_rep
, RAW_REP_LENGTH
* sizeof(uint8_t));
544 rc
= modbus_receive_confirmation(ctx
, rsp
);
546 printf("1/5-B No response from slave %d on indication/confirmation messages: ",
549 if (rc
== -1 && errno
== ETIMEDOUT
) {
552 printf("FAILED (%d)\n", rc
);
556 /* Send an INVALID request for another slave */
557 modbus_send_raw_request(ctx
, raw_invalid_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
558 rc
= modbus_receive_confirmation(ctx
, rsp
);
560 printf("1/5-C No response from slave %d with invalid request: ",
563 if (rc
== -1 && errno
== ETIMEDOUT
) {
566 printf("FAILED (%d)\n", rc
);
570 /* Response in TCP mode */
571 printf("1/4 Response from slave %d: ", INVALID_SERVER_ID
);
573 if (rc
== UT_REGISTERS_NB
) {
581 rc
= modbus_set_slave(ctx
, MODBUS_BROADCAST_ADDRESS
);
583 printf("Invalid broacast address\n");
587 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
588 UT_REGISTERS_NB
, tab_rp_registers
);
589 printf("2/5 Reply after a broadcast query: ");
590 if (rc
== UT_REGISTERS_NB
) {
598 if (use_backend
== RTU
) {
599 modbus_set_slave(ctx
, SERVER_ID
);
601 modbus_set_slave(ctx
, MODBUS_TCP_SLAVE
);
604 printf("3/5 Report slave ID: \n");
605 /* tab_rp_bits is used to store bytes */
606 rc
= modbus_report_slave_id(ctx
, tab_rp_bits
);
612 /* Slave ID is an arbitraty number for libmodbus */
614 printf("OK Slave ID is %d\n", tab_rp_bits
[0]);
620 /* Run status indicator */
621 if (rc
> 1 && tab_rp_bits
[1] == 0xFF) {
622 printf("OK Run Status Indicator is %s\n", tab_rp_bits
[1] ? "ON" : "OFF");
628 /* Print additional data as string */
630 printf("Additional data: ");
631 for (i
=2; i
< rc
; i
++) {
632 printf("%c", tab_rp_bits
[i
]);
637 printf("5/5 Response with an invalid TID or slave: ");
638 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE
,
639 1, tab_rp_registers
);
647 /* Save original timeout */
648 modbus_get_response_timeout(ctx
, &old_response_timeout
);
650 /* Define a new and too short timeout */
651 response_timeout
.tv_sec
= 0;
652 response_timeout
.tv_usec
= 0;
653 modbus_set_response_timeout(ctx
, &response_timeout
);
655 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
656 UT_REGISTERS_NB
, tab_rp_registers
);
657 printf("4/4 Too short timeout: ");
658 if (rc
== -1 && errno
== ETIMEDOUT
) {
661 printf("FAILED (can fail on slow systems or Windows)\n");
664 /* Restore original timeout */
665 modbus_set_response_timeout(ctx
, &old_response_timeout
);
667 /* A wait and flush operation is done by the error recovery code of
671 printf("\nTEST BAD RESPONSE ERROR:\n");
673 /* Allocate only the required space */
674 tab_rp_registers_bad
= (uint16_t *) malloc(
675 UT_REGISTERS_NB_SPECIAL
* sizeof(uint16_t));
676 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
,
677 UT_REGISTERS_NB_SPECIAL
, tab_rp_registers_bad
);
678 printf("* modbus_read_registers: ");
679 if (rc
== -1 && errno
== EMBBADDATA
) {
686 free(tab_rp_registers_bad
);
688 /** MANUAL EXCEPTION **/
689 printf("\nTEST MANUAL EXCEPTION:\n");
691 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS_SPECIAL
,
692 UT_REGISTERS_NB
, tab_rp_registers
);
693 printf("* modbus_read_registers at special address: ");
694 if (rc
== -1 && errno
== EMBXSBUSY
) {
702 printf("\nTEST RAW REQUEST:\n");
704 const int RAW_REQ_LENGTH
= 6;
705 uint8_t raw_req
[] = { (use_backend
== RTU
) ? SERVER_ID
: 0xFF,
706 0x03, 0x00, 0x01, 0x0, 0x05 };
708 uint8_t rsp
[MODBUS_TCP_MAX_ADU_LENGTH
];
710 req_length
= modbus_send_raw_request(ctx
, raw_req
,
711 RAW_REQ_LENGTH
* sizeof(uint8_t));
713 printf("* modbus_send_raw_request: ");
714 if ((use_backend
== RTU
&& req_length
== (RAW_REQ_LENGTH
+ 2)) ||
715 ((use_backend
== TCP
|| use_backend
== TCP_PI
) &&
716 req_length
== (RAW_REQ_LENGTH
+ 6))) {
719 printf("FAILED (%d)\n", req_length
);
723 printf("* modbus_receive_confirmation: ");
724 rc
= modbus_receive_confirmation(ctx
, rsp
);
725 if ((use_backend
== RTU
&& rc
== 15) ||
726 ((use_backend
== TCP
|| use_backend
== TCP_PI
) &&
730 printf("FAILED (%d)\n", rc
);
735 printf("\nALL TESTS PASS WITH SUCCESS.\n");
738 /* Free the memory */
740 free(tab_rp_registers
);
742 /* Close the connection */