Selaa lähdekoodia

New random-test-slave
- rename test-master-random => random-test-master
- fix memset
- update README
- add the files to configure.ac and wscript

Stéphane Raimbault 17 vuotta sitten
vanhempi
commit
3948142c51
7 muutettua tiedostoa jossa 144 lisäystä ja 60 poistoa
  1. 10 6
      tests/Makefile.am
  2. 7 10
      tests/README
  3. 4 2
      tests/bench-bandwidth-master.c
  4. 45 37
      tests/random-test-master.c
  5. 63 0
      tests/random-test-slave.c
  6. 4 1
      tests/unit-test-master.c
  7. 11 4
      tests/wscript

+ 10 - 6
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)

+ 7 - 10
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

+ 4 - 2
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));

+ 45 - 37
tests/test-master-random.c → 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<nb_points_total; i++) {
+                        for (i=0; i<nb; i++) {
                                 tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
                                 tab_rq_status[i] = tab_rq_registers[i] % 2;
+                                printf("%d = %d\n", i, tab_rq_status[i]);
                         }
-                        nb_points = ADDRESS_END - addr;
+                        nb = ADDRESS_END - addr;
 
                         /* SINGLE COIL */
                         ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]);
@@ -109,21 +113,23 @@ int main(void)
                         }
 
                         /* MULTIPLE COILS */
-                        ret = force_multiple_coils(&mb_param, SLAVE, addr, nb_points, tab_rq_status);
-                        if (ret != nb_points) {
+                        ret = force_multiple_coils(&mb_param, SLAVE, addr, nb, tab_rq_status);
+                        if (ret != nb) {
                                 printf("ERROR force_multiple_coils (%d)\n", ret);
-                                printf("Slave = %d, address = %d, nb_points = %d\n",
-                                       SLAVE, addr, nb_points);
+                                printf("Slave = %d, address = %d, nb = %d\n",
+                                       SLAVE, addr, nb);
                                 nb_fail++;
                         } else {
-                                ret = read_coil_status(&mb_param, SLAVE, addr, nb_points, tab_rp_status);
-                                if (ret != nb_points) {
+                                ret = read_coil_status(&mb_param, SLAVE, addr, nb, tab_rp_status);
+                                if (ret != nb) {
                                         printf("ERROR read_coil_status\n");
-                                        printf("Slave = %d, address = %d, nb_points = %d\n",
-                                               SLAVE, addr, nb_points);
+                                        printf("Slave = %d, address = %d, nb = %d\n",
+                                               SLAVE, addr, nb);
                                         nb_fail++;
                                 } else {
-                                        for (i=0; i<nb_points; i++) {
+                                        for (i=0; i<nb; i++) {
+                                                printf("i = %d, tab_rp_status[i] = %d, tab_rq_status[i] = %d\n",
+                                                       i, tab_rp_status[i], tab_rq_status[i]);
                                                 if (tab_rp_status[i] != tab_rq_status[i]) {
                                                         printf("ERROR read_coil_status ");
                                                         printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]);
@@ -135,6 +141,8 @@ int main(void)
                                 }
                         }
 
+                        break;
+
                         /* SINGLE REGISTER */
                         ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]);
                         if (ret != 1) {
@@ -164,22 +172,22 @@ int main(void)
                         
                         /* MULTIPLE REGISTERS */
                         ret = preset_multiple_registers(&mb_param, SLAVE,
-                                                        addr, nb_points, tab_rq_registers);
-                        if (ret != nb_points) {
+                                                        addr, nb, tab_rq_registers);
+                        if (ret != nb) {
                                 printf("ERROR preset_multiple_registers (%d)\n", ret);
-                                printf("Slave = %d, address = %d, nb_points = %d\n",
-                                               SLAVE, addr, nb_points);
+                                printf("Slave = %d, address = %d, nb = %d\n",
+                                               SLAVE, addr, nb);
                                 nb_fail++;
                         } else {
                                 ret = read_holding_registers(&mb_param, SLAVE,
-                                                             addr, nb_points, tab_rp_registers);
-                                if (ret != nb_points) {
+                                                             addr, nb, tab_rp_registers);
+                                if (ret != nb) {
                                         printf("ERROR read_holding_registers (%d)\n", ret);
-                                        printf("Slave = %d, address = %d, nb_points = %d\n",
-                                               SLAVE, addr, nb_points);
+                                        printf("Slave = %d, address = %d, nb = %d\n",
+                                               SLAVE, addr, nb);
                                         nb_fail++;
                                 } else {
-                                        for (i=0; i<nb_points; i++) {
+                                        for (i=0; i<nb; i++) {
                                                 if (tab_rq_registers[i] != tab_rp_registers[i]) {
                                                         printf("ERROR read_holding_registers ");
                                                         printf("(%d != %d)\n",
@@ -200,10 +208,10 @@ int main(void)
         }
 
         /* Free the memory */
-        free(tab_rp_status);                                           
         free(tab_rq_status);
-        free(tab_rp_registers);
+        free(tab_rp_status);                                           
         free(tab_rq_registers);
+        free(tab_rp_registers);
 
         /* Close the connection */
         modbus_close(&mb_param);

+ 63 - 0
tests/random-test-slave.c

@@ -0,0 +1,63 @@
+/*
+ * Copyright © 2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <modbus/modbus.h>
+
+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;
+}
+        

+ 4 - 1
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) ?

+ 11 - 4
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')