Commit 3948142c515d33490b762e902f39b56ca2656035

Authored by Stéphane Raimbault
1 parent 4bc60984

New random-test-slave

- rename test-master-random => random-test-master
- fix memset
- update README
- add the files to configure.ac and wscript
tests/Makefile.am
1 noinst_PROGRAMS = \ 1 noinst_PROGRAMS = \
2 - unit-test-master \ 2 + random-test-slave \
  3 + random-test-master \
3 unit-test-slave \ 4 unit-test-slave \
4 - test-master-random \ 5 + unit-test-master \
5 bench-bandwidth-slave \ 6 bench-bandwidth-slave \
6 bench-bandwidth-master 7 bench-bandwidth-master
7 8
8 common_ldflags = \ 9 common_ldflags = \
9 $(top_builddir)/modbus/libmodbus.la 10 $(top_builddir)/modbus/libmodbus.la
10 11
11 -unit_test_master_SOURCES = unit-test-master.c  
12 -unit_test_master_LDADD = $(common_ldflags) 12 +random_test_slave_SOURCES = random-test-slave.c
  13 +random_test_slave_LDADD = $(common_ldflags)
  14 +
  15 +random_test_master_SOURCES = random-test-master.c
  16 +random_test_master_LDADD = $(common_ldflags)
13 17
14 unit_test_slave_SOURCES = unit-test-slave.c 18 unit_test_slave_SOURCES = unit-test-slave.c
15 unit_test_slave_LDADD = $(common_ldflags) 19 unit_test_slave_LDADD = $(common_ldflags)
16 20
17 -test_master_random_SOURCES = test-master-random.c  
18 -test_master_random_LDADD = $(common_ldflags) 21 +unit_test_master_SOURCES = unit-test-master.c
  22 +unit_test_master_LDADD = $(common_ldflags)
19 23
20 bench_bandwidth_slave_SOURCES = bench-bandwidth-slave.c 24 bench_bandwidth_slave_SOURCES = bench-bandwidth-slave.c
21 bench_bandwidth_slave_LDADD = $(common_ldflags) 25 bench_bandwidth_slave_LDADD = $(common_ldflags)
tests/README
1 -test-master-random 1 +random-test-slave
  2 +-----------------
  3 +It's necessary to launch this server before run random-test-master. By
  4 +default, it receives and responses to Modbus query on the localhost
  5 +and port 1502.
  6 +
  7 +random-test-master
2 ------------------ 8 ------------------
3 This programm sends many different queries to a large range of 9 This programm sends many different queries to a large range of
4 addresses and values to test the communication between the master and 10 addresses and values to test the communication between the master and
5 the slave. 11 the slave.
6 12
7 unit-test-slave 13 unit-test-slave
8 ----------------  
9 -It's necessary to launch this server before run unit-test-master. By  
10 -default, it receives and responses to Modbus query on the localhost  
11 -and port 1502.  
12 -  
13 unit-test-master 14 unit-test-master
14 ---------------- 15 ----------------
15 By default, this program sends some queries with the values defined in 16 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 @@ -17,10 +18,6 @@ unit-test.h and checks the responses. These programs are useful to
17 test the protocol implementation. 18 test the protocol implementation.
18 19
19 bench-bandwidth-slave 20 bench-bandwidth-slave
20 ----------------------  
21 -It's a program very similar to unit-test-slave and works with  
22 -bench-bandwidth-master.  
23 -  
24 bench-bandwidth-master 21 bench-bandwidth-master
25 ---------------------- 22 ----------------------
26 It returns some very useful informations about the performance of 23 It returns some very useful informations about the performance of
tests/bench-bandwidth-master.c
@@ -53,8 +53,10 @@ int main(void) @@ -53,8 +53,10 @@ int main(void)
53 53
54 /* TCP */ 54 /* TCP */
55 modbus_init_tcp(&mb_param, "127.0.0.1", 1502); 55 modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
56 -  
57 - modbus_connect(&mb_param); 56 + if (modbus_connect(&mb_param) == -1) {
  57 + printf("ERROR Connection failed\n");
  58 + exit(1);
  59 + }
58 60
59 /* Allocate and initialize the memory to store the status */ 61 /* Allocate and initialize the memory to store the status */
60 tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t)); 62 tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t));
tests/test-master-random.c renamed to tests/random-test-master.c
@@ -38,7 +38,7 @@ @@ -38,7 +38,7 @@
38 #define LOOP 1 38 #define LOOP 1
39 #define SLAVE 0x11 39 #define SLAVE 0x11
40 #define ADDRESS_START 0 40 #define ADDRESS_START 0
41 -#define ADDRESS_END 499 41 +#define ADDRESS_END 3
42 42
43 /* At each loop, the program works in the range ADDRESS_START to 43 /* At each loop, the program works in the range ADDRESS_START to
44 * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on. 44 * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
@@ -49,7 +49,7 @@ int main(void) @@ -49,7 +49,7 @@ int main(void)
49 int nb_fail; 49 int nb_fail;
50 int nb_loop; 50 int nb_loop;
51 int addr; 51 int addr;
52 - int nb_points_total; 52 + int nb;
53 uint8_t *tab_rq_status; 53 uint8_t *tab_rq_status;
54 uint8_t *tab_rp_status; 54 uint8_t *tab_rp_status;
55 uint16_t *tab_rq_registers; 55 uint16_t *tab_rq_registers;
@@ -60,36 +60,40 @@ int main(void) @@ -60,36 +60,40 @@ int main(void)
60 /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ 60 /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
61 61
62 /* TCP */ 62 /* TCP */
63 - modbus_init_tcp(&mb_param, "192.168.0.100", MODBUS_TCP_DEFAULT_PORT); 63 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
64 modbus_set_debug(&mb_param, TRUE); 64 modbus_set_debug(&mb_param, TRUE);
65 -  
66 - modbus_connect(&mb_param); 65 + if (modbus_connect(&mb_param) == -1) {
  66 + printf("ERROR Connection failed\n");
  67 + exit(1);
  68 + }
67 69
68 /* Allocate and initialize the different memory spaces */ 70 /* Allocate and initialize the different memory spaces */
69 - nb_points_total = ADDRESS_END - ADDRESS_START; 71 + nb = ADDRESS_END - ADDRESS_START;
  72 +
  73 + tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
  74 + memset(tab_rq_status, 0, nb * sizeof(uint8_t));
70 75
71 - tab_rq_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t));  
72 - memset(tab_rq_status, 0, nb_points_total * sizeof(uint8_t));  
73 - tab_rp_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t));  
74 - memset(tab_rp_status, 0, nb_points_total * sizeof(uint8_t)); 76 + tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
  77 + memset(tab_rp_status, 0, nb * sizeof(uint8_t));
75 78
76 - tab_rq_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t));  
77 - memset(tab_rq_registers, 0, nb_points_total * sizeof(uint16_t));  
78 - tab_rp_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t));  
79 - memset(tab_rp_status, 0, nb_points_total * sizeof(uint16_t)); 79 + tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  80 + memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
  81 +
  82 + tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  83 + memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
80 84
81 nb_loop = nb_fail = 0; 85 nb_loop = nb_fail = 0;
82 while (nb_loop++ < LOOP) { 86 while (nb_loop++ < LOOP) {
83 for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) { 87 for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
84 int i; 88 int i;
85 - int nb_points;  
86 89
87 /* Random numbers (short) */ 90 /* Random numbers (short) */
88 - for (i=0; i<nb_points_total; i++) { 91 + for (i=0; i<nb; i++) {
89 tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0)); 92 tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
90 tab_rq_status[i] = tab_rq_registers[i] % 2; 93 tab_rq_status[i] = tab_rq_registers[i] % 2;
  94 + printf("%d = %d\n", i, tab_rq_status[i]);
91 } 95 }
92 - nb_points = ADDRESS_END - addr; 96 + nb = ADDRESS_END - addr;
93 97
94 /* SINGLE COIL */ 98 /* SINGLE COIL */
95 ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]); 99 ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]);
@@ -109,21 +113,23 @@ int main(void) @@ -109,21 +113,23 @@ int main(void)
109 } 113 }
110 114
111 /* MULTIPLE COILS */ 115 /* MULTIPLE COILS */
112 - ret = force_multiple_coils(&mb_param, SLAVE, addr, nb_points, tab_rq_status);  
113 - if (ret != nb_points) { 116 + ret = force_multiple_coils(&mb_param, SLAVE, addr, nb, tab_rq_status);
  117 + if (ret != nb) {
114 printf("ERROR force_multiple_coils (%d)\n", ret); 118 printf("ERROR force_multiple_coils (%d)\n", ret);
115 - printf("Slave = %d, address = %d, nb_points = %d\n",  
116 - SLAVE, addr, nb_points); 119 + printf("Slave = %d, address = %d, nb = %d\n",
  120 + SLAVE, addr, nb);
117 nb_fail++; 121 nb_fail++;
118 } else { 122 } else {
119 - ret = read_coil_status(&mb_param, SLAVE, addr, nb_points, tab_rp_status);  
120 - if (ret != nb_points) { 123 + ret = read_coil_status(&mb_param, SLAVE, addr, nb, tab_rp_status);
  124 + if (ret != nb) {
121 printf("ERROR read_coil_status\n"); 125 printf("ERROR read_coil_status\n");
122 - printf("Slave = %d, address = %d, nb_points = %d\n",  
123 - SLAVE, addr, nb_points); 126 + printf("Slave = %d, address = %d, nb = %d\n",
  127 + SLAVE, addr, nb);
124 nb_fail++; 128 nb_fail++;
125 } else { 129 } else {
126 - for (i=0; i<nb_points; i++) { 130 + for (i=0; i<nb; i++) {
  131 + printf("i = %d, tab_rp_status[i] = %d, tab_rq_status[i] = %d\n",
  132 + i, tab_rp_status[i], tab_rq_status[i]);
127 if (tab_rp_status[i] != tab_rq_status[i]) { 133 if (tab_rp_status[i] != tab_rq_status[i]) {
128 printf("ERROR read_coil_status "); 134 printf("ERROR read_coil_status ");
129 printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]); 135 printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]);
@@ -135,6 +141,8 @@ int main(void) @@ -135,6 +141,8 @@ int main(void)
135 } 141 }
136 } 142 }
137 143
  144 + break;
  145 +
138 /* SINGLE REGISTER */ 146 /* SINGLE REGISTER */
139 ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]); 147 ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]);
140 if (ret != 1) { 148 if (ret != 1) {
@@ -164,22 +172,22 @@ int main(void) @@ -164,22 +172,22 @@ int main(void)
164 172
165 /* MULTIPLE REGISTERS */ 173 /* MULTIPLE REGISTERS */
166 ret = preset_multiple_registers(&mb_param, SLAVE, 174 ret = preset_multiple_registers(&mb_param, SLAVE,
167 - addr, nb_points, tab_rq_registers);  
168 - if (ret != nb_points) { 175 + addr, nb, tab_rq_registers);
  176 + if (ret != nb) {
169 printf("ERROR preset_multiple_registers (%d)\n", ret); 177 printf("ERROR preset_multiple_registers (%d)\n", ret);
170 - printf("Slave = %d, address = %d, nb_points = %d\n",  
171 - SLAVE, addr, nb_points); 178 + printf("Slave = %d, address = %d, nb = %d\n",
  179 + SLAVE, addr, nb);
172 nb_fail++; 180 nb_fail++;
173 } else { 181 } else {
174 ret = read_holding_registers(&mb_param, SLAVE, 182 ret = read_holding_registers(&mb_param, SLAVE,
175 - addr, nb_points, tab_rp_registers);  
176 - if (ret != nb_points) { 183 + addr, nb, tab_rp_registers);
  184 + if (ret != nb) {
177 printf("ERROR read_holding_registers (%d)\n", ret); 185 printf("ERROR read_holding_registers (%d)\n", ret);
178 - printf("Slave = %d, address = %d, nb_points = %d\n",  
179 - SLAVE, addr, nb_points); 186 + printf("Slave = %d, address = %d, nb = %d\n",
  187 + SLAVE, addr, nb);
180 nb_fail++; 188 nb_fail++;
181 } else { 189 } else {
182 - for (i=0; i<nb_points; i++) { 190 + for (i=0; i<nb; i++) {
183 if (tab_rq_registers[i] != tab_rp_registers[i]) { 191 if (tab_rq_registers[i] != tab_rp_registers[i]) {
184 printf("ERROR read_holding_registers "); 192 printf("ERROR read_holding_registers ");
185 printf("(%d != %d)\n", 193 printf("(%d != %d)\n",
@@ -200,10 +208,10 @@ int main(void) @@ -200,10 +208,10 @@ int main(void)
200 } 208 }
201 209
202 /* Free the memory */ 210 /* Free the memory */
203 - free(tab_rp_status);  
204 free(tab_rq_status); 211 free(tab_rq_status);
205 - free(tab_rp_registers); 212 + free(tab_rp_status);
206 free(tab_rq_registers); 213 free(tab_rq_registers);
  214 + free(tab_rp_registers);
207 215
208 /* Close the connection */ 216 /* Close the connection */
209 modbus_close(&mb_param); 217 modbus_close(&mb_param);
tests/random-test-slave.c 0 → 100644
  1 +/*
  2 + * Copyright © 2008 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 <stdlib.h>
  21 +
  22 +#include <modbus/modbus.h>
  23 +
  24 +int main(void)
  25 +{
  26 + int socket;
  27 + modbus_param_t mb_param;
  28 + modbus_mapping_t mb_mapping;
  29 + int ret;
  30 +
  31 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  32 + modbus_set_debug(&mb_param, TRUE);
  33 +
  34 + ret = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500);
  35 + if (ret == FALSE) {
  36 + printf("Memory allocation failed\n");
  37 + exit(1);
  38 + }
  39 +
  40 + socket = modbus_init_listen_tcp(&mb_param);
  41 +
  42 + while (1) {
  43 + uint8_t query[MAX_MESSAGE_LENGTH];
  44 + int query_size;
  45 +
  46 + ret = modbus_listen(&mb_param, query, &query_size);
  47 + if (ret == 0) {
  48 + manage_query(&mb_param, query, query_size, &mb_mapping);
  49 + } else if (ret == CONNECTION_CLOSED) {
  50 + /* Connection closed by the client, end of server */
  51 + break;
  52 + } else {
  53 + printf("Error in modbus_listen (%d)\n", ret);
  54 + }
  55 + }
  56 +
  57 + close(socket);
  58 + modbus_mapping_free(&mb_mapping);
  59 + modbus_close(&mb_param);
  60 +
  61 + return 0;
  62 +}
  63 +
tests/unit-test-master.c
@@ -45,7 +45,10 @@ int main(void) @@ -45,7 +45,10 @@ int main(void)
45 modbus_init_tcp(&mb_param, "127.0.0.1", 1502); 45 modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
46 /* modbus_set_debug(&mb_param, TRUE);*/ 46 /* modbus_set_debug(&mb_param, TRUE);*/
47 47
48 - modbus_connect(&mb_param); 48 + if (modbus_connect(&mb_param) == -1) {
  49 + printf("ERROR Connection failed\n");
  50 + exit(1);
  51 + }
49 52
50 /* Allocate and initialize the memory to store the status */ 53 /* Allocate and initialize the memory to store the status */
51 nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ? 54 nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
tests/wscript
1 def build(bld): 1 def build(bld):
2 obj = bld.create_obj('cc', 'program') 2 obj = bld.create_obj('cc', 'program')
3 - obj.source = 'unit-test-master.c' 3 + obj.source = 'random-test-slave.c'
4 obj.includes = '. ..' 4 obj.includes = '. ..'
5 obj.uselib_local = 'modbus' 5 obj.uselib_local = 'modbus'
6 - obj.target = 'unit-test-master' 6 + obj.target = 'random-test-slave'
  7 + obj.inst_var = 0
  8 +
  9 + obj = bld.create_obj('cc', 'program')
  10 + obj.source = 'random-test-master.c'
  11 + obj.includes = '. ..'
  12 + obj.uselib_local = 'modbus'
  13 + obj.target = 'random-test-master'
7 obj.inst_var = 0 14 obj.inst_var = 0
8 15
9 obj = bld.create_obj('cc', 'program') 16 obj = bld.create_obj('cc', 'program')
@@ -14,10 +21,10 @@ def build(bld): @@ -14,10 +21,10 @@ def build(bld):
14 obj.inst_var = 0 21 obj.inst_var = 0
15 22
16 obj = bld.create_obj('cc', 'program') 23 obj = bld.create_obj('cc', 'program')
17 - obj.source = 'test-master-random.c' 24 + obj.source = 'unit-test-master.c'
18 obj.includes = '. ..' 25 obj.includes = '. ..'
19 obj.uselib_local = 'modbus' 26 obj.uselib_local = 'modbus'
20 - obj.target = 'test-master-random' 27 + obj.target = 'unit-test-master'
21 obj.inst_var = 0 28 obj.inst_var = 0
22 29
23 obj = bld.create_obj('cc', 'program') 30 obj = bld.create_obj('cc', 'program')