mb_master.h
changeset 1 59783e8ee3d2
parent 0 ae252e0fd9b8
equal deleted inserted replaced
0:ae252e0fd9b8 1:59783e8ee3d2
    51 
    51 
    52 
    52 
    53 /* FUNCTION 0x01   - Read Coils
    53 /* FUNCTION 0x01   - Read Coils
    54  * Bits are stored on an int array, one bit per int.
    54  * Bits are stored on an int array, one bit per int.
    55  */
    55  */
    56 inline int read_output_bits(u8  slave,
    56 int read_output_bits(u8  slave,
    57                             u16 start_addr,
    57                      u16 start_addr,
    58                             u16 count,
    58                      u16 count,
    59                             u16 *dest,
    59                      u16 *dest,
    60                             int dest_size,
    60                      int dest_size,
    61                             int fd,
    61                      int fd,
    62                             int send_retries,
    62                      int send_retries,
    63                             u8  *error_code,
    63                      u8  *error_code,
    64                             const struct timespec *response_timeout,
    64                      const struct timespec *response_timeout,
    65                             pthread_mutex_t *data_access_mutex);
    65                      pthread_mutex_t *data_access_mutex);
    66 #define read_coils(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
    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)
    67         read_output_bits(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
    68 
    68 
    69  
    69  
    70 /* FUNCTION 0x01   - Read Coils
    70 /* FUNCTION 0x01   - Read Coils
    71  * Bits are stored on an u32 array, 32 bits per u32.
    71  * Bits are stored on an u32 array, 32 bits per u32.
    72  * Unused bits in last u32 are set to 0.
    72  * Unused bits in last u32 are set to 0.
    73  */
    73  */
    74 inline int read_output_bits_u32(u8  slave,
    74 int read_output_bits_u32(u8  slave,
    75                                 u16 start_addr,
    75                          u16 start_addr,
    76                                 u16 count,
    76                          u16 count,
    77                                 u32 *dest,
    77                          u32 *dest,
    78                                 int fd,
    78                          int fd,
    79                                 int send_retries,
    79                          int send_retries,
    80                                 u8  *error_code,
    80                          u8  *error_code,
    81                                 const struct timespec *response_timeout);
    81                          const struct timespec *response_timeout);
    82 #define read_coils_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
    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)
    83         read_output_bits_u32(p1,p2,p3,p4,p5,p6,p7,p8)
    84 
    84 
    85 
    85 
    86 
    86 
    87 
    87 
    88 /* FUNCTION 0x02   - Read Discrete Inputs
    88 /* FUNCTION 0x02   - Read Discrete Inputs
    89  * Bits are stored on an int array, one bit per int.
    89  * Bits are stored on an int array, one bit per int.
    90  */
    90  */
    91 inline int read_input_bits(u8  slave,
    91 int read_input_bits(u8  slave,
    92                            u16 start_addr,
    92                     u16 start_addr,
    93                            u16 count,
    93                     u16 count,
    94                            u16 *dest,
    94                     u16 *dest,
    95                            int dest_size,
    95                     int dest_size,
    96                            int fd,
    96                     int fd,
    97                            int send_retries,
    97                     int send_retries,
    98                            u8  *error_code,
    98                     u8  *error_code,
    99                            const struct timespec *response_timeout,
    99                     const struct timespec *response_timeout,
   100                            pthread_mutex_t *data_access_mutex);
   100                     pthread_mutex_t *data_access_mutex);
   101 #define read_discrete_inputs(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
   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)
   102         read_input_bits     (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
   103 
   103 
   104 
   104 
   105 /* FUNCTION 0x02   - Read Discrete Inputs
   105 /* FUNCTION 0x02   - Read Discrete Inputs
   106  * Bits are stored on an u32 array, 32 bits per u32.
   106  * Bits are stored on an u32 array, 32 bits per u32.
   107  * Unused bits in last u32 are set to 0.
   107  * Unused bits in last u32 are set to 0.
   108  */
   108  */
   109 inline int read_input_bits_u32(u8  slave,
   109 int read_input_bits_u32(u8  slave,
   110                                u16 start_addr,
   110                         u16 start_addr,
   111                                u16 count,
   111                         u16 count,
   112                                u32 *dest,
   112                         u32 *dest,
   113                                int fd,
   113                         int fd,
   114                                int send_retries,
   114                         int send_retries,
   115                                u8  *error_code,
   115                         u8  *error_code,
   116                                const struct timespec *response_timeout);
   116                         const struct timespec *response_timeout);
   117 #define read_discrete_inputs_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   118         read_input_bits_u32     (p1,p2,p3,p4,p5,p6,p7,p8)
   119 
   119 
   120 
   120 
   121         
   121         
   122 
   122 
   123 /* FUNCTION 0x03   - Read Holding Registers */
   123 /* FUNCTION 0x03   - Read Holding Registers */
   124 inline int read_output_words(u8  slave,
   124 int read_output_words(u8  slave,
   125                              u16 start_addr,
   125                       u16 start_addr,
   126                              u16 count,
   126                       u16 count,
   127                              u16 *dest,
   127                       u16 *dest,
   128                              int dest_size,
   128                       int dest_size,
   129                              int fd,
   129                       int fd,
   130                              int send_retries,
   130                       int send_retries,
   131                              u8  *error_code,
   131                       u8  *error_code,
   132                              const struct timespec *response_timeout,
   132                       const struct timespec *response_timeout,
   133                              pthread_mutex_t *data_access_mutex);
   133                       pthread_mutex_t *data_access_mutex);
   134 #define read_holding_registers(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
   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)
   135         read_output_words     (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
   136 
   136 
   137 
   137 
   138 /* FUNCTION 0x03   - Read Holding Registers
   138 /* FUNCTION 0x03   - Read Holding Registers
   139  * u16 registers are stored in array of u32, two registers per u32.
   139  * u16 registers are stored in array of u32, two registers per u32.
   140  * Unused bits of last u32 element are set to 0.
   140  * Unused bits of last u32 element are set to 0.
   141  */
   141  */
   142 inline int read_output_words_u32(u8  slave,
   142 int read_output_words_u32(u8  slave,
   143                                  u16 start_addr,
   143                           u16 start_addr,
   144                                  u16 count,
   144                           u16 count,
   145                                  u32 *dest,
   145                           u32 *dest,
   146                                  int fd,
   146                           int fd,
   147                                  int send_retries,
   147                           int send_retries,
   148                                  u8  *error_code,
   148                           u8  *error_code,
   149                                  const struct timespec *response_timeout);
   149                           const struct timespec *response_timeout);
   150 #define read_holding_registers_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   151         read_output_words_u32     (p1,p2,p3,p4,p5,p6,p7,p8)
   152 
   152 
   153 
   153 
   154 /* FUNCTION 0x03   - Read Holding Registers
   154 /* FUNCTION 0x03   - Read Holding Registers
   155  * return the array with the data to the calling function
   155  * return the array with the data to the calling function
   156  */
   156  */
   157 inline int read_output_words_u16_ref(u8  slave,
   157 int read_output_words_u16_ref(u8  slave,
   158                                      u16 start_addr,
   158                               u16 start_addr,
   159                                      u16 count,
   159                               u16 count,
   160                                      u16 **dest,
   160                               u16 **dest,
   161                                      int ttyfd,
   161                               int ttyfd,
   162                                      int send_retries,
   162                               int send_retries,
   163                                      u8  *error_code,
   163                               u8  *error_code,
   164                                      const struct timespec *response_timeout);
   164                               const struct timespec *response_timeout);
   165 #define read_holding_registers_u16_ref(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   166         read_output_words_u16_ref     (p1,p2,p3,p4,p5,p6,p7,p8)
   167 
   167 
   168 
   168 
   169 
   169 
   170 /* FUNCTION 0x04   - Read Input Registers */
   170 /* FUNCTION 0x04   - Read Input Registers */
   171 inline int read_input_words(u8  slave,
   171 int read_input_words(u8  slave,
   172                             u16 start_addr,
   172                      u16 start_addr,
   173                             u16 count,
   173                      u16 count,
   174                             u16 *dest,
   174                      u16 *dest,
   175                             int dest_size,
   175                      int dest_size,
   176                             int fd,
   176                      int fd,
   177                             int send_retries,
   177                      int send_retries,
   178                             u8  *error_code,
   178                      u8  *error_code,
   179                             const struct timespec *response_timeout,
   179                      const struct timespec *response_timeout,
   180                             pthread_mutex_t *data_access_mutex);
   180                      pthread_mutex_t *data_access_mutex);
   181 #define read_input_registers(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) \
   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)
   182         read_input_words    (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
   183 
   183 
   184 
   184 
   185 
   185 
   186 /* FUNCTION 0x04   - Read Input Registers
   186 /* FUNCTION 0x04   - Read Input Registers
   187  * u16 registers are stored in array of u32, two registers per u32.
   187  * u16 registers are stored in array of u32, two registers per u32.
   188  * Unused bits of last u32 element are set to 0.
   188  * Unused bits of last u32 element are set to 0.
   189  */
   189  */
   190 inline int read_input_words_u32(u8  slave,
   190 int read_input_words_u32(u8  slave,
   191                                 u16 start_addr,
   191                          u16 start_addr,
   192                                 u16 count,
   192                          u16 count,
   193                                 u32 *dest,
   193                          u32 *dest,
   194                                 int fd,
   194                          int fd,
   195                                 int send_retries,
   195                          int send_retries,
   196                                 u8  *error_code,
   196                          u8  *error_code,
   197                                 const struct timespec *response_timeout);
   197                          const struct timespec *response_timeout);
   198 #define read_input_registers_u32(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   199         read_input_words_u32    (p1,p2,p3,p4,p5,p6,p7,p8)
   200 
   200 
   201 
   201 
   202 
   202 
   203 /* FUNCTION 0x04   - Read Input Registers
   203 /* FUNCTION 0x04   - Read Input Registers
   204  * return the array with the data to the calling function
   204  * return the array with the data to the calling function
   205  */
   205  */
   206 inline int read_input_words_u16_ref(u8  slave,
   206 int read_input_words_u16_ref(u8  slave,
   207                                     u16 start_addr,
   207                              u16 start_addr,
   208                                     u16 count,
   208                              u16 count,
   209                                     u16 **dest,
   209                              u16 **dest,
   210                                     int ttyfd,
   210                              int ttyfd,
   211                                     int send_retries,
   211                              int send_retries,
   212                                     u8  *error_code,
   212                              u8  *error_code,
   213                                     const struct timespec *response_timeout);
   213                              const struct timespec *response_timeout);
   214 #define read_input_registers_u16_ref(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   215         read_input_words_u16_ref    (p1,p2,p3,p4,p5,p6,p7,p8)
   216 
   216 
   217 
   217 
   218 
   218 
   219 
   219 
   220 /* FUNCTION 0x05   - Force Single Coil */
   220 /* FUNCTION 0x05   - Force Single Coil */
   221 inline int write_output_bit(u8  slave,
   221 int write_output_bit(u8  slave,
   222                             u16 coil_addr,
   222                      u16 coil_addr,
   223                             u16 state,
   223                      u16 state,
   224                             int fd,
   224                      int fd,
   225                             int send_retries,
   225                      int send_retries,
   226                             u8  *error_code,
   226                      u8  *error_code,
   227                             const struct timespec *response_timeout,
   227                      const struct timespec *response_timeout,
   228                             pthread_mutex_t *data_access_mutex);
   228                      pthread_mutex_t *data_access_mutex);
   229 #define force_single_coil(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   230         write_output_bit (p1,p2,p3,p4,p5,p6,p7,p8)
   231 
   231 
   232 
   232 
   233 
   233 
   234 
   234 
   235 
   235 
   236 
   236 
   237 /* FUNCTION 0x06   - Write Single Register */
   237 /* FUNCTION 0x06   - Write Single Register */
   238 inline int write_output_word(u8  slave,
   238 int write_output_word(u8  slave,
   239                              u16 reg_addr,
   239                       u16 reg_addr,
   240                              u16 value,
   240                       u16 value,
   241                              int fd,
   241                       int fd,
   242                              int send_retries,
   242                       int send_retries,
   243                              u8  *error_code,
   243                       u8  *error_code,
   244                              const struct timespec *response_timeout,
   244                       const struct timespec *response_timeout,
   245                              pthread_mutex_t *data_access_mutex);
   245                       pthread_mutex_t *data_access_mutex);
   246 #define write_single_register(p1,p2,p3,p4,p5,p6,p7,p8) \
   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)
   247         write_output_word    (p1,p2,p3,p4,p5,p6,p7,p8)
   248 
   248 
   249 
   249 
   250 
   250