2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
17 /* The goal of this program is to check all major functions of
27 All these functions are called with random values on a address
28 range defined by the following defines.
32 #define ADDRESS_START 0
33 #define ADDRESS_END 99
35 /* At each loop, the program works in the range ADDRESS_START to
36 * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
48 uint16_t *tab_rq_registers
;
49 uint16_t *tab_rw_rq_registers
;
50 uint16_t *tab_rp_registers
;
54 ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
55 modbus_set_slave(ctx, SERVER_ID);
59 ctx
= modbus_new_tcp("127.0.0.1", 1502);
60 modbus_set_debug(ctx
, TRUE
);
62 if (modbus_connect(ctx
) == -1) {
63 fprintf(stderr
, "Connection failed: %s\n", modbus_strerror(errno
));
68 /* Allocate and initialize the different memory spaces */
69 nb
= ADDRESS_END
- ADDRESS_START
;
71 tab_rq_bits
= (uint8_t *) malloc(nb
* sizeof(uint8_t));
72 memset(tab_rq_bits
, 0, nb
* sizeof(uint8_t));
74 tab_rp_bits
= (uint8_t *) malloc(nb
* sizeof(uint8_t));
75 memset(tab_rp_bits
, 0, nb
* sizeof(uint8_t));
77 tab_rq_registers
= (uint16_t *) malloc(nb
* sizeof(uint16_t));
78 memset(tab_rq_registers
, 0, nb
* sizeof(uint16_t));
80 tab_rp_registers
= (uint16_t *) malloc(nb
* sizeof(uint16_t));
81 memset(tab_rp_registers
, 0, nb
* sizeof(uint16_t));
83 tab_rw_rq_registers
= (uint16_t *) malloc(nb
* sizeof(uint16_t));
84 memset(tab_rw_rq_registers
, 0, nb
* sizeof(uint16_t));
86 nb_loop
= nb_fail
= 0;
87 while (nb_loop
++ < LOOP
) {
88 for (addr
= ADDRESS_START
; addr
< ADDRESS_END
; addr
++) {
91 /* Random numbers (short) */
92 for (i
= 0; i
< nb
; i
++) {
93 tab_rq_registers
[i
] = (uint16_t) (65535.0 * rand() / (RAND_MAX
+ 1.0));
94 tab_rw_rq_registers
[i
] = ~tab_rq_registers
[i
];
95 tab_rq_bits
[i
] = tab_rq_registers
[i
] % 2;
97 nb
= ADDRESS_END
- addr
;
100 rc
= modbus_write_bit(ctx
, addr
, tab_rq_bits
[0]);
102 printf("ERROR modbus_write_bit (%d)\n", rc
);
103 printf("Address = %d, value = %d\n", addr
, tab_rq_bits
[0]);
106 rc
= modbus_read_bits(ctx
, addr
, 1, tab_rp_bits
);
107 if (rc
!= 1 || tab_rq_bits
[0] != tab_rp_bits
[0]) {
108 printf("ERROR modbus_read_bits single (%d)\n", rc
);
109 printf("address = %d\n", addr
);
115 rc
= modbus_write_bits(ctx
, addr
, nb
, tab_rq_bits
);
117 printf("ERROR modbus_write_bits (%d)\n", rc
);
118 printf("Address = %d, nb = %d\n", addr
, nb
);
121 rc
= modbus_read_bits(ctx
, addr
, nb
, tab_rp_bits
);
123 printf("ERROR modbus_read_bits\n");
124 printf("Address = %d, nb = %d\n", addr
, nb
);
127 for (i
= 0; i
< nb
; i
++) {
128 if (tab_rp_bits
[i
] != tab_rq_bits
[i
]) {
129 printf("ERROR modbus_read_bits\n");
130 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
142 /* SINGLE REGISTER */
143 rc
= modbus_write_register(ctx
, addr
, tab_rq_registers
[0]);
145 printf("ERROR modbus_write_register (%d)\n", rc
);
146 printf("Address = %d, value = %d (0x%X)\n",
149 tab_rq_registers
[0]);
152 rc
= modbus_read_registers(ctx
, addr
, 1, tab_rp_registers
);
154 printf("ERROR modbus_read_registers single (%d)\n", rc
);
155 printf("Address = %d\n", addr
);
158 if (tab_rq_registers
[0] != tab_rp_registers
[0]) {
159 printf("ERROR modbus_read_registers single\n");
160 printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n",
165 tab_rp_registers
[0]);
171 /* MULTIPLE REGISTERS */
172 rc
= modbus_write_registers(ctx
, addr
, nb
, tab_rq_registers
);
174 printf("ERROR modbus_write_registers (%d)\n", rc
);
175 printf("Address = %d, nb = %d\n", addr
, nb
);
178 rc
= modbus_read_registers(ctx
, addr
, nb
, tab_rp_registers
);
180 printf("ERROR modbus_read_registers (%d)\n", rc
);
181 printf("Address = %d, nb = %d\n", addr
, nb
);
184 for (i
= 0; i
< nb
; i
++) {
185 if (tab_rq_registers
[i
] != tab_rp_registers
[i
]) {
186 printf("ERROR modbus_read_registers\n");
187 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
192 tab_rp_registers
[i
]);
198 /* R/W MULTIPLE REGISTERS */
199 rc
= modbus_write_and_read_registers(
200 ctx
, addr
, nb
, tab_rw_rq_registers
, addr
, nb
, tab_rp_registers
);
202 printf("ERROR modbus_read_and_write_registers (%d)\n", rc
);
203 printf("Address = %d, nb = %d\n", addr
, nb
);
206 for (i
= 0; i
< nb
; i
++) {
207 if (tab_rp_registers
[i
] != tab_rw_rq_registers
[i
]) {
208 printf("ERROR modbus_read_and_write_registers READ\n");
209 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
212 tab_rw_rq_registers
[i
],
214 tab_rw_rq_registers
[i
]);
219 rc
= modbus_read_registers(ctx
, addr
, nb
, tab_rp_registers
);
221 printf("ERROR modbus_read_registers (%d)\n", rc
);
222 printf("Address = %d, nb = %d\n", addr
, nb
);
225 for (i
= 0; i
< nb
; i
++) {
226 if (tab_rw_rq_registers
[i
] != tab_rp_registers
[i
]) {
227 printf("ERROR modbus_read_and_write_registers WRITE\n");
228 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
230 tab_rw_rq_registers
[i
],
231 tab_rw_rq_registers
[i
],
233 tab_rp_registers
[i
]);
243 printf("%d FAILS\n", nb_fail
);
248 /* Free the memory */
251 free(tab_rq_registers
);
252 free(tab_rp_registers
);
253 free(tab_rw_rq_registers
);
255 /* Close the connection */