Commit 13a5501386760886d34ef6b1c478d8cdabc64e69

Authored by Stéphane Raimbault
1 parent d3aecacf

New examples programs (slave and master)

src/Makefile.am
1 1 lib_LTLIBRARIES = libmodbus.la
2 2 libmodbus_la_SOURCES = modbus.c modbus.h
3 3  
4   -bin_PROGRAMS = test-modbus
5   -test_modbus_SOURCES = test-modbus.c
6   -test_modbus_INCLUDES = @GLIB_CFLAGS@
7   -test_modbus_LDADD = libmodbus.la @GLIB_LIBS@
  4 +bin_PROGRAMS = test-modbus-master test-modbus-slave
  5 +test_modbus_master_SOURCES = test-modbus-master.c
  6 +test_modbus_master_INCLUDES = @GLIB_CFLAGS@
  7 +test_modbus_master_LDADD = libmodbus.la @GLIB_LIBS@
  8 +
  9 +test_modbus_slave_SOURCES = test-modbus-slave.c
  10 +test_modbus_slave_INCLUDES = @GLIB_CFLAGS@
  11 +test_modbus_slave_LDADD = libmodbus.la @GLIB_LIBS@
8 12  
9 13 INCLUDES = @GLIB_CFLAGS@
10 14 LDADD = @GLIB_LIBS@
... ...
src/test-modbus.c renamed to src/test-modbus-master.c
1 1 /*
2   - Copyright (C) 2001-2005 Stéphane Raimbault <stephane.raimbault@free.fr>
  2 + Copyright (C) 2001-2007 Stéphane Raimbault <stephane.raimbault@gmail.com>
3 3  
4 4 This library is free software; you can redistribute it and/or
5 5 modify it under the terms of the GNU Lesser General Public
... ... @@ -26,11 +26,11 @@
26 26  
27 27 #include <modbus.h>
28 28  
29   -#define LOOP 1
30   -#define SLAVE 1
31   -#define ADDR_MIN 100
32   -#define ADDR_MAX 150
33   -#define FIELDS 50
  29 +#define LOOP 1
  30 +#define SLAVE 0x11
  31 +#define ADDR_MIN 0
  32 +#define ADDR_MAX 499
  33 +#define FIELDS 500
34 34  
35 35 int main(void)
36 36 {
... ... @@ -47,7 +47,7 @@ int main(void)
47 47 /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
48 48  
49 49 /* TCP */
50   - modbus_init_tcp(&mb_param, "192.168.0.100");
  50 + modbus_init_tcp(&mb_param, "169.254.7.104");
51 51 modbus_set_debug(&mb_param, TRUE);
52 52  
53 53 modbus_connect(&mb_param);
... ... @@ -56,6 +56,19 @@ int main(void)
56 56 tab_rq_bits = (int *) malloc(FIELDS * sizeof(int));
57 57 tab_rp = (int *) malloc(FIELDS * sizeof(int));
58 58  
  59 + read_coil_status(&mb_param, SLAVE, 0x13, 0x25, tab_rp);
  60 + read_input_status(&mb_param, SLAVE, 0xC4, 0x16, tab_rp);
  61 + read_holding_registers(&mb_param, SLAVE, 0x6B, 3, tab_rp);
  62 + read_input_registers(&mb_param, SLAVE, 0x8, 1, tab_rp);
  63 + force_single_coil(&mb_param, SLAVE, 0xAC, ON);
  64 +
  65 + free(tab_rp);
  66 + free(tab_rq);
  67 + free(tab_rq_bits);
  68 + modbus_close(&mb_param);
  69 +
  70 + return 0;
  71 +
59 72 loop_nb = ok = fail = 0;
60 73 while (loop_nb++ < LOOP) {
61 74 for (addr=ADDR_MIN; addr <= ADDR_MAX; addr++) {
... ...
src/test-modbus-slave.c 0 → 100644
  1 +/*
  2 + Copyright (C) 2001-2007 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3 +
  4 + This library is free software; you can redistribute it and/or
  5 + modify it under the terms of the GNU Lesser General Public
  6 + License as published by the Free Software Foundation; either
  7 + version 2 of the License, or (at your option) any later version.
  8 +
  9 + This library 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 GNU
  12 + Lesser General Public License for more details.
  13 +
  14 + You should have received a copy of the GNU Lesser General Public
  15 + License along with this library; if not, write to the Free Software
  16 + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  17 +
  18 + These library of functions are designed to enable a program send and
  19 + receive data from a device that communicates using the Modbus protocol.
  20 +*/
  21 +
  22 +#include <stdio.h>
  23 +#include <unistd.h>
  24 +#include <string.h>
  25 +#include <stdlib.h>
  26 +
  27 +#include <modbus.h>
  28 +
  29 +int main(void)
  30 +{
  31 + const int nb_coil_status = 500;
  32 + const int nb_input_status = 500;
  33 + const int nb_input_registers = 500;
  34 + const int nb_holding_registers = 500;
  35 + int socket;
  36 + modbus_param_t mb_param;
  37 + modbus_mapping_t mb_mapping;
  38 + int ret;
  39 + int i;
  40 +
  41 + modbus_init_tcp(&mb_param, "127.0.0.1");
  42 + modbus_set_debug(&mb_param, TRUE);
  43 +
  44 + modbus_mapping_new(&mb_mapping,
  45 + nb_coil_status, nb_input_status, nb_input_registers, nb_holding_registers);
  46 +
  47 + /* Examples from PI_MODBUS_300.pdf */
  48 +
  49 + /* Coil status */
  50 + mb_mapping.tab_coil_status[26] = ON;
  51 + mb_mapping.tab_coil_status[25] = ON;
  52 + mb_mapping.tab_coil_status[24] = OFF;
  53 + mb_mapping.tab_coil_status[23] = OFF;
  54 + mb_mapping.tab_coil_status[22] = ON;
  55 + mb_mapping.tab_coil_status[21] = ON;
  56 + mb_mapping.tab_coil_status[20] = OFF;
  57 + mb_mapping.tab_coil_status[19] = ON;
  58 +
  59 + /* Input status */
  60 + mb_mapping.tab_input_status[203] = ON;
  61 + mb_mapping.tab_input_status[202] = OFF;
  62 + mb_mapping.tab_input_status[201] = ON;
  63 + mb_mapping.tab_input_status[200] = OFF;
  64 + mb_mapping.tab_input_status[199] = ON;
  65 + mb_mapping.tab_input_status[198] = ON;
  66 + mb_mapping.tab_input_status[197] = OFF;
  67 + mb_mapping.tab_input_status[196] = OFF;
  68 +
  69 + mb_mapping.tab_input_status[211] = ON;
  70 + mb_mapping.tab_input_status[210] = ON;
  71 + mb_mapping.tab_input_status[209] = OFF;
  72 + mb_mapping.tab_input_status[208] = ON;
  73 + mb_mapping.tab_input_status[207] = ON;
  74 + mb_mapping.tab_input_status[206] = OFF;
  75 + mb_mapping.tab_input_status[205] = ON;
  76 + mb_mapping.tab_input_status[204] = ON;
  77 +
  78 + /* Incomplete byte */
  79 + mb_mapping.tab_input_status[217] = ON;
  80 + mb_mapping.tab_input_status[216] = ON;
  81 + mb_mapping.tab_input_status[215] = OFF;
  82 + mb_mapping.tab_input_status[214] = ON;
  83 + mb_mapping.tab_input_status[213] = OFF;
  84 + mb_mapping.tab_input_status[212] = ON;
  85 +
  86 + /* Holding registers */
  87 + mb_mapping.tab_holding_registers[107] = 0x022B;
  88 + mb_mapping.tab_holding_registers[108] = 0x0000;
  89 + mb_mapping.tab_holding_registers[109] = 0x0064;
  90 +
  91 + /* Input registers */
  92 + mb_mapping.tab_input_registers[8] = 0x000A;
  93 +
  94 + socket = modbus_init_listen_tcp(&mb_param);
  95 +
  96 + i =0;
  97 + while (i++ < 5) {
  98 + unsigned char query[MAX_PACKET_SIZE];
  99 + int query_size;
  100 + int i;
  101 +
  102 + ret = modbus_listen(&mb_param, query, &query_size);
  103 + if (ret == 0)
  104 + manage_query(&mb_param, query, query_size, &mb_mapping);
  105 + else
  106 + printf("Error in modbus_listen (%d)\n", ret);
  107 + }
  108 +
  109 + close(socket);
  110 + modbus_mapping_free(&mb_mapping);
  111 + modbus_close(&mb_param);
  112 +
  113 + return 0;
  114 +}
  115 +
... ...