Link against socket and nsl libs when building on SunOS
[libmodbus.git] / tests / random-test-client.c
blobc2ccf63fd7b5b49d9ea6dbaa142b0cfa8fc363f2
1 /*
2 * Copyright © 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 <errno.h>
12 #include <stdlib.h>
13 #include <string.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", modbus_strerror(errno));
64 modbus_free(ctx);
65 return -1;
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++) {
89 int i;
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;
99 /* WRITE BIT */
100 rc = modbus_write_bit(ctx, addr, tab_rq_bits[0]);
101 if (rc != 1) {
102 printf("ERROR modbus_write_bit (%d)\n", rc);
103 printf("Address = %d, value = %d\n", addr, tab_rq_bits[0]);
104 nb_fail++;
105 } else {
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);
110 nb_fail++;
114 /* MULTIPLE BITS */
115 rc = modbus_write_bits(ctx, addr, nb, tab_rq_bits);
116 if (rc != nb) {
117 printf("ERROR modbus_write_bits (%d)\n", rc);
118 printf("Address = %d, nb = %d\n", addr, nb);
119 nb_fail++;
120 } else {
121 rc = modbus_read_bits(ctx, addr, nb, tab_rp_bits);
122 if (rc != nb) {
123 printf("ERROR modbus_read_bits\n");
124 printf("Address = %d, nb = %d\n", addr, nb);
125 nb_fail++;
126 } else {
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",
131 addr,
132 tab_rq_bits[i],
133 tab_rq_bits[i],
134 tab_rp_bits[i],
135 tab_rp_bits[i]);
136 nb_fail++;
142 /* SINGLE REGISTER */
143 rc = modbus_write_register(ctx, addr, tab_rq_registers[0]);
144 if (rc != 1) {
145 printf("ERROR modbus_write_register (%d)\n", rc);
146 printf("Address = %d, value = %d (0x%X)\n",
147 addr,
148 tab_rq_registers[0],
149 tab_rq_registers[0]);
150 nb_fail++;
151 } else {
152 rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
153 if (rc != 1) {
154 printf("ERROR modbus_read_registers single (%d)\n", rc);
155 printf("Address = %d\n", addr);
156 nb_fail++;
157 } else {
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",
161 addr,
162 tab_rq_registers[0],
163 tab_rq_registers[0],
164 tab_rp_registers[0],
165 tab_rp_registers[0]);
166 nb_fail++;
171 /* MULTIPLE REGISTERS */
172 rc = modbus_write_registers(ctx, addr, nb, tab_rq_registers);
173 if (rc != nb) {
174 printf("ERROR modbus_write_registers (%d)\n", rc);
175 printf("Address = %d, nb = %d\n", addr, nb);
176 nb_fail++;
177 } else {
178 rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
179 if (rc != nb) {
180 printf("ERROR modbus_read_registers (%d)\n", rc);
181 printf("Address = %d, nb = %d\n", addr, nb);
182 nb_fail++;
183 } else {
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",
188 addr,
189 tab_rq_registers[i],
190 tab_rq_registers[i],
191 tab_rp_registers[i],
192 tab_rp_registers[i]);
193 nb_fail++;
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);
201 if (rc != nb) {
202 printf("ERROR modbus_read_and_write_registers (%d)\n", rc);
203 printf("Address = %d, nb = %d\n", addr, nb);
204 nb_fail++;
205 } else {
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",
210 addr,
211 tab_rp_registers[i],
212 tab_rw_rq_registers[i],
213 tab_rp_registers[i],
214 tab_rw_rq_registers[i]);
215 nb_fail++;
219 rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
220 if (rc != nb) {
221 printf("ERROR modbus_read_registers (%d)\n", rc);
222 printf("Address = %d, nb = %d\n", addr, nb);
223 nb_fail++;
224 } else {
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",
229 addr,
230 tab_rw_rq_registers[i],
231 tab_rw_rq_registers[i],
232 tab_rp_registers[i],
233 tab_rp_registers[i]);
234 nb_fail++;
241 printf("Test: ");
242 if (nb_fail)
243 printf("%d FAILS\n", nb_fail);
244 else
245 printf("SUCCESS\n");
248 /* Free the memory */
249 free(tab_rq_bits);
250 free(tab_rp_bits);
251 free(tab_rq_registers);
252 free(tab_rp_registers);
253 free(tab_rw_rq_registers);
255 /* Close the connection */
256 modbus_close(ctx);
257 modbus_free(ctx);
259 return 0;