random-test-client.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2001-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 
24 #include <modbus.h>
25 
26 /* The goal of this program is to check all major functions of
27  libmodbus:
28  - write_coil
29  - read_bits
30  - write_coils
31  - write_register
32  - read_registers
33  - write_registers
34  - read_registers
35 
36  All these functions are called with random values on a address
37  range defined by the following defines.
38 */
39 #define LOOP 1
40 #define SERVER_ID 17
41 #define ADDRESS_START 0
42 #define ADDRESS_END 99
43 
44 /* At each loop, the program works in the range ADDRESS_START to
45  * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
46  */
47 int main(void)
48 {
49  modbus_t *ctx;
50  int rc;
51  int nb_fail;
52  int nb_loop;
53  int addr;
54  int nb;
55  uint8_t *tab_rq_bits;
56  uint8_t *tab_rp_bits;
57  uint16_t *tab_rq_registers;
58  uint16_t *tab_rw_rq_registers;
59  uint16_t *tab_rp_registers;
60 
61  /* RTU */
62 /*
63  ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
64  modbus_set_slave(ctx, SERVER_ID);
65 */
66 
67  /* TCP */
68  ctx = modbus_new_tcp("127.0.0.1", 1502);
69  modbus_set_debug(ctx, TRUE);
70 
71  if (modbus_connect(ctx) == -1) {
72  fprintf(stderr, "Connection failed: %s\n",
73  modbus_strerror(errno));
74  modbus_free(ctx);
75  return -1;
76  }
77 
78  /* Allocate and initialize the different memory spaces */
80 
81  tab_rq_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
82  memset(tab_rq_bits, 0, nb * sizeof(uint8_t));
83 
84  tab_rp_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
85  memset(tab_rp_bits, 0, nb * sizeof(uint8_t));
86 
87  tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
88  memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
89 
90  tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
91  memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
92 
93  tab_rw_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
94  memset(tab_rw_rq_registers, 0, nb * sizeof(uint16_t));
95 
96  nb_loop = nb_fail = 0;
97  while (nb_loop++ < LOOP) {
98  for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
99  int i;
100 
101  /* Random numbers (short) */
102  for (i=0; i<nb; i++) {
103  tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
104  tab_rw_rq_registers[i] = ~tab_rq_registers[i];
105  tab_rq_bits[i] = tab_rq_registers[i] % 2;
106  }
107  nb = ADDRESS_END - addr;
108 
109  /* WRITE BIT */
110  rc = modbus_write_bit(ctx, addr, tab_rq_bits[0]);
111  if (rc != 1) {
112  printf("ERROR modbus_write_bit (%d)\n", rc);
113  printf("Address = %d, value = %d\n", addr, tab_rq_bits[0]);
114  nb_fail++;
115  } else {
116  rc = modbus_read_bits(ctx, addr, 1, tab_rp_bits);
117  if (rc != 1 || tab_rq_bits[0] != tab_rp_bits[0]) {
118  printf("ERROR modbus_read_bits single (%d)\n", rc);
119  printf("address = %d\n", addr);
120  nb_fail++;
121  }
122  }
123 
124  /* MULTIPLE BITS */
125  rc = modbus_write_bits(ctx, addr, nb, tab_rq_bits);
126  if (rc != nb) {
127  printf("ERROR modbus_write_bits (%d)\n", rc);
128  printf("Address = %d, nb = %d\n", addr, nb);
129  nb_fail++;
130  } else {
131  rc = modbus_read_bits(ctx, addr, nb, tab_rp_bits);
132  if (rc != nb) {
133  printf("ERROR modbus_read_bits\n");
134  printf("Address = %d, nb = %d\n", addr, nb);
135  nb_fail++;
136  } else {
137  for (i=0; i<nb; i++) {
138  if (tab_rp_bits[i] != tab_rq_bits[i]) {
139  printf("ERROR modbus_read_bits\n");
140  printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
141  addr, tab_rq_bits[i], tab_rq_bits[i],
142  tab_rp_bits[i], tab_rp_bits[i]);
143  nb_fail++;
144  }
145  }
146  }
147  }
148 
149  /* SINGLE REGISTER */
150  rc = modbus_write_register(ctx, addr, tab_rq_registers[0]);
151  if (rc != 1) {
152  printf("ERROR modbus_write_register (%d)\n", rc);
153  printf("Address = %d, value = %d (0x%X)\n",
154  addr, tab_rq_registers[0], tab_rq_registers[0]);
155  nb_fail++;
156  } else {
157  rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
158  if (rc != 1) {
159  printf("ERROR modbus_read_registers single (%d)\n", rc);
160  printf("Address = %d\n", addr);
161  nb_fail++;
162  } else {
163  if (tab_rq_registers[0] != tab_rp_registers[0]) {
164  printf("ERROR modbus_read_registers single\n");
165  printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n",
166  addr, tab_rq_registers[0], tab_rq_registers[0],
167  tab_rp_registers[0], tab_rp_registers[0]);
168  nb_fail++;
169  }
170  }
171  }
172 
173  /* MULTIPLE REGISTERS */
174  rc = modbus_write_registers(ctx, addr, nb, tab_rq_registers);
175  if (rc != nb) {
176  printf("ERROR modbus_write_registers (%d)\n", rc);
177  printf("Address = %d, nb = %d\n", addr, nb);
178  nb_fail++;
179  } else {
180  rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
181  if (rc != nb) {
182  printf("ERROR modbus_read_registers (%d)\n", rc);
183  printf("Address = %d, nb = %d\n", addr, nb);
184  nb_fail++;
185  } else {
186  for (i=0; i<nb; i++) {
187  if (tab_rq_registers[i] != tab_rp_registers[i]) {
188  printf("ERROR modbus_read_registers\n");
189  printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
190  addr, tab_rq_registers[i], tab_rq_registers[i],
191  tab_rp_registers[i], tab_rp_registers[i]);
192  nb_fail++;
193  }
194  }
195  }
196  }
197  /* R/W MULTIPLE REGISTERS */
199  addr, nb, tab_rw_rq_registers,
200  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, tab_rp_registers[i], tab_rw_rq_registers[i],
211  tab_rp_registers[i], tab_rw_rq_registers[i]);
212  nb_fail++;
213  }
214  }
215 
216  rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
217  if (rc != nb) {
218  printf("ERROR modbus_read_registers (%d)\n", rc);
219  printf("Address = %d, nb = %d\n", addr, nb);
220  nb_fail++;
221  } else {
222  for (i=0; i<nb; i++) {
223  if (tab_rw_rq_registers[i] != tab_rp_registers[i]) {
224  printf("ERROR modbus_read_and_write_registers WRITE\n");
225  printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
226  addr, tab_rw_rq_registers[i], tab_rw_rq_registers[i],
227  tab_rp_registers[i], tab_rp_registers[i]);
228  nb_fail++;
229  }
230  }
231  }
232  }
233  }
234 
235  printf("Test: ");
236  if (nb_fail)
237  printf("%d FAILS\n", nb_fail);
238  else
239  printf("SUCCESS\n");
240  }
241 
242  /* Free the memory */
243  free(tab_rq_bits);
244  free(tab_rp_bits);
245  free(tab_rq_registers);
246  free(tab_rp_registers);
247  free(tab_rw_rq_registers);
248 
249  /* Close the connection */
250  modbus_close(ctx);
251  modbus_free(ctx);
252 
253  return 0;
254 }
#define ADDRESS_END
#define LOOP
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)
Definition: modbus.c:1059
void modbus_free(modbus_t *ctx)
Definition: modbus.c:1528
void modbus_set_debug(modbus_t *ctx, int boolean)
Definition: modbus.c:1537
void modbus_close(modbus_t *ctx)
Definition: modbus.c:1520
int main(void)
int modbus_write_register(modbus_t *ctx, int reg_addr, int value)
Definition: modbus.c:1225
#define ADDRESS_START
modbus_t * ctx
const char * modbus_strerror(int errnum)
Definition: modbus.c:53
int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data)
Definition: modbus.c:1290
int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data)
Definition: modbus.c:1231
#define TRUE
Definition: modbus.h:52
int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)
Definition: modbus.c:1153
int modbus_connect(modbus_t *ctx)
Definition: modbus.c:1515
int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb, const uint16_t *src, int read_addr, int read_nb, uint16_t *dest)
Definition: modbus.c:1336
modbus_t * modbus_new_tcp(const char *ip_address, int port)
Definition: modbus-tcp.c:636
int modbus_write_bit(modbus_t *ctx, int coil_addr, int status)
Definition: modbus.c:1218


libmodbus
Author(s):
autogenerated on Sat Nov 21 2020 03:17:32