Fix address range in random-test-client
[libmodbus.git] / tests / random-test-client.c
blob2ebbded5b487ff31161d42a93d22e41cb6e3a0d4
1 /*
2 * Copyright © 2008-2014 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
7 #include <stdio.h>
8 #ifndef _MSC_VER
9 #include <unistd.h>
10 #endif
11 #include <string.h>
12 #include <stdlib.h>
13 #include <errno.h>
15 #include <modbus.h>
17 /* The goal of this program is to check all major functions of
18 libmodbus:
19 - write_coil
20 - read_bits
21 - write_coils
22 - write_register
23 - read_registers
24 - write_registers
25 - read_registers
27 All these functions are called with random values on a address
28 range defined by the following defines.
30 #define LOOP 1
31 #define SERVER_ID 17
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.
38 int main(void)
40 modbus_t *ctx;
41 int rc;
42 int nb_fail;
43 int nb_loop;
44 int addr;
45 int nb;
46 uint8_t *tab_rq_bits;
47 uint8_t *tab_rp_bits;
48 uint16_t *tab_rq_registers;
49 uint16_t *tab_rw_rq_registers;
50 uint16_t *tab_rp_registers;
52 /* RTU */
54 ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
55 modbus_set_slave(ctx, SERVER_ID);
58 /* TCP */
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));
65 modbus_free(ctx);
66 return -1;
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++) {
90 int i;
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;
100 /* WRITE BIT */
101 rc = modbus_write_bit(ctx, addr, tab_rq_bits[0]);
102 if (rc != 1) {
103 printf("ERROR modbus_write_bit (%d)\n", rc);
104 printf("Address = %d, value = %d\n", addr, tab_rq_bits[0]);
105 nb_fail++;
106 } else {
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);
111 nb_fail++;
115 /* MULTIPLE BITS */
116 rc = modbus_write_bits(ctx, addr, nb, tab_rq_bits);
117 if (rc != nb) {
118 printf("ERROR modbus_write_bits (%d)\n", rc);
119 printf("Address = %d, nb = %d\n", addr, nb);
120 nb_fail++;
121 } else {
122 rc = modbus_read_bits(ctx, addr, nb, tab_rp_bits);
123 if (rc != nb) {
124 printf("ERROR modbus_read_bits\n");
125 printf("Address = %d, nb = %d\n", addr, nb);
126 nb_fail++;
127 } else {
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]);
134 nb_fail++;
140 /* SINGLE REGISTER */
141 rc = modbus_write_register(ctx, addr, tab_rq_registers[0]);
142 if (rc != 1) {
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]);
146 nb_fail++;
147 } else {
148 rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
149 if (rc != 1) {
150 printf("ERROR modbus_read_registers single (%d)\n", rc);
151 printf("Address = %d\n", addr);
152 nb_fail++;
153 } else {
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]);
159 nb_fail++;
164 /* MULTIPLE REGISTERS */
165 rc = modbus_write_registers(ctx, addr, nb, tab_rq_registers);
166 if (rc != nb) {
167 printf("ERROR modbus_write_registers (%d)\n", rc);
168 printf("Address = %d, nb = %d\n", addr, nb);
169 nb_fail++;
170 } else {
171 rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
172 if (rc != nb) {
173 printf("ERROR modbus_read_registers (%d)\n", rc);
174 printf("Address = %d, nb = %d\n", addr, nb);
175 nb_fail++;
176 } else {
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]);
183 nb_fail++;
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);
192 if (rc != nb) {
193 printf("ERROR modbus_read_and_write_registers (%d)\n", rc);
194 printf("Address = %d, nb = %d\n", addr, nb);
195 nb_fail++;
196 } else {
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]);
203 nb_fail++;
207 rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
208 if (rc != nb) {
209 printf("ERROR modbus_read_registers (%d)\n", rc);
210 printf("Address = %d, nb = %d\n", addr, nb);
211 nb_fail++;
212 } else {
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]);
219 nb_fail++;
226 printf("Test: ");
227 if (nb_fail)
228 printf("%d FAILS\n", nb_fail);
229 else
230 printf("SUCCESS\n");
233 /* Free the memory */
234 free(tab_rq_bits);
235 free(tab_rp_bits);
236 free(tab_rq_registers);
237 free(tab_rp_registers);
238 free(tab_rw_rq_registers);
240 /* Close the connection */
241 modbus_close(ctx);
242 modbus_free(ctx);
244 return 0;