From 6cfbf2dee130cc36a72d57adea0e9c04bb31f620 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Wed, 12 Oct 2011 21:50:48 +0100 Subject: Initial commit --- tests/bandwidth-client.c | 220 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 tests/bandwidth-client.c (limited to 'tests/bandwidth-client.c') diff --git a/tests/bandwidth-client.c b/tests/bandwidth-client.c new file mode 100644 index 0000000..e9109e1 --- /dev/null +++ b/tests/bandwidth-client.c @@ -0,0 +1,220 @@ +/* + * Copyright © 2008-2010 Stéphane Raimbault + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#define G_MSEC_PER_SEC 1000 + +uint32_t gettime_ms(void) +{ + struct timeval tv; + gettimeofday (&tv, NULL); + + return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + +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, "Connexion 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