diff --git a/tests/Makefile.am b/tests/Makefile.am index fe1cca8..3dae213 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,21 +1,25 @@ noinst_PROGRAMS = \ - unit-test-master \ + random-test-slave \ + random-test-master \ unit-test-slave \ - test-master-random \ + unit-test-master \ bench-bandwidth-slave \ bench-bandwidth-master common_ldflags = \ $(top_builddir)/modbus/libmodbus.la -unit_test_master_SOURCES = unit-test-master.c -unit_test_master_LDADD = $(common_ldflags) +random_test_slave_SOURCES = random-test-slave.c +random_test_slave_LDADD = $(common_ldflags) + +random_test_master_SOURCES = random-test-master.c +random_test_master_LDADD = $(common_ldflags) unit_test_slave_SOURCES = unit-test-slave.c unit_test_slave_LDADD = $(common_ldflags) -test_master_random_SOURCES = test-master-random.c -test_master_random_LDADD = $(common_ldflags) +unit_test_master_SOURCES = unit-test-master.c +unit_test_master_LDADD = $(common_ldflags) bench_bandwidth_slave_SOURCES = bench-bandwidth-slave.c bench_bandwidth_slave_LDADD = $(common_ldflags) diff --git a/tests/README b/tests/README index f1a1b87..a2a1d52 100644 --- a/tests/README +++ b/tests/README @@ -1,15 +1,16 @@ -test-master-random +random-test-slave +----------------- +It's necessary to launch this server before run random-test-master. By +default, it receives and responses to Modbus query on the localhost +and port 1502. + +random-test-master ------------------ This programm sends many different queries to a large range of addresses and values to test the communication between the master and the slave. unit-test-slave ---------------- -It's necessary to launch this server before run unit-test-master. By -default, it receives and responses to Modbus query on the localhost -and port 1502. - unit-test-master ---------------- By default, this program sends some queries with the values defined in @@ -17,10 +18,6 @@ unit-test.h and checks the responses. These programs are useful to test the protocol implementation. bench-bandwidth-slave ---------------------- -It's a program very similar to unit-test-slave and works with -bench-bandwidth-master. - bench-bandwidth-master ---------------------- It returns some very useful informations about the performance of diff --git a/tests/bench-bandwidth-master.c b/tests/bench-bandwidth-master.c index e23d502..1d5b32f 100644 --- a/tests/bench-bandwidth-master.c +++ b/tests/bench-bandwidth-master.c @@ -53,8 +53,10 @@ int main(void) /* TCP */ modbus_init_tcp(&mb_param, "127.0.0.1", 1502); - - modbus_connect(&mb_param); + if (modbus_connect(&mb_param) == -1) { + printf("ERROR Connection failed\n"); + exit(1); + } /* Allocate and initialize the memory to store the status */ tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t)); diff --git a/tests/test-master-random.c b/tests/random-test-master.c index 5ed4b20..f51326c 100644 --- a/tests/test-master-random.c +++ b/tests/random-test-master.c @@ -38,7 +38,7 @@ #define LOOP 1 #define SLAVE 0x11 #define ADDRESS_START 0 -#define ADDRESS_END 499 +#define ADDRESS_END 3 /* At each loop, the program works in the range ADDRESS_START to * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on. @@ -49,7 +49,7 @@ int main(void) int nb_fail; int nb_loop; int addr; - int nb_points_total; + int nb; uint8_t *tab_rq_status; uint8_t *tab_rp_status; uint16_t *tab_rq_registers; @@ -60,36 +60,40 @@ int main(void) /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ /* TCP */ - modbus_init_tcp(&mb_param, "192.168.0.100", MODBUS_TCP_DEFAULT_PORT); + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); modbus_set_debug(&mb_param, TRUE); - - modbus_connect(&mb_param); + if (modbus_connect(&mb_param) == -1) { + printf("ERROR Connection failed\n"); + exit(1); + } /* Allocate and initialize the different memory spaces */ - nb_points_total = ADDRESS_END - ADDRESS_START; + nb = ADDRESS_END - ADDRESS_START; + + tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t)); + memset(tab_rq_status, 0, nb * sizeof(uint8_t)); - tab_rq_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t)); - memset(tab_rq_status, 0, nb_points_total * sizeof(uint8_t)); - tab_rp_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t)); - memset(tab_rp_status, 0, nb_points_total * sizeof(uint8_t)); + tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t)); + memset(tab_rp_status, 0, nb * sizeof(uint8_t)); - tab_rq_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t)); - memset(tab_rq_registers, 0, nb_points_total * sizeof(uint16_t)); - tab_rp_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t)); - memset(tab_rp_status, 0, nb_points_total * sizeof(uint16_t)); + tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t)); + memset(tab_rq_registers, 0, nb * sizeof(uint16_t)); + + tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t)); + memset(tab_rp_registers, 0, nb * sizeof(uint16_t)); nb_loop = nb_fail = 0; while (nb_loop++ < LOOP) { for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) { int i; - int nb_points; /* Random numbers (short) */ - for (i=0; i + * + * 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 + +int main(void) +{ + int socket; + modbus_param_t mb_param; + modbus_mapping_t mb_mapping; + int ret; + + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); + modbus_set_debug(&mb_param, TRUE); + + ret = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500); + if (ret == FALSE) { + printf("Memory allocation failed\n"); + exit(1); + } + + socket = modbus_init_listen_tcp(&mb_param); + + while (1) { + uint8_t query[MAX_MESSAGE_LENGTH]; + int query_size; + + ret = modbus_listen(&mb_param, query, &query_size); + if (ret == 0) { + manage_query(&mb_param, query, query_size, &mb_mapping); + } else if (ret == CONNECTION_CLOSED) { + /* Connection closed by the client, end of server */ + break; + } else { + printf("Error in modbus_listen (%d)\n", ret); + } + } + + close(socket); + modbus_mapping_free(&mb_mapping); + modbus_close(&mb_param); + + return 0; +} + diff --git a/tests/unit-test-master.c b/tests/unit-test-master.c index 6f2389d..df6da7b 100644 --- a/tests/unit-test-master.c +++ b/tests/unit-test-master.c @@ -45,7 +45,10 @@ int main(void) modbus_init_tcp(&mb_param, "127.0.0.1", 1502); /* modbus_set_debug(&mb_param, TRUE);*/ - modbus_connect(&mb_param); + if (modbus_connect(&mb_param) == -1) { + printf("ERROR Connection failed\n"); + exit(1); + } /* Allocate and initialize the memory to store the status */ nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ? diff --git a/tests/wscript b/tests/wscript index 68fa73b..8aa7ec7 100644 --- a/tests/wscript +++ b/tests/wscript @@ -1,9 +1,16 @@ def build(bld): obj = bld.create_obj('cc', 'program') - obj.source = 'unit-test-master.c' + obj.source = 'random-test-slave.c' obj.includes = '. ..' obj.uselib_local = 'modbus' - obj.target = 'unit-test-master' + obj.target = 'random-test-slave' + obj.inst_var = 0 + + obj = bld.create_obj('cc', 'program') + obj.source = 'random-test-master.c' + obj.includes = '. ..' + obj.uselib_local = 'modbus' + obj.target = 'random-test-master' obj.inst_var = 0 obj = bld.create_obj('cc', 'program') @@ -14,10 +21,10 @@ def build(bld): obj.inst_var = 0 obj = bld.create_obj('cc', 'program') - obj.source = 'test-master-random.c' + obj.source = 'unit-test-master.c' obj.includes = '. ..' obj.uselib_local = 'modbus' - obj.target = 'test-master-random' + obj.target = 'unit-test-master' obj.inst_var = 0 obj = bld.create_obj('cc', 'program')