2 * Copyright © 2008-2014 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",
64 modbus_strerror(errno
));
69 /* Allocate and initialize the different memory spaces */
70 nb
= ADDRESS_END
- ADDRESS_START
;
72 tab_rq_bits
= (uint8_t *) malloc(nb
* sizeof(uint8_t));
73 memset(tab_rq_bits
, 0, nb
* sizeof(uint8_t));
75 tab_rp_bits
= (uint8_t *) malloc(nb
* sizeof(uint8_t));
76 memset(tab_rp_bits
, 0, nb
* sizeof(uint8_t));
78 tab_rq_registers
= (uint16_t *) malloc(nb
* sizeof(uint16_t));
79 memset(tab_rq_registers
, 0, nb
* sizeof(uint16_t));
81 tab_rp_registers
= (uint16_t *) malloc(nb
* sizeof(uint16_t));
82 memset(tab_rp_registers
, 0, nb
* sizeof(uint16_t));
84 tab_rw_rq_registers
= (uint16_t *) malloc(nb
* sizeof(uint16_t));
85 memset(tab_rw_rq_registers
, 0, nb
* sizeof(uint16_t));
87 nb_loop
= nb_fail
= 0;
88 while (nb_loop
++ < LOOP
) {
89 for (addr
= ADDRESS_START
; addr
< ADDRESS_END
; addr
++) {
92 /* Random numbers (short) */
93 for (i
=0; i
<nb
; i
++) {
94 tab_rq_registers
[i
] = (uint16_t) (65535.0*rand() / (RAND_MAX
+ 1.0));
95 tab_rw_rq_registers
[i
] = ~tab_rq_registers
[i
];
96 tab_rq_bits
[i
] = tab_rq_registers
[i
] % 2;
98 nb
= ADDRESS_END
- addr
;
101 rc
= modbus_write_bit(ctx
, addr
, tab_rq_bits
[0]);
103 printf("ERROR modbus_write_bit (%d)\n", rc
);
104 printf("Address = %d, value = %d\n", addr
, tab_rq_bits
[0]);
107 rc
= modbus_read_bits(ctx
, addr
, 1, tab_rp_bits
);
108 if (rc
!= 1 || tab_rq_bits
[0] != tab_rp_bits
[0]) {
109 printf("ERROR modbus_read_bits single (%d)\n", rc
);
110 printf("address = %d\n", addr
);
116 rc
= modbus_write_bits(ctx
, addr
, nb
, tab_rq_bits
);
118 printf("ERROR modbus_write_bits (%d)\n", rc
);
119 printf("Address = %d, nb = %d\n", addr
, nb
);
122 rc
= modbus_read_bits(ctx
, addr
, nb
, tab_rp_bits
);
124 printf("ERROR modbus_read_bits\n");
125 printf("Address = %d, nb = %d\n", addr
, nb
);
128 for (i
=0; i
<nb
; i
++) {
129 if (tab_rp_bits
[i
] != tab_rq_bits
[i
]) {
130 printf("ERROR modbus_read_bits\n");
131 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
132 addr
, tab_rq_bits
[i
], tab_rq_bits
[i
],
133 tab_rp_bits
[i
], tab_rp_bits
[i
]);
140 /* SINGLE REGISTER */
141 rc
= modbus_write_register(ctx
, addr
, tab_rq_registers
[0]);
143 printf("ERROR modbus_write_register (%d)\n", rc
);
144 printf("Address = %d, value = %d (0x%X)\n",
145 addr
, tab_rq_registers
[0], tab_rq_registers
[0]);
148 rc
= modbus_read_registers(ctx
, addr
, 1, tab_rp_registers
);
150 printf("ERROR modbus_read_registers single (%d)\n", rc
);
151 printf("Address = %d\n", addr
);
154 if (tab_rq_registers
[0] != tab_rp_registers
[0]) {
155 printf("ERROR modbus_read_registers single\n");
156 printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n",
157 addr
, tab_rq_registers
[0], tab_rq_registers
[0],
158 tab_rp_registers
[0], tab_rp_registers
[0]);
164 /* MULTIPLE REGISTERS */
165 rc
= modbus_write_registers(ctx
, addr
, nb
, tab_rq_registers
);
167 printf("ERROR modbus_write_registers (%d)\n", rc
);
168 printf("Address = %d, nb = %d\n", addr
, nb
);
171 rc
= modbus_read_registers(ctx
, addr
, nb
, tab_rp_registers
);
173 printf("ERROR modbus_read_registers (%d)\n", rc
);
174 printf("Address = %d, nb = %d\n", addr
, nb
);
177 for (i
=0; i
<nb
; i
++) {
178 if (tab_rq_registers
[i
] != tab_rp_registers
[i
]) {
179 printf("ERROR modbus_read_registers\n");
180 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
181 addr
, tab_rq_registers
[i
], tab_rq_registers
[i
],
182 tab_rp_registers
[i
], tab_rp_registers
[i
]);
188 /* R/W MULTIPLE REGISTERS */
189 rc
= modbus_write_and_read_registers(ctx
,
190 addr
, nb
, tab_rw_rq_registers
,
191 addr
, nb
, tab_rp_registers
);
193 printf("ERROR modbus_read_and_write_registers (%d)\n", rc
);
194 printf("Address = %d, nb = %d\n", addr
, nb
);
197 for (i
=0; i
<nb
; i
++) {
198 if (tab_rp_registers
[i
] != tab_rw_rq_registers
[i
]) {
199 printf("ERROR modbus_read_and_write_registers READ\n");
200 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
201 addr
, tab_rp_registers
[i
], tab_rw_rq_registers
[i
],
202 tab_rp_registers
[i
], tab_rw_rq_registers
[i
]);
207 rc
= modbus_read_registers(ctx
, addr
, nb
, tab_rp_registers
);
209 printf("ERROR modbus_read_registers (%d)\n", rc
);
210 printf("Address = %d, nb = %d\n", addr
, nb
);
213 for (i
=0; i
<nb
; i
++) {
214 if (tab_rw_rq_registers
[i
] != tab_rp_registers
[i
]) {
215 printf("ERROR modbus_read_and_write_registers WRITE\n");
216 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
217 addr
, tab_rw_rq_registers
[i
], tab_rw_rq_registers
[i
],
218 tab_rp_registers
[i
], tab_rp_registers
[i
]);
228 printf("%d FAILS\n", nb_fail
);
233 /* Free the memory */
236 free(tab_rq_registers
);
237 free(tab_rp_registers
);
238 free(tab_rw_rq_registers
);
240 /* Close the connection */