-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmb_layer1_prototypes.h
148 lines (126 loc) · 6.02 KB
/
mb_layer1_prototypes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2002,2016 Mario de Sousa (msousa@fe.up.pt)
*
* This file is part of the Modbus library for Beremiz and matiec.
*
* This Modbus library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this Modbus library. If not, see <http://www.gnu.org/licenses/>.
*
* This code is made available on the understanding that it will not be
* used in safety-critical situations without a full and competent review.
*/
/* write a modbus frame */
/* WARNING: when calling this function, the *frame_data buffer
* must be allocated with an extra *extra_bytes
* beyond those required for the frame_length.
* This is because the extra bytes will be used
* to store the crc before sending the frame.
*
* The *extra_bytes value will be returned by the
* modbus_init() function call.
*/
/* NOTE: calling this function will flush the input stream,
* which means any frames that may have arrived
* but have not yet been read using modbus_read()
* will be permanently lost...
*/
int modbus_write(int nd,
u8 *frame_data,
size_t frame_length,
u16 transaction_id,
const struct timespec *transmit_timeout
);
/* read a modbus frame */
/*
* The frame is read from:
* - the node descriptor nd, if nd >= 0
* - any valid and initialised node descriptor, if nd = -1
* In this case, the node where the data is eventually read from
* is returned in *nd.
* NOTE: (only avaliable if using TCP)
*/
/* NOTE: calling modbus_write() will flush the input stream,
* which means any frames that may have arrived
* but have not yet been read using modbus_read()
* will be permanently lost...
*
* NOTE: Ususal select semantics for (a: recv_timeout == NULL) and
* (b: *recv_timeout == 0) also apply.
* (a) Indefinite timeout
* (b) Try once, and and quit if no data available.
*/
/* NOTE: send_data and send_length is used to pass to the modbus_read() function
* the frame that was previously sent over the same connection (node).
* This data is then allows the modbus_read() function to ignore any
* data that is read but identical to the previously sent data. This
* is used when using serial ports that echoes back all the data that is
* sent out over the same serial port. When using some RS232 to RS485
* converters, this functionality is essential as not all these converters
* are capable of not echoing back the sent data.
* These parameters are ignored when using TCP!
*
* requested_frame_type: one of -> MB_req_frame, MB_resp_frame, MB_any_frame
* the type of frame we should search for (request, response, or any)
* NOTE: only used by the RTu layer. Other layers simply ignore this parameter.
* NOTE: whatever the type of frame, searching for error frames is
* always enabled (i.e. this function may always return that it found
* an error frame, whatever frame type it was told to look for)
* NOTE:
* This is needed because the RTU protocol may confuse some valid
* response frames with valid query frames (e.g. the response to
* read registers may contain data with values that just so happens
* to match the correct CRC of the read registers query packet, making
* it impossible to distinguish the beginning of a read registers
* response to a read registers query). We only give this priority
* if it is possible to have this con
*/
/* RETURNS: number of bytes read
* -1 on read from file/node error
* -2 on timeout
*/
int modbus_read(int *nd, /* node descriptor */
u8 **recv_data_ptr,
u16 *transaction_id,
mb_frame_type_t requested_frame_type,
const u8 *send_data,
int send_length,
const struct timespec *recv_timeout);
/* init the library */
int modbus_init(int nd_count, /* maximum number of nodes... */
optimization_t opt);
/* shutdown the library...*/
int modbus_done(void);
/* Open a node for master / slave operation.
* Returns the node descriptor, or -1 on error.
*/
int modbus_connect(node_addr_t node_addr);
int modbus_listen(node_addr_t node_addr);
/* Close a node, needs a node descriptor as argument... */
int modbus_close(int nd);
/* Tell the library that the user will probably not be communicating
* for some time...
* This will allow the library to release any resources it will not
* be needing during the silence.
* NOTE: This is onlyused by the TCP version to close down tcp connections
* when the silence will going to be longer than second.
*/
int modbus_silence_init(void);
/* determine the minmum acceptable timeout... */
/* NOTE: timeout values passed to modbus_read() lower than the value returned
* by this function may result in frames being aborted midway, since they
* take at least modbus_get_min_timeout() seconds to transmit.
*/
double modbus_get_min_timeout(int baud,
int parity,
int data_bits,
int stop_bits);