From 35c5a230876d6729dac6950225e02b0817deacce Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Sat, 5 Apr 2008 12:50:59 +0200 Subject: [PATCH] - Rename test files - Separate unit-test-master and test-master-random --- tests/Makefile.am | 16 ++++++++++------ tests/test-master-random.c | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-modbus-master.c | 199 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tests/test-modbus-slave.c | 113 ----------------------------------------------------------------------------------------------------------------- tests/unit-test-master.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/unit-test-slave.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/wscript | 15 +++++++++++---- wscript | 2 +- 8 files changed, 417 insertions(+), 323 deletions(-) create mode 100644 tests/test-master-random.c delete mode 100644 tests/test-modbus-master.c delete mode 100644 tests/test-modbus-slave.c create mode 100644 tests/unit-test-master.c create mode 100644 tests/unit-test-slave.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 6f0729e..4b9af06 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,15 +1,19 @@ noinst_PROGRAMS = \ - test-modbus-master \ - test-modbus-slave + unit-test-master \ + unit-test-slave \ + test-master-random common_ldflags = \ $(top_builddir)/modbus/libmodbus.la -test_modbus_master_SOURCES = test-modbus-master.c -test_modbus_master_LDADD = $(common_ldflags) +unit_test_master_SOURCES = unit-test-master.c +unit_test_master_LDADD = $(common_ldflags) -test_modbus_slave_SOURCES = test-modbus-slave.c -test_modbus_slave_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) INCLUDES = -I$(top_srcdir) CLEANFILES = *~ diff --git a/tests/test-master-random.c b/tests/test-master-random.c new file mode 100644 index 0000000..719229c --- /dev/null +++ b/tests/test-master-random.c @@ -0,0 +1,210 @@ +/* + * Copyright (C) 2008 Stéphane Raimbault + * + * Licensed under the GNU General Public License Version 2 + * + * 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 2 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 + +/* The goal of this program is to check all major functions of + libmodbus: + - force_single_coil + - read_coil_status + - force_multiple_coils + - preset_single_register + - read_holding_registers + - preset_multiple_registers + - read_holding_registers + + All these functions are called with random values on a address + range defined by following defines. + + This program is also really useful to test your remote target unit. +*/ +#define LOOP 1 +#define SLAVE 0x11 +#define ADDR_MIN 0 +#define ADDR_MAX 499 +#define FIELDS 500 + +int main(void) +{ + int ok, fail; + int loop_nb; + int addr; + int field_nb; + int *tab_rq; + int *tab_rq_bits; + int *tab_rp; + modbus_param_t mb_param; + + /* RTU parity : none, even, odd */ + /* 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_set_debug(&mb_param, TRUE); + + modbus_connect(&mb_param);./configu + + /* Allocate and initialize the different memory spaces */ + tab_rq = (int *) malloc(FIELDS * sizeof(int)); + memset(tab_rq, 0, FIELDS * sizeof(int)); + + tab_rq_bits = (int *) malloc(FIELDS * sizeof(int)); + memset(tab_rq_bits, 0, FIELDS * sizeof(int)); + + tab_rp = (int *) malloc(FIELDS * sizeof(int)); + memset(tab_rp, 0, FIELDS * sizeof(int)); + + loop_nb = ok = fail = 0; + while (loop_nb++ < LOOP) { + for (addr=ADDR_MIN; addr <= ADDR_MAX; addr++) { + for (field_nb=1; field_nb<=FIELDS; field_nb++) { + int i; + + /* Random numbers (short) */ + for (i=0; i - * - * Licensed under the GNU General Public License Version 2 - * - * 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 2 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 - -#define LOOP 1 -#define SLAVE 0x11 -#define ADDR_MIN 0 -#define ADDR_MAX 499 -#define FIELDS 500 - -int main(void) -{ - int ok, fail; - int loop_nb; - int addr; - int field_nb; - int *tab_rq; - int *tab_rq_bits; - int *tab_rp; - modbus_param_t mb_param; - - /* RTU parity : none, even, odd */ -/* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ - - /* TCP */ - modbus_init_tcp(&mb_param, "127.0.0.1", 1502); - modbus_set_debug(&mb_param, TRUE); - - modbus_connect(&mb_param); - - tab_rq = (int *) malloc(FIELDS * sizeof(int)); - tab_rq_bits = (int *) malloc(FIELDS * sizeof(int)); - tab_rp = (int *) malloc(FIELDS * sizeof(int)); - - read_coil_status(&mb_param, SLAVE, 0x13, 0x25, tab_rp); - read_input_status(&mb_param, SLAVE, 0xC4, 0x16, tab_rp); - read_holding_registers(&mb_param, SLAVE, 0x6B, 3, tab_rp); - read_input_registers(&mb_param, SLAVE, 0x8, 1, tab_rp); - force_single_coil(&mb_param, SLAVE, 0xAC, ON); - - free(tab_rp); - free(tab_rq); - free(tab_rq_bits); - modbus_close(&mb_param); - - return 0; - - loop_nb = ok = fail = 0; - while (loop_nb++ < LOOP) { - for (addr=ADDR_MIN; addr <= ADDR_MAX; addr++) { - for (field_nb=1; field_nb<=FIELDS; field_nb++) { - int i; - - /* Random numbers (short) */ - for (i=0; i - * - * Licensed under the GNU General Public License Version 2 - * - * 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 2 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 - -int main(void) -{ - const int nb_coil_status = 500; - const int nb_input_status = 500; - const int nb_input_registers = 500; - const int nb_holding_registers = 500; - int socket; - modbus_param_t mb_param; - modbus_mapping_t mb_mapping; - int ret; - int i; - - modbus_init_tcp(&mb_param, "127.0.0.1", 1502); - modbus_set_debug(&mb_param, TRUE); - - modbus_mapping_new(&mb_mapping, - nb_coil_status, nb_input_status, - nb_input_registers, nb_holding_registers); - - /* Examples from PI_MODBUS_300.pdf */ - - /* Coil status */ - mb_mapping.tab_coil_status[26] = ON; - mb_mapping.tab_coil_status[25] = ON; - mb_mapping.tab_coil_status[24] = OFF; - mb_mapping.tab_coil_status[23] = OFF; - mb_mapping.tab_coil_status[22] = ON; - mb_mapping.tab_coil_status[21] = ON; - mb_mapping.tab_coil_status[20] = OFF; - mb_mapping.tab_coil_status[19] = ON; - - /* Input status */ - mb_mapping.tab_input_status[203] = ON; - mb_mapping.tab_input_status[202] = OFF; - mb_mapping.tab_input_status[201] = ON; - mb_mapping.tab_input_status[200] = OFF; - mb_mapping.tab_input_status[199] = ON; - mb_mapping.tab_input_status[198] = ON; - mb_mapping.tab_input_status[197] = OFF; - mb_mapping.tab_input_status[196] = OFF; - - mb_mapping.tab_input_status[211] = ON; - mb_mapping.tab_input_status[210] = ON; - mb_mapping.tab_input_status[209] = OFF; - mb_mapping.tab_input_status[208] = ON; - mb_mapping.tab_input_status[207] = ON; - mb_mapping.tab_input_status[206] = OFF; - mb_mapping.tab_input_status[205] = ON; - mb_mapping.tab_input_status[204] = ON; - - /* Incomplete byte */ - mb_mapping.tab_input_status[217] = ON; - mb_mapping.tab_input_status[216] = ON; - mb_mapping.tab_input_status[215] = OFF; - mb_mapping.tab_input_status[214] = ON; - mb_mapping.tab_input_status[213] = OFF; - mb_mapping.tab_input_status[212] = ON; - - /* Holding registers */ - mb_mapping.tab_holding_registers[107] = 0x022B; - mb_mapping.tab_holding_registers[108] = 0x0000; - mb_mapping.tab_holding_registers[109] = 0x0064; - - /* Input registers */ - mb_mapping.tab_input_registers[8] = 0x000A; - - socket = modbus_init_listen_tcp(&mb_param); - - i = 0; - while (i++ < 5) { - unsigned char query[MAX_PACKET_SIZE]; - int query_size; - - ret = modbus_listen(&mb_param, query, &query_size); - if (ret == 0) - manage_query(&mb_param, query, query_size, &mb_mapping); - 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 new file mode 100644 index 0000000..e76ddab --- /dev/null +++ b/tests/unit-test-master.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2001-2008 Stéphane Raimbault + * + * Licensed under the GNU General Public License Version 2 + * + * 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 2 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 + +/* Tests based on PI-MBUS-300 documentation */ +#define SLAVE 0x11 +#define FIELDS 500 + +int main(void) +{ + int *tab_rq; + int *tab_rq_bits; + int *tab_rp; + modbus_param_t mb_param; + + /* RTU parity : none, even, odd */ +/* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ + + /* TCP */ + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); + modbus_set_debug(&mb_param, TRUE); + + modbus_connect(&mb_param); + + /* Allocate and initialize the different memory spaces */ + tab_rq = (int *) malloc(FIELDS * sizeof(int)); + memset(tab_rq, 0, FIELDS * sizeof(int)); + + tab_rq_bits = (int *) malloc(FIELDS * sizeof(int)); + memset(tab_rq_bits, 0, FIELDS * sizeof(int)); + + tab_rp = (int *) malloc(FIELDS * sizeof(int)); + memset(tab_rp, 0, FIELDS * sizeof(int)); + + read_coil_status(&mb_param, SLAVE, 0x13, 0x25, tab_rp); + read_input_status(&mb_param, SLAVE, 0xC4, 0x16, tab_rp); + read_holding_registers(&mb_param, SLAVE, 0x6B, 3, tab_rp); + read_input_registers(&mb_param, SLAVE, 0x8, 1, tab_rp); + force_single_coil(&mb_param, SLAVE, 0xAC, ON); + + /* Free the memory */ + free(tab_rp); + free(tab_rq); + free(tab_rq_bits); + + /* Close the connection */ + modbus_close(&mb_param); + + return 0; +} diff --git a/tests/unit-test-slave.c b/tests/unit-test-slave.c new file mode 100644 index 0000000..bc6e348 --- /dev/null +++ b/tests/unit-test-slave.c @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2001-2008 Stéphane Raimbault + * + * Licensed under the GNU General Public License Version 2 + * + * 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 2 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 + +int main(void) +{ + const int nb_coil_status = 500; + const int nb_input_status = 500; + const int nb_input_registers = 500; + const int nb_holding_registers = 500; + int socket; + modbus_param_t mb_param; + modbus_mapping_t mb_mapping; + int ret; + int i; + + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); + modbus_set_debug(&mb_param, TRUE); + + modbus_mapping_new(&mb_mapping, + nb_coil_status, nb_input_status, + nb_input_registers, nb_holding_registers); + + /* Examples from PI_MODBUS_300.pdf */ + + /* Coil status */ + mb_mapping.tab_coil_status[26] = ON; + mb_mapping.tab_coil_status[25] = ON; + mb_mapping.tab_coil_status[24] = OFF; + mb_mapping.tab_coil_status[23] = OFF; + mb_mapping.tab_coil_status[22] = ON; + mb_mapping.tab_coil_status[21] = ON; + mb_mapping.tab_coil_status[20] = OFF; + mb_mapping.tab_coil_status[19] = ON; + + /* Input status */ + mb_mapping.tab_input_status[203] = ON; + mb_mapping.tab_input_status[202] = OFF; + mb_mapping.tab_input_status[201] = ON; + mb_mapping.tab_input_status[200] = OFF; + mb_mapping.tab_input_status[199] = ON; + mb_mapping.tab_input_status[198] = ON; + mb_mapping.tab_input_status[197] = OFF; + mb_mapping.tab_input_status[196] = OFF; + + mb_mapping.tab_input_status[211] = ON; + mb_mapping.tab_input_status[210] = ON; + mb_mapping.tab_input_status[209] = OFF; + mb_mapping.tab_input_status[208] = ON; + mb_mapping.tab_input_status[207] = ON; + mb_mapping.tab_input_status[206] = OFF; + mb_mapping.tab_input_status[205] = ON; + mb_mapping.tab_input_status[204] = ON; + + /* Incomplete byte */ + mb_mapping.tab_input_status[217] = ON; + mb_mapping.tab_input_status[216] = ON; + mb_mapping.tab_input_status[215] = OFF; + mb_mapping.tab_input_status[214] = ON; + mb_mapping.tab_input_status[213] = OFF; + mb_mapping.tab_input_status[212] = ON; + + /* Holding registers */ + mb_mapping.tab_holding_registers[107] = 0x022B; + mb_mapping.tab_holding_registers[108] = 0x0000; + mb_mapping.tab_holding_registers[109] = 0x0064; + + /* Input registers */ + mb_mapping.tab_input_registers[8] = 0x000A; + + socket = modbus_init_listen_tcp(&mb_param); + + i = 0; + while (i++ < 5) { + unsigned char query[MAX_PACKET_SIZE]; + int query_size; + + ret = modbus_listen(&mb_param, query, &query_size); + if (ret == 0) + manage_query(&mb_param, query, query_size, &mb_mapping); + 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/wscript b/tests/wscript index a0a7649..33718e8 100644 --- a/tests/wscript +++ b/tests/wscript @@ -1,14 +1,21 @@ def build(bld): obj = bld.create_obj('cc', 'program') - obj.source = 'test-modbus-master.c' + obj.source = 'unit-test-master.c' obj.includes = '. ..' obj.uselib_local = 'modbus' - obj.target = 'test-modbus-master' + obj.target = 'unit-test-master' obj.inst_var = 0 obj = bld.create_obj('cc', 'program') - obj.source = 'test-modbus-slave.c' + obj.source = 'unit-test-slave.c' obj.includes = '. ..' obj.uselib_local = 'modbus' - obj.target = 'test-modbus-slave' + obj.target = 'unit-test-slave' + obj.inst_var = 0 + + obj = bld.create_obj('cc', 'program') + obj.source = 'test-master-random.c' + obj.includes = '. ..' + obj.uselib_local = 'modbus' + obj.target = 'test-master-random' obj.inst_var = 0 diff --git a/wscript b/wscript index a54c0af..a73597e 100644 --- a/wscript +++ b/wscript @@ -16,7 +16,7 @@ def configure(conf): conf.check_tool('compiler_cc') conf.check_tool('misc') - headers = 'stdio.h string.h stdlib.h termios.h sys/time.h \ + headers = 'termios.h sys/time.h \ unistd.h errno.h limits.h fcntl.h \ sys/types.h sys/socket.h sys/ioctl.h \ netinet/in.h netinet/ip.h netinet/tcp.h arpa/inet.h' -- libgit2 0.21.4