mb_slave.h
changeset 0 ae252e0fd9b8
equal deleted inserted replaced
-1:000000000000 0:ae252e0fd9b8
       
     1 /*
       
     2  * Copyright (c) 2001,2016 Mario de Sousa (msousa@fe.up.pt)
       
     3  *
       
     4  * This file is part of the Modbus library for Beremiz and matiec.
       
     5  *
       
     6  * This Modbus library is free software: you can redistribute it and/or modify
       
     7  * it under the terms of the GNU Lesser General Public License as published by
       
     8  * the Free Software Foundation, either version 3 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, but
       
    12  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
       
    14  * General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public License
       
    17  * along with this Modbus library.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  * This code is made available on the understanding that it will not be
       
    20  * used in safety-critical situations without a full and competent review.
       
    21  */
       
    22 
       
    23 
       
    24 /* mb_slave.h */
       
    25 
       
    26 
       
    27 #ifndef MODBUS_SLAVE_H
       
    28 #define MODBUS_SLAVE_H
       
    29 
       
    30 #include <time.h> /* struct timespec data structure */
       
    31 
       
    32 #include "mb_types.h" /* get the data types */
       
    33 #include "mb_addr.h"  /* get definition of common variable types and error codes */
       
    34 
       
    35 
       
    36 
       
    37 
       
    38 /* Initialise the Modbus Library to work as Slave/Server only */
       
    39 int mb_slave_init(int nd_count);
       
    40 /* Shut down the Modbus Library */
       
    41 int mb_slave_done(void);
       
    42 
       
    43 
       
    44 
       
    45 
       
    46 /* Create a new slave/server...
       
    47  * 
       
    48  * This function creates a new node used to:
       
    49  *   - accept connection requests (TCP version)
       
    50  *   - receive frames from masters (RTU and ASCII versions)
       
    51  * 
       
    52  * The type of address (naf_tcp, naf_rtu, naf_ascii) will specify the lower
       
    53  * layer of modbus that will be used by the newly opened node.
       
    54  */
       
    55 int mb_slave_new(node_addr_t node_addr);
       
    56 /* close a node crreated by mb_slave_new() */
       
    57 int mb_slave_close(int nd);
       
    58 
       
    59 
       
    60 
       
    61 
       
    62 /***********************************************/
       
    63 /***********************************************/
       
    64 /**                                           **/
       
    65 /**               Run the slave               **/
       
    66 /**                                           **/
       
    67 /***********************************************/
       
    68 /***********************************************/
       
    69 
       
    70 
       
    71 /* The following functions must return:
       
    72  *  -2  on attempt to read invalid address 
       
    73  *  -1  on all other errors... 
       
    74  *   0  on success.
       
    75  *
       
    76  *  Start_addr may start from 0, to 65535!
       
    77  *  In other words, we use 0 based addressing!  
       
    78  */ 
       
    79 typedef struct {
       
    80   int (*read_inbits)   (void *arg, u16 start_addr, u16 bit_count,  u8  *data_bytes); /* bits are packed into bytes... */
       
    81   int (*read_outbits)  (void *arg, u16 start_addr, u16 bit_count,  u8  *data_bytes); /* bits are packed into bytes... */
       
    82   int (*write_outbits) (void *arg, u16 start_addr, u16 bit_count,  u8  *data_bytes); /* bits are packed into bytes... */
       
    83   int (*read_inwords)  (void *arg, u16 start_addr, u16 word_count, u16 *data_words);
       
    84   int (*read_outwords) (void *arg, u16 start_addr, u16 word_count, u16 *data_words);
       
    85   int (*write_outwords)(void *arg, u16 start_addr, u16 word_count, u16 *data_words);
       
    86   void *arg;
       
    87  } mb_slave_callback_t;
       
    88 
       
    89 /* Execute the Slave and process requests coming from masters...
       
    90  * This function enters an infinite loop wating for new connection requests, 
       
    91  * and for modbus requests over previoulsy open connections...
       
    92  *
       
    93  * The frames are read from:
       
    94  *   -  the node descriptor nd, if nd >= 0
       
    95  *       When using TCP, if the referenced node nd was created to listen for new connections
       
    96  *       [mb_slave_listen()], then this function will also reply to Modbus data requests arriving
       
    97  *       on other nodes that were created as a consequence of accepting connections requests to
       
    98  *       the referenced node nd.
       
    99  *       All other nodes are ignored!
       
   100  *       
       
   101  *   -  any valid and initialised TCP node descriptor, if nd = -1
       
   102  *      In this case, will also accept connection requests arriving from a previously
       
   103  *       created node to listen for new connection requests [mb_slave_listen() ].
       
   104  *      NOTE: (only avaliable if using TCP)
       
   105  * 
       
   106  * slaveid identifies the address (RTU and ASCII) or slaveid (TCP) that we implement.
       
   107  *     Any requests that we receive sent with a slaveid different
       
   108  *     than the one specified, and also different to 0, will be silently ignored!
       
   109  *     Whatever the slaveid specified, we always reply to requests
       
   110  *     to slaveid 0 (the modbus broadcast address).
       
   111  *     Calling this function with a slaveid of 0 means to ignore this 
       
   112  *     parameter and to reply to all requests (whatever the slaveid
       
   113  *     used in the request). This should mostly be used by TCP servers... 
       
   114  */
       
   115 int mb_slave_run(int nd, mb_slave_callback_t callback_functions, u8 slaveid);
       
   116 
       
   117 
       
   118 
       
   119 
       
   120 #endif  /* MODBUS_SLAVE_H */
       
   121 
       
   122 
       
   123 
       
   124 
       
   125 
       
   126 
       
   127 
       
   128