mb_master.h
changeset 0 ae252e0fd9b8
child 1 59783e8ee3d2
equal deleted inserted replaced
-1:000000000000 0:ae252e0fd9b8
       
     1 /*
       
     2  * Copyright (c) 2001-2003,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_master.h */
       
    25 
       
    26 
       
    27 #ifndef MODBUS_MASTER_H
       
    28 #define MODBUS_MASTER_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 
       
    39 	 Note: All functions used for sending or receiving data via
       
    40 	       modbus return these return values.
       
    41 
       
    42 
       
    43 	Returns:	string_length if OK
       
    44 			-1 on internal error or port failure
       
    45 			-2 on timeout
       
    46 			-3 if a valid yet un-expected frame is received!
       
    47 			-4 for modbus exception errors
       
    48 			   (in this case exception code is returned in *error_code)
       
    49 
       
    50 ***********************************************************************/
       
    51 
       
    52 
       
    53 /* FUNCTION 0x01   - Read Coils
       
    54  * Bits are stored on an int array, one bit per int.
       
    55  */
       
    56 inline int read_output_bits(u8  slave,
       
    57                             u16 start_addr,
       
    58                             u16 count,
       
    59                             u16 *dest,
       
    60                             int dest_size,
       
    61                             int fd,
       
    62                             int send_retries,
       
    63                             u8  *error_code,
       
    64                             const struct timespec *response_timeout,
       
    65                             pthread_mutex_t *data_access_mutex);
       
    66 #define read_coils(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
       
    67         read_output_bits(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
       
    68 
       
    69  
       
    70 /* FUNCTION 0x01   - Read Coils
       
    71  * Bits are stored on an u32 array, 32 bits per u32.
       
    72  * Unused bits in last u32 are set to 0.
       
    73  */
       
    74 inline int read_output_bits_u32(u8  slave,
       
    75                                 u16 start_addr,
       
    76                                 u16 count,
       
    77                                 u32 *dest,
       
    78                                 int fd,
       
    79                                 int send_retries,
       
    80                                 u8  *error_code,
       
    81                                 const struct timespec *response_timeout);
       
    82 #define read_coils_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
       
    83         read_output_bits_u32(p1,p2,p3,p4,p5,p6,p7,p8)
       
    84 
       
    85 
       
    86 
       
    87 
       
    88 /* FUNCTION 0x02   - Read Discrete Inputs
       
    89  * Bits are stored on an int array, one bit per int.
       
    90  */
       
    91 inline int read_input_bits(u8  slave,
       
    92                            u16 start_addr,
       
    93                            u16 count,
       
    94                            u16 *dest,
       
    95                            int dest_size,
       
    96                            int fd,
       
    97                            int send_retries,
       
    98                            u8  *error_code,
       
    99                            const struct timespec *response_timeout,
       
   100                            pthread_mutex_t *data_access_mutex);
       
   101 #define read_discrete_inputs(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
       
   102         read_input_bits     (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
       
   103 
       
   104 
       
   105 /* FUNCTION 0x02   - Read Discrete Inputs
       
   106  * Bits are stored on an u32 array, 32 bits per u32.
       
   107  * Unused bits in last u32 are set to 0.
       
   108  */
       
   109 inline int read_input_bits_u32(u8  slave,
       
   110                                u16 start_addr,
       
   111                                u16 count,
       
   112                                u32 *dest,
       
   113                                int fd,
       
   114                                int send_retries,
       
   115                                u8  *error_code,
       
   116                                const struct timespec *response_timeout);
       
   117 #define read_discrete_inputs_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   118         read_input_bits_u32     (p1,p2,p3,p4,p5,p6,p7,p8)
       
   119 
       
   120 
       
   121         
       
   122 
       
   123 /* FUNCTION 0x03   - Read Holding Registers */
       
   124 inline int read_output_words(u8  slave,
       
   125                              u16 start_addr,
       
   126                              u16 count,
       
   127                              u16 *dest,
       
   128                              int dest_size,
       
   129                              int fd,
       
   130                              int send_retries,
       
   131                              u8  *error_code,
       
   132                              const struct timespec *response_timeout,
       
   133                              pthread_mutex_t *data_access_mutex);
       
   134 #define read_holding_registers(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
       
   135         read_output_words     (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
       
   136 
       
   137 
       
   138 /* FUNCTION 0x03   - Read Holding Registers
       
   139  * u16 registers are stored in array of u32, two registers per u32.
       
   140  * Unused bits of last u32 element are set to 0.
       
   141  */
       
   142 inline int read_output_words_u32(u8  slave,
       
   143                                  u16 start_addr,
       
   144                                  u16 count,
       
   145                                  u32 *dest,
       
   146                                  int fd,
       
   147                                  int send_retries,
       
   148                                  u8  *error_code,
       
   149                                  const struct timespec *response_timeout);
       
   150 #define read_holding_registers_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   151         read_output_words_u32     (p1,p2,p3,p4,p5,p6,p7,p8)
       
   152 
       
   153 
       
   154 /* FUNCTION 0x03   - Read Holding Registers
       
   155  * return the array with the data to the calling function
       
   156  */
       
   157 inline int read_output_words_u16_ref(u8  slave,
       
   158                                      u16 start_addr,
       
   159                                      u16 count,
       
   160                                      u16 **dest,
       
   161                                      int ttyfd,
       
   162                                      int send_retries,
       
   163                                      u8  *error_code,
       
   164                                      const struct timespec *response_timeout);
       
   165 #define read_holding_registers_u16_ref(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   166         read_output_words_u16_ref     (p1,p2,p3,p4,p5,p6,p7,p8)
       
   167 
       
   168 
       
   169 
       
   170 /* FUNCTION 0x04   - Read Input Registers */
       
   171 inline int read_input_words(u8  slave,
       
   172                             u16 start_addr,
       
   173                             u16 count,
       
   174                             u16 *dest,
       
   175                             int dest_size,
       
   176                             int fd,
       
   177                             int send_retries,
       
   178                             u8  *error_code,
       
   179                             const struct timespec *response_timeout,
       
   180                             pthread_mutex_t *data_access_mutex);
       
   181 #define read_input_registers(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
       
   182         read_input_words    (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
       
   183 
       
   184 
       
   185 
       
   186 /* FUNCTION 0x04   - Read Input Registers
       
   187  * u16 registers are stored in array of u32, two registers per u32.
       
   188  * Unused bits of last u32 element are set to 0.
       
   189  */
       
   190 inline int read_input_words_u32(u8  slave,
       
   191                                 u16 start_addr,
       
   192                                 u16 count,
       
   193                                 u32 *dest,
       
   194                                 int fd,
       
   195                                 int send_retries,
       
   196                                 u8  *error_code,
       
   197                                 const struct timespec *response_timeout);
       
   198 #define read_input_registers_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   199         read_input_words_u32    (p1,p2,p3,p4,p5,p6,p7,p8)
       
   200 
       
   201 
       
   202 
       
   203 /* FUNCTION 0x04   - Read Input Registers
       
   204  * return the array with the data to the calling function
       
   205  */
       
   206 inline int read_input_words_u16_ref(u8  slave,
       
   207                                     u16 start_addr,
       
   208                                     u16 count,
       
   209                                     u16 **dest,
       
   210                                     int ttyfd,
       
   211                                     int send_retries,
       
   212                                     u8  *error_code,
       
   213                                     const struct timespec *response_timeout);
       
   214 #define read_input_registers_u16_ref(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   215         read_input_words_u16_ref    (p1,p2,p3,p4,p5,p6,p7,p8)
       
   216 
       
   217 
       
   218 
       
   219 
       
   220 /* FUNCTION 0x05   - Force Single Coil */
       
   221 inline int write_output_bit(u8  slave,
       
   222                             u16 coil_addr,
       
   223                             u16 state,
       
   224                             int fd,
       
   225                             int send_retries,
       
   226                             u8  *error_code,
       
   227                             const struct timespec *response_timeout,
       
   228                             pthread_mutex_t *data_access_mutex);
       
   229 #define force_single_coil(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   230         write_output_bit (p1,p2,p3,p4,p5,p6,p7,p8)
       
   231 
       
   232 
       
   233 
       
   234 
       
   235 
       
   236 
       
   237 /* FUNCTION 0x06   - Write Single Register */
       
   238 inline int write_output_word(u8  slave,
       
   239                              u16 reg_addr,
       
   240                              u16 value,
       
   241                              int fd,
       
   242                              int send_retries,
       
   243                              u8  *error_code,
       
   244                              const struct timespec *response_timeout,
       
   245                              pthread_mutex_t *data_access_mutex);
       
   246 #define write_single_register(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   247         write_output_word    (p1,p2,p3,p4,p5,p6,p7,p8)
       
   248 
       
   249 
       
   250 
       
   251 
       
   252 
       
   253 /* FUNCTION 0x0F   - Force Multiple Coils */
       
   254 int write_output_bits(u8  slave,
       
   255                       u16 start_addr,
       
   256                       u16 coil_count,
       
   257                       u16 *data,
       
   258                       int fd,
       
   259                       int send_retries,
       
   260                       u8  *error_code,
       
   261                       const struct timespec *response_timeout,
       
   262                       pthread_mutex_t *data_access_mutex);
       
   263 #define force_multiple_coils(p1,p2,p3,p4,p5,p6,p7,p8,p9) \
       
   264         write_output_bits   (p1,p2,p3,p4,p5,p6,p7,p8,p9)
       
   265 
       
   266 
       
   267 /* FUNCTION 0x0F   - Force Multiple Coils
       
   268  * Bits should be stored on an u32 array, 32 bits per u32.
       
   269  * Unused bits in last u32 should be set to 0.
       
   270  */
       
   271 int write_output_bits_u32(u8  slave,
       
   272                           u16 start_addr,
       
   273                           u16 coil_count,
       
   274                           u32 *data,
       
   275                           int fd,
       
   276                           int send_retries,
       
   277                           u8  *error_code,
       
   278                           const struct timespec *response_timeout);
       
   279 #define force_multiple_coils_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   280         write_output_bits_u32   (p1,p2,p3,p4,p5,p6,p7,p8)
       
   281 
       
   282 
       
   283 
       
   284 /* FUNCTION 0x10   - Force Multiple Registers */
       
   285 int write_output_words(u8  slave,
       
   286                        u16 start_addr,
       
   287                        u16 reg_count,
       
   288                        u16 *data,
       
   289                        int fd,
       
   290                        int send_retries,
       
   291                        u8  *error_code,
       
   292                        const struct timespec *response_timeout,
       
   293                        pthread_mutex_t *data_access_mutex);
       
   294 #define force_multiple_registers(p1,p2,p3,p4,p5,p6,p7,p8,p9) \
       
   295         write_output_words      (p1,p2,p3,p4,p5,p6,p7,p8,p9)
       
   296 
       
   297 
       
   298 
       
   299 /* FUNCTION 0x10   - Force Multiple Registers
       
   300  * u16 registers are stored in array of u32, two registers per u32.
       
   301  * Unused bits of last u32 element are set to 0.
       
   302  */
       
   303 int write_output_words_u32(u8  slave,
       
   304                            u16 start_addr,
       
   305                            u16 reg_count,
       
   306                            u32 *data,
       
   307                            int fd,
       
   308                            int send_retries,
       
   309                            u8  *error_code,
       
   310                            const struct timespec *response_timeout);
       
   311 
       
   312 #define force_multiple_registers_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
       
   313         write_output_words_u32      (p1,p2,p3,p4,p5,p6,p7,p8)
       
   314 
       
   315 
       
   316 
       
   317 
       
   318 
       
   319 
       
   320 /* Initialise the Modbus Library to work as Master only */
       
   321 int mb_master_init(int nd_count);
       
   322 /* Shut down the Modbus Library */
       
   323 int mb_master_done(void);
       
   324 
       
   325 
       
   326 
       
   327 /* Establish a connection to a remote server/slave.
       
   328  * The address type (naf_tcp, naf_rtu, naf_ascii) specifies the lower
       
   329  * layer to use for the newly opened node.
       
   330  */
       
   331 int mb_master_connect(node_addr_t node_addr);
       
   332 /* Shut down a connection to a remote server/slave */
       
   333 int mb_master_close(int nd);
       
   334 
       
   335 
       
   336 
       
   337 
       
   338 /* Tell the library that communications will be suspended for some time. */
       
   339 /* RTU and ASCII versions ignore this function
       
   340  * TCP version closes all the open tcp connections (connections are automatically
       
   341  *   re-established the next time an IO function to the slave is requested).
       
   342  *   To be more precise, the TCP version makes an estimate of how long
       
   343  *   the silence will be based on previous invocations to this exact same
       
   344  *   function, and will only close the connections if this silence is
       
   345  *   expected to be longer than 1 second!
       
   346  */
       
   347 int mb_master_tcp_silence_init(void);
       
   348 
       
   349 
       
   350 
       
   351 #endif  /* MODBUS_MASTER_H */
       
   352 
       
   353 
       
   354 
       
   355 
       
   356 
       
   357 
       
   358 
       
   359