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