Avoid to shadow 'boolean' on weird platform (Windows)
[libmodbus.git] / tests / unit-test-client.c
blob8b155ada41ab84c7303c4059e455d7889d0152ed
1 /*
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/>.
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <modbus.h>
25 #include "unit-test.h"
27 enum {
28 TCP,
29 TCP_PI,
30 RTU
33 int main(int argc, char *argv[])
35 uint8_t *tab_rp_bits;
36 uint16_t *tab_rp_registers;
37 uint16_t *tab_rp_registers_bad;
38 modbus_t *ctx;
39 int i;
40 uint8_t value;
41 int nb_points;
42 int rc;
43 float real;
44 uint32_t ireal;
45 struct timeval old_response_timeout;
46 struct timeval response_timeout;
47 int use_backend;
49 if (argc > 1) {
50 if (strcmp(argv[1], "tcp") == 0) {
51 use_backend = TCP;
52 } else if (strcmp(argv[1], "tcppi") == 0) {
53 use_backend = TCP_PI;
54 } else if (strcmp(argv[1], "rtu") == 0) {
55 use_backend = RTU;
56 } else {
57 printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]);
58 exit(1);
60 } else {
61 /* By default */
62 use_backend = TCP;
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");
69 } else {
70 ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
72 if (ctx == NULL) {
73 fprintf(stderr, "Unable to allocate libmodbus context\n");
74 return -1;
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));
87 modbus_free(ctx);
88 return -1;
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");
106 /** COIL BITS **/
108 /* Single */
109 rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
110 printf("1/2 modbus_write_bit: ");
111 if (rc == 1) {
112 printf("OK\n");
113 } else {
114 printf("FAILED\n");
115 goto close;
118 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
119 printf("2/2 modbus_read_bits: ");
120 if (rc != 1) {
121 printf("FAILED (nb points %d)\n", rc);
122 goto close;
125 if (tab_rp_bits[0] != ON) {
126 printf("FAILED (%0X != %0X)\n", tab_rp_bits[0], ON);
127 goto close;
129 printf("OK\n");
130 /* End single */
132 /* Multiple bits */
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) {
141 printf("OK\n");
142 } else {
143 printf("FAILED\n");
144 goto close;
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);
152 goto close;
155 i = 0;
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]);
163 goto close;
166 nb_points -= nb_bits;
167 i++;
169 printf("OK\n");
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);
179 goto close;
182 i = 0;
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]);
190 goto close;
193 nb_points -= nb_bits;
194 i++;
196 printf("OK\n");
198 /** HOLDING REGISTERS **/
200 /* Single register */
201 rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234);
202 printf("1/2 modbus_write_register: ");
203 if (rc == 1) {
204 printf("OK\n");
205 } else {
206 printf("FAILED\n");
207 goto close;
210 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
211 1, tab_rp_registers);
212 printf("2/2 modbus_read_registers: ");
213 if (rc != 1) {
214 printf("FAILED (nb points %d)\n", rc);
215 goto close;
218 if (tab_rp_registers[0] != 0x1234) {
219 printf("FAILED (%0X != %0X)\n",
220 tab_rp_registers[0], 0x1234);
221 goto close;
223 printf("OK\n");
224 /* End of single register */
226 /* Many registers */
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) {
231 printf("OK\n");
232 } else {
233 printf("FAILED\n");
234 goto close;
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);
242 goto close;
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",
248 tab_rp_registers[i],
249 UT_REGISTERS_TAB[i]);
250 goto close;
253 printf("OK\n");
255 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
256 0, tab_rp_registers);
257 printf("3/5 modbus_read_registers (0): ");
258 if (rc != 0) {
259 printf("FAILED (nb points %d)\n", rc);
260 goto close;
262 printf("OK\n");
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,
274 tab_rp_registers,
275 UT_REGISTERS_ADDRESS,
276 UT_REGISTERS_NB,
277 tab_rp_registers);
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);
281 goto close;
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);
293 goto close;
296 printf("OK\n");
298 /* End of many registers */
301 /** INPUT REGISTERS **/
302 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
303 UT_INPUT_REGISTERS_NB,
304 tab_rp_registers);
305 printf("1/1 modbus_read_input_registers: ");
306 if (rc != UT_INPUT_REGISTERS_NB) {
307 printf("FAILED (nb points %d)\n", rc);
308 goto close;
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]);
315 goto close;
318 printf("OK\n");
320 printf("\nTEST FLOATS\n");
321 /** FLOAT **/
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)) {
326 printf("OK\n");
327 } else {
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);
333 goto close;
336 printf("2/4 Get float: ");
337 real = modbus_get_float(tab_rp_registers);
338 if (real == UT_REAL) {
339 printf("OK\n");
340 } else {
341 printf("FAILED (%f != %f)\n", real, UT_REAL);
342 goto close;
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)) {
349 printf("OK\n");
350 } else {
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);
354 goto close;
357 printf("4/4 Get float in DCBA order: ");
358 real = modbus_get_float_dcba(tab_rp_registers);
359 if (real == UT_REAL) {
360 printf("OK\n");
361 } else {
362 printf("FAILED (%f != %f)\n", real, UT_REAL);
363 goto close;
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) {
377 printf("OK\n");
378 } else {
379 printf("FAILED\n");
380 goto close;
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)
387 printf("OK\n");
388 else {
389 printf("FAILED\n");
390 goto close;
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)
397 printf("OK\n");
398 else {
399 printf("FAILED\n");
400 goto close;
403 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
404 UT_INPUT_REGISTERS_NB + 1,
405 tab_rp_registers);
406 printf("* modbus_read_input_registers: ");
407 if (rc == -1 && errno == EMBXILADD)
408 printf("OK\n");
409 else {
410 printf("FAILED\n");
411 goto close;
414 rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
415 printf("* modbus_write_bit: ");
416 if (rc == -1 && errno == EMBXILADD) {
417 printf("OK\n");
418 } else {
419 printf("FAILED\n");
420 goto close;
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) {
427 printf("OK\n");
428 } else {
429 printf("FAILED\n");
430 goto close;
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) {
437 printf("OK\n");
438 } else {
439 printf("FAILED\n");
440 goto close;
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) {
451 printf("OK\n");
452 } else {
453 printf("FAILED\n");
454 goto close;
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) {
461 printf("OK\n");
462 } else {
463 printf("FAILED\n");
464 goto close;
467 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
468 MODBUS_MAX_READ_REGISTERS + 1,
469 tab_rp_registers);
470 printf("* modbus_read_registers: ");
471 if (rc == -1 && errno == EMBMDATA) {
472 printf("OK\n");
473 } else {
474 printf("FAILED\n");
475 goto close;
478 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
479 MODBUS_MAX_READ_REGISTERS + 1,
480 tab_rp_registers);
481 printf("* modbus_read_input_registers: ");
482 if (rc == -1 && errno == EMBMDATA) {
483 printf("OK\n");
484 } else {
485 printf("FAILED\n");
486 goto close;
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) {
493 printf("OK\n");
494 } else {
495 goto close;
496 printf("FAILED\n");
499 rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
500 MODBUS_MAX_WRITE_REGISTERS + 1,
501 tab_rp_registers);
502 printf("* modbus_write_registers: ");
503 if (rc == -1 && errno == EMBMDATA) {
504 printf("OK\n");
505 } else {
506 printf("FAILED\n");
507 goto close;
510 /** SLAVE REPLY **/
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) {
528 printf("OK\n");
529 } else {
530 printf("FAILED\n");
531 goto close;
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
541 * ignore both. */
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: ",
547 INVALID_SERVER_ID);
549 if (rc == -1 && errno == ETIMEDOUT) {
550 printf("OK\n");
551 } else {
552 printf("FAILED (%d)\n", rc);
553 goto close;
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: ",
561 INVALID_SERVER_ID);
563 if (rc == -1 && errno == ETIMEDOUT) {
564 printf("OK\n");
565 } else {
566 printf("FAILED (%d)\n", rc);
567 goto close;
569 } else {
570 /* Response in TCP mode */
571 printf("1/4 Response from slave %d: ", INVALID_SERVER_ID);
573 if (rc == UT_REGISTERS_NB) {
574 printf("OK\n");
575 } else {
576 printf("FAILED\n");
577 goto close;
581 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
582 if (rc == -1) {
583 printf("Invalid broacast address\n");
584 goto close;
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) {
591 printf("OK\n");
592 } else {
593 printf("FAILED\n");
594 goto close;
597 /* Restore slave */
598 if (use_backend == RTU) {
599 modbus_set_slave(ctx, SERVER_ID);
600 } else {
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);
607 if (rc == -1) {
608 printf("FAILED\n");
609 goto close;
612 /* Slave ID is an arbitraty number for libmodbus */
613 if (rc > 0) {
614 printf("OK Slave ID is %d\n", tab_rp_bits[0]);
615 } else {
616 printf("FAILED\n");
617 goto close;
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");
623 } else {
624 printf("FAILED\n");
625 goto close;
628 /* Print additional data as string */
629 if (rc > 2) {
630 printf("Additional data: ");
631 for (i=2; i < rc; i++) {
632 printf("%c", tab_rp_bits[i]);
634 printf("\n");
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);
640 if (rc == -1) {
641 printf("OK\n");
642 } else {
643 printf("FAILED\n");
644 goto close;
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) {
659 printf("OK\n");
660 } else {
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
668 * libmodbus */
670 /** BAD RESPONSE **/
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) {
680 printf("OK\n");
681 } else {
682 printf("FAILED\n");
683 goto close;
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) {
695 printf("OK\n");
696 } else {
697 printf("FAILED\n");
698 goto close;
701 /** RAW REQUEST */
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 };
707 int req_length;
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))) {
717 printf("OK\n");
718 } else {
719 printf("FAILED (%d)\n", req_length);
720 goto close;
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) &&
727 rc == 19)) {
728 printf("OK\n");
729 } else {
730 printf("FAILED (%d)\n", rc);
731 goto close;
735 printf("\nALL TESTS PASS WITH SUCCESS.\n");
737 close:
738 /* Free the memory */
739 free(tab_rp_bits);
740 free(tab_rp_registers);
742 /* Close the connection */
743 modbus_close(ctx);
744 modbus_free(ctx);
746 return 0;