modbus-private.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2010-2012 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef _MODBUS_PRIVATE_H_
20 #define _MODBUS_PRIVATE_H_
21 
22 #ifndef _MSC_VER
23 # include <stdint.h>
24 # include <sys/time.h>
25 #else
26 # include "stdint.h"
27 # include <time.h>
28 typedef int ssize_t;
29 #endif
30 #include <sys/types.h>
31 #include <libmodbus/config.h>
32 
33 #include "libmodbus/modbus.h"
34 
36 
37 /* It's not really the minimal length (the real one is report slave ID
38  * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
39  * communications to read many values or write a single one.
40  * Maximum between :
41  * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2)
42  * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2)
43  */
44 #define _MIN_REQ_LENGTH 12
45 
46 #define _REPORT_SLAVE_ID 180
47 
48 #define _MODBUS_EXCEPTION_RSP_LENGTH 5
49 
50 /* Timeouts in microsecond (0.5 s) */
51 #define _RESPONSE_TIMEOUT 500000
52 #define _BYTE_TIMEOUT 500000
53 
54 /* Function codes */
55 #define _FC_READ_COILS 0x01
56 #define _FC_READ_DISCRETE_INPUTS 0x02
57 #define _FC_READ_HOLDING_REGISTERS 0x03
58 #define _FC_READ_INPUT_REGISTERS 0x04
59 #define _FC_WRITE_SINGLE_COIL 0x05
60 #define _FC_WRITE_SINGLE_REGISTER 0x06
61 #define _FC_READ_EXCEPTION_STATUS 0x07
62 #define _FC_WRITE_MULTIPLE_COILS 0x0F
63 #define _FC_WRITE_MULTIPLE_REGISTERS 0x10
64 #define _FC_REPORT_SLAVE_ID 0x11
65 #define _FC_WRITE_AND_READ_REGISTERS 0x17
66 
67 typedef enum {
71 
72 /* This structure reduces the number of params in functions and so
73  * optimizes the speed of execution (~ 37%). */
74 typedef struct _sft {
75  int slave;
76  int function;
77  int t_id;
78 } sft_t;
79 
80 typedef struct _modbus_backend {
81  unsigned int backend_type;
82  unsigned int header_length;
83  unsigned int checksum_length;
84  unsigned int max_adu_length;
85  int (*set_slave) (modbus_t *ctx, int slave);
86  int (*build_request_basis) (modbus_t *ctx, int function, int addr,
87  int nb, uint8_t *req);
88  int (*build_response_basis) (sft_t *sft, uint8_t *rsp);
89  int (*prepare_response_tid) (const uint8_t *req, int *req_length);
90  int (*send_msg_pre) (uint8_t *req, int req_length);
91  ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);
92  ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length);
93  int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
94  const int msg_length);
95  int (*pre_check_confirmation) (modbus_t *ctx, const uint8_t *req,
96  const uint8_t *rsp, int rsp_length);
97  int (*connect) (modbus_t *ctx);
98  void (*close) (modbus_t *ctx);
99  int (*flush) (modbus_t *ctx);
100  int (*select) (modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length);
101  int (*filter_request) (modbus_t *ctx, int slave);
103 
104 struct _modbus {
105  /* Slave address */
106  int slave;
107  /* Socket or file descriptor */
108  int s;
109  int debug;
111  struct timeval response_timeout;
112  struct timeval byte_timeout;
115 };
116 
118 void _error_print(modbus_t *ctx, const char *context);
119 
120 #ifndef HAVE_STRLCPY
121 size_t strlcpy(char *dest, const char *src, size_t dest_size);
122 #endif
123 
125 
126 #endif /* _MODBUS_PRIVATE_H_ */
unsigned int header_length
size_t strlcpy(char *dest, const char *src, size_t dest_size)
Definition: modbus.c:1651
void _error_print(modbus_t *ctx, const char *context)
Definition: modbus.c:88
modbus_bakend_type_t
unsigned int max_adu_length
const modbus_backend_t * backend
void * backend_data
int slave
struct _sft sft_t
#define MODBUS_BEGIN_DECLS
Definition: modbus.h:41
void _modbus_init_common(modbus_t *ctx)
Definition: modbus.c:1448
unsigned int checksum_length
int error_recovery
modbus_t * ctx
unsigned int backend_type
#define MODBUS_END_DECLS
Definition: modbus.h:42
int t_id
struct _modbus_backend modbus_backend_t


libmodbus
Author(s):
autogenerated on Sat Nov 21 2020 03:17:32