Commit 35c5a230876d6729dac6950225e02b0817deacce
1 parent
07b51bce
- Rename test files
- Separate unit-test-master and test-master-random
Showing
6 changed files
with
124 additions
and
30 deletions
tests/Makefile.am
| 1 | 1 | noinst_PROGRAMS = \ |
| 2 | - test-modbus-master \ | |
| 3 | - test-modbus-slave | |
| 2 | + unit-test-master \ | |
| 3 | + unit-test-slave \ | |
| 4 | + test-master-random | |
| 4 | 5 | |
| 5 | 6 | common_ldflags = \ |
| 6 | 7 | $(top_builddir)/modbus/libmodbus.la |
| 7 | 8 | |
| 8 | -test_modbus_master_SOURCES = test-modbus-master.c | |
| 9 | -test_modbus_master_LDADD = $(common_ldflags) | |
| 9 | +unit_test_master_SOURCES = unit-test-master.c | |
| 10 | +unit_test_master_LDADD = $(common_ldflags) | |
| 10 | 11 | |
| 11 | -test_modbus_slave_SOURCES = test-modbus-slave.c | |
| 12 | -test_modbus_slave_LDADD = $(common_ldflags) | |
| 12 | +unit_test_slave_SOURCES = unit-test-slave.c | |
| 13 | +unit_test_slave_LDADD = $(common_ldflags) | |
| 14 | + | |
| 15 | +test_master_random_SOURCES = test-master-random.c | |
| 16 | +test_master_random_LDADD = $(common_ldflags) | |
| 13 | 17 | |
| 14 | 18 | INCLUDES = -I$(top_srcdir) |
| 15 | 19 | CLEANFILES = *~ | ... | ... |
tests/test-modbus-master.c renamed to tests/test-master-random.c
| 1 | 1 | /* |
| 2 | - * Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com> | |
| 2 | + * Copyright (C) 2008 Stéphane Raimbault <stephane.raimbault@gmail.com> | |
| 3 | 3 | * |
| 4 | 4 | * Licensed under the GNU General Public License Version 2 |
| 5 | 5 | * |
| ... | ... | @@ -24,6 +24,21 @@ |
| 24 | 24 | |
| 25 | 25 | #include <modbus/modbus.h> |
| 26 | 26 | |
| 27 | +/* The goal of this program is to check all major functions of | |
| 28 | + libmodbus: | |
| 29 | + - force_single_coil | |
| 30 | + - read_coil_status | |
| 31 | + - force_multiple_coils | |
| 32 | + - preset_single_register | |
| 33 | + - read_holding_registers | |
| 34 | + - preset_multiple_registers | |
| 35 | + - read_holding_registers | |
| 36 | + | |
| 37 | + All these functions are called with random values on a address | |
| 38 | + range defined by following defines. | |
| 39 | + | |
| 40 | + This program is also really useful to test your remote target unit. | |
| 41 | +*/ | |
| 27 | 42 | #define LOOP 1 |
| 28 | 43 | #define SLAVE 0x11 |
| 29 | 44 | #define ADDR_MIN 0 |
| ... | ... | @@ -42,30 +57,23 @@ int main(void) |
| 42 | 57 | modbus_param_t mb_param; |
| 43 | 58 | |
| 44 | 59 | /* RTU parity : none, even, odd */ |
| 45 | -/* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ | |
| 60 | + /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ | |
| 46 | 61 | |
| 47 | 62 | /* TCP */ |
| 48 | - modbus_init_tcp(&mb_param, "127.0.0.1", 1502); | |
| 63 | + modbus_init_tcp(&mb_param, "192.168.0.100", MODBUS_TCP_DEFAULT_PORT); | |
| 49 | 64 | modbus_set_debug(&mb_param, TRUE); |
| 50 | - | |
| 51 | - modbus_connect(&mb_param); | |
| 52 | 65 | |
| 66 | + modbus_connect(&mb_param);./configu | |
| 67 | + | |
| 68 | + /* Allocate and initialize the different memory spaces */ | |
| 53 | 69 | tab_rq = (int *) malloc(FIELDS * sizeof(int)); |
| 70 | + memset(tab_rq, 0, FIELDS * sizeof(int)); | |
| 71 | + | |
| 54 | 72 | tab_rq_bits = (int *) malloc(FIELDS * sizeof(int)); |
| 55 | - tab_rp = (int *) malloc(FIELDS * sizeof(int)); | |
| 56 | - | |
| 57 | - read_coil_status(&mb_param, SLAVE, 0x13, 0x25, tab_rp); | |
| 58 | - read_input_status(&mb_param, SLAVE, 0xC4, 0x16, tab_rp); | |
| 59 | - read_holding_registers(&mb_param, SLAVE, 0x6B, 3, tab_rp); | |
| 60 | - read_input_registers(&mb_param, SLAVE, 0x8, 1, tab_rp); | |
| 61 | - force_single_coil(&mb_param, SLAVE, 0xAC, ON); | |
| 73 | + memset(tab_rq_bits, 0, FIELDS * sizeof(int)); | |
| 62 | 74 | |
| 63 | - free(tab_rp); | |
| 64 | - free(tab_rq); | |
| 65 | - free(tab_rq_bits); | |
| 66 | - modbus_close(&mb_param); | |
| 67 | - | |
| 68 | - return 0; | |
| 75 | + tab_rp = (int *) malloc(FIELDS * sizeof(int)); | |
| 76 | + memset(tab_rp, 0, FIELDS * sizeof(int)); | |
| 69 | 77 | |
| 70 | 78 | loop_nb = ok = fail = 0; |
| 71 | 79 | while (loop_nb++ < LOOP) { |
| ... | ... | @@ -189,11 +197,14 @@ int main(void) |
| 189 | 197 | } |
| 190 | 198 | } |
| 191 | 199 | |
| 200 | + /* Free the memory */ | |
| 192 | 201 | free(tab_rp); |
| 193 | 202 | free(tab_rq); |
| 194 | 203 | free(tab_rq_bits); |
| 204 | + | |
| 205 | + /* Close the connection */ | |
| 195 | 206 | modbus_close(&mb_param); |
| 196 | 207 | |
| 197 | 208 | return 0; |
| 198 | 209 | } |
| 199 | - | |
| 210 | + | ... | ... |
tests/unit-test-master.c
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com> | |
| 3 | + * | |
| 4 | + * Licensed under the GNU General Public License Version 2 | |
| 5 | + * | |
| 6 | + * This program is free software; you can redistribute it and/or modify | |
| 7 | + * it under the terms of the GNU General Public License as published by | |
| 8 | + * the Free Software Foundation; either version 2 of the License, or | |
| 9 | + * (at your option) any later version. | |
| 10 | + * | |
| 11 | + * This program is distributed in the hope that it will be useful, | |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | + * GNU General Public License for more details. | |
| 15 | + * | |
| 16 | + * You should have received a copy of the GNU General Public License | |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 18 | + */ | |
| 19 | + | |
| 20 | +#include <stdio.h> | |
| 21 | +#include <unistd.h> | |
| 22 | +#include <string.h> | |
| 23 | +#include <stdlib.h> | |
| 24 | + | |
| 25 | +#include <modbus/modbus.h> | |
| 26 | + | |
| 27 | +/* Tests based on PI-MBUS-300 documentation */ | |
| 28 | +#define SLAVE 0x11 | |
| 29 | +#define FIELDS 500 | |
| 30 | + | |
| 31 | +int main(void) | |
| 32 | +{ | |
| 33 | + int *tab_rq; | |
| 34 | + int *tab_rq_bits; | |
| 35 | + int *tab_rp; | |
| 36 | + modbus_param_t mb_param; | |
| 37 | + | |
| 38 | + /* RTU parity : none, even, odd */ | |
| 39 | +/* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ | |
| 40 | + | |
| 41 | + /* TCP */ | |
| 42 | + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); | |
| 43 | + modbus_set_debug(&mb_param, TRUE); | |
| 44 | + | |
| 45 | + modbus_connect(&mb_param); | |
| 46 | + | |
| 47 | + /* Allocate and initialize the different memory spaces */ | |
| 48 | + tab_rq = (int *) malloc(FIELDS * sizeof(int)); | |
| 49 | + memset(tab_rq, 0, FIELDS * sizeof(int)); | |
| 50 | + | |
| 51 | + tab_rq_bits = (int *) malloc(FIELDS * sizeof(int)); | |
| 52 | + memset(tab_rq_bits, 0, FIELDS * sizeof(int)); | |
| 53 | + | |
| 54 | + tab_rp = (int *) malloc(FIELDS * sizeof(int)); | |
| 55 | + memset(tab_rp, 0, FIELDS * sizeof(int)); | |
| 56 | + | |
| 57 | + read_coil_status(&mb_param, SLAVE, 0x13, 0x25, tab_rp); | |
| 58 | + read_input_status(&mb_param, SLAVE, 0xC4, 0x16, tab_rp); | |
| 59 | + read_holding_registers(&mb_param, SLAVE, 0x6B, 3, tab_rp); | |
| 60 | + read_input_registers(&mb_param, SLAVE, 0x8, 1, tab_rp); | |
| 61 | + force_single_coil(&mb_param, SLAVE, 0xAC, ON); | |
| 62 | + | |
| 63 | + /* Free the memory */ | |
| 64 | + free(tab_rp); | |
| 65 | + free(tab_rq); | |
| 66 | + free(tab_rq_bits); | |
| 67 | + | |
| 68 | + /* Close the connection */ | |
| 69 | + modbus_close(&mb_param); | |
| 70 | + | |
| 71 | + return 0; | |
| 72 | +} | ... | ... |
tests/test-modbus-slave.c renamed to tests/unit-test-slave.c
tests/wscript
| 1 | 1 | def build(bld): |
| 2 | 2 | obj = bld.create_obj('cc', 'program') |
| 3 | - obj.source = 'test-modbus-master.c' | |
| 3 | + obj.source = 'unit-test-master.c' | |
| 4 | 4 | obj.includes = '. ..' |
| 5 | 5 | obj.uselib_local = 'modbus' |
| 6 | - obj.target = 'test-modbus-master' | |
| 6 | + obj.target = 'unit-test-master' | |
| 7 | 7 | obj.inst_var = 0 |
| 8 | 8 | |
| 9 | 9 | obj = bld.create_obj('cc', 'program') |
| 10 | - obj.source = 'test-modbus-slave.c' | |
| 10 | + obj.source = 'unit-test-slave.c' | |
| 11 | 11 | obj.includes = '. ..' |
| 12 | 12 | obj.uselib_local = 'modbus' |
| 13 | - obj.target = 'test-modbus-slave' | |
| 13 | + obj.target = 'unit-test-slave' | |
| 14 | + obj.inst_var = 0 | |
| 15 | + | |
| 16 | + obj = bld.create_obj('cc', 'program') | |
| 17 | + obj.source = 'test-master-random.c' | |
| 18 | + obj.includes = '. ..' | |
| 19 | + obj.uselib_local = 'modbus' | |
| 20 | + obj.target = 'test-master-random' | |
| 14 | 21 | obj.inst_var = 0 | ... | ... |
wscript
| ... | ... | @@ -16,7 +16,7 @@ def configure(conf): |
| 16 | 16 | conf.check_tool('compiler_cc') |
| 17 | 17 | conf.check_tool('misc') |
| 18 | 18 | |
| 19 | - headers = 'stdio.h string.h stdlib.h termios.h sys/time.h \ | |
| 19 | + headers = 'termios.h sys/time.h \ | |
| 20 | 20 | unistd.h errno.h limits.h fcntl.h \ |
| 21 | 21 | sys/types.h sys/socket.h sys/ioctl.h \ |
| 22 | 22 | netinet/in.h netinet/ip.h netinet/tcp.h arpa/inet.h' | ... | ... |