bandwidth-client.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2008-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 <time.h>
23 #include <sys/time.h>
24 #include <errno.h>
25 
26 #include <modbus.h>
27 
28 #define G_MSEC_PER_SEC 1000
29 
30 uint32_t gettime_ms(void)
31 {
32  struct timeval tv;
33  gettimeofday (&tv, NULL);
34 
35  return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
36 }
37 
38 enum {
39  TCP,
41 };
42 
43 /* Tests based on PI-MBUS-300 documentation */
44 int main(int argc, char *argv[])
45 {
46  uint8_t *tab_bit;
47  uint16_t *tab_reg;
48  modbus_t *ctx;
49  int i;
50  int nb_points;
51  double elapsed;
52  uint32_t start;
53  uint32_t end;
54  uint32_t bytes;
55  uint32_t rate;
56  int rc;
57  int n_loop;
58  int use_backend;
59 
60  if (argc > 1) {
61  if (strcmp(argv[1], "tcp") == 0) {
62  use_backend = TCP;
63  n_loop = 100000;
64  } else if (strcmp(argv[1], "rtu") == 0) {
65  use_backend = RTU;
66  n_loop = 100;
67  } else {
68  printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n", argv[0]);
69  exit(1);
70  }
71  } else {
72  /* By default */
73  use_backend = TCP;
74  n_loop = 100000;
75  }
76 
77  if (use_backend == TCP) {
78  ctx = modbus_new_tcp("127.0.0.1", 1502);
79  } else {
80  ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
81  modbus_set_slave(ctx, 1);
82  }
83  if (modbus_connect(ctx) == -1) {
84  fprintf(stderr, "Connexion failed: %s\n",
85  modbus_strerror(errno));
86  modbus_free(ctx);
87  return -1;
88  }
89 
90  /* Allocate and initialize the memory to store the status */
91  tab_bit = (uint8_t *) malloc(MODBUS_MAX_READ_BITS * sizeof(uint8_t));
92  memset(tab_bit, 0, MODBUS_MAX_READ_BITS * sizeof(uint8_t));
93 
94  /* Allocate and initialize the memory to store the registers */
95  tab_reg = (uint16_t *) malloc(MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t));
96  memset(tab_reg, 0, MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t));
97 
98  printf("READ BITS\n\n");
99 
100  nb_points = MODBUS_MAX_READ_BITS;
101  start = gettime_ms();
102  for (i=0; i<n_loop; i++) {
103  rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
104  if (rc == -1) {
105  fprintf(stderr, "%s\n", modbus_strerror(errno));
106  return -1;
107  }
108  }
109  end = gettime_ms();
110  elapsed = end - start;
111 
112  rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
113  printf("Transfert rate in points/seconds:\n");
114  printf("* %d points/s\n", rate);
115  printf("\n");
116 
117  bytes = n_loop * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
118  rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
119  printf("Values:\n");
120  printf("* %d x %d values\n", n_loop, nb_points);
121  printf("* %.3f ms for %d bytes\n", elapsed, bytes);
122  printf("* %d KiB/s\n", rate);
123  printf("\n");
124 
125  /* TCP: Query and reponse header and values */
126  bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
127  printf("Values and TCP Modbus overhead:\n");
128  printf("* %d x %d bytes\n", n_loop, bytes);
129  bytes = n_loop * bytes;
130  rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
131  printf("* %.3f ms for %d bytes\n", elapsed, bytes);
132  printf("* %d KiB/s\n", rate);
133  printf("\n\n");
134 
135  printf("READ REGISTERS\n\n");
136 
137  nb_points = MODBUS_MAX_READ_REGISTERS;
138  start = gettime_ms();
139  for (i=0; i<n_loop; i++) {
140  rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
141  if (rc == -1) {
142  fprintf(stderr, "%s\n", modbus_strerror(errno));
143  return -1;
144  }
145  }
146  end = gettime_ms();
147  elapsed = end - start;
148 
149  rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
150  printf("Transfert rate in points/seconds:\n");
151  printf("* %d registers/s\n", rate);
152  printf("\n");
153 
154  bytes = n_loop * nb_points * sizeof(uint16_t);
155  rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
156  printf("Values:\n");
157  printf("* %d x %d values\n", n_loop, nb_points);
158  printf("* %.3f ms for %d bytes\n", elapsed, bytes);
159  printf("* %d KiB/s\n", rate);
160  printf("\n");
161 
162  /* TCP:Query and reponse header and values */
163  bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
164  printf("Values and TCP Modbus overhead:\n");
165  printf("* %d x %d bytes\n", n_loop, bytes);
166  bytes = n_loop * bytes;
167  rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
168  printf("* %.3f ms for %d bytes\n", elapsed, bytes);
169  printf("* %d KiB/s\n", rate);
170  printf("\n\n");
171 
172  printf("READ AND WRITE REGISTERS\n\n");
173 
174  nb_points = MODBUS_MAX_RW_WRITE_REGISTERS;
175  start = gettime_ms();
176  for (i=0; i<n_loop; i++) {
178  0, nb_points, tab_reg,
179  0, nb_points, tab_reg);
180  if (rc == -1) {
181  fprintf(stderr, "%s\n", modbus_strerror(errno));
182  return -1;
183  }
184  }
185  end = gettime_ms();
186  elapsed = end - start;
187 
188  rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
189  printf("Transfert rate in points/seconds:\n");
190  printf("* %d registers/s\n", rate);
191  printf("\n");
192 
193  bytes = n_loop * nb_points * sizeof(uint16_t);
194  rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
195  printf("Values:\n");
196  printf("* %d x %d values\n", n_loop, nb_points);
197  printf("* %.3f ms for %d bytes\n", elapsed, bytes);
198  printf("* %d KiB/s\n", rate);
199  printf("\n");
200 
201  /* TCP:Query and reponse header and values */
202  bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
203  printf("Values and TCP Modbus overhead:\n");
204  printf("* %d x %d bytes\n", n_loop, bytes);
205  bytes = n_loop * bytes;
206  rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
207  printf("* %.3f ms for %d bytes\n", elapsed, bytes);
208  printf("* %d KiB/s\n", rate);
209  printf("\n");
210 
211  /* Free the memory */
212  free(tab_bit);
213  free(tab_reg);
214 
215  /* Close the connection */
216  modbus_close(ctx);
217  modbus_free(ctx);
218 
219  return 0;
220 }
uint32_t gettime_ms(void)
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)
Definition: modbus.c:1059
#define G_MSEC_PER_SEC
int main(int argc, char *argv[])
void modbus_free(modbus_t *ctx)
Definition: modbus.c:1528
#define MODBUS_MAX_READ_REGISTERS
Definition: modbus.h:80
void modbus_close(modbus_t *ctx)
Definition: modbus.c:1520
int modbus_set_slave(modbus_t *ctx, int slave)
Definition: modbus.c:1465
modbus_t * ctx
const char * modbus_strerror(int errnum)
Definition: modbus.c:53
#define MODBUS_MAX_RW_WRITE_REGISTERS
Definition: modbus.h:82
modbus_t * modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit)
Definition: modbus-rtu.c:917
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
#define MODBUS_MAX_READ_BITS
Definition: modbus.h:70
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


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