0
|
1 |
/*
|
|
2 |
* Copyright (c) 2002,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 |
|
|
25 |
|
|
26 |
/* write a modbus frame */
|
|
27 |
/* WARNING: when calling this function, the *frame_data buffer
|
|
28 |
* must be allocated with an extra *extra_bytes
|
|
29 |
* beyond those required for the frame_length.
|
|
30 |
* This is because the extra bytes will be used
|
|
31 |
* to store the crc before sending the frame.
|
|
32 |
*
|
|
33 |
* The *extra_bytes value will be returned by the
|
|
34 |
* modbus_init() function call.
|
|
35 |
*/
|
|
36 |
/* NOTE: calling this function will flush the input stream,
|
|
37 |
* which means any frames that may have arrived
|
|
38 |
* but have not yet been read using modbus_read()
|
|
39 |
* will be permanently lost...
|
|
40 |
*/
|
|
41 |
int modbus_write(int nd,
|
|
42 |
u8 *frame_data,
|
|
43 |
size_t frame_length,
|
|
44 |
u16 transaction_id,
|
|
45 |
const struct timespec *transmit_timeout
|
|
46 |
);
|
|
47 |
|
|
48 |
/* read a modbus frame */
|
|
49 |
/*
|
|
50 |
* The frame is read from:
|
|
51 |
* - the node descriptor nd, if nd >= 0
|
|
52 |
* - any valid and initialised node descriptor, if nd = -1
|
|
53 |
* In this case, the node where the data is eventually read from
|
|
54 |
* is returned in *nd.
|
|
55 |
* NOTE: (only avaliable if using TCP)
|
|
56 |
*/
|
|
57 |
/* NOTE: calling modbus_write() will flush the input stream,
|
|
58 |
* which means any frames that may have arrived
|
|
59 |
* but have not yet been read using modbus_read()
|
|
60 |
* will be permanently lost...
|
|
61 |
*
|
|
62 |
* NOTE: Ususal select semantics for (a: recv_timeout == NULL) and
|
|
63 |
* (b: *recv_timeout == 0) also apply.
|
|
64 |
* (a) Indefinite timeout
|
|
65 |
* (b) Try once, and and quit if no data available.
|
|
66 |
*/
|
|
67 |
/* NOTE: send_data and send_length is used to pass to the modbus_read() function
|
|
68 |
* the frame that was previously sent over the same connection (node).
|
|
69 |
* This data is then allows the modbus_read() function to ignore any
|
|
70 |
* data that is read but identical to the previously sent data. This
|
|
71 |
* is used when using serial ports that echoes back all the data that is
|
|
72 |
* sent out over the same serial port. When using some RS232 to RS485
|
|
73 |
* converters, this functionality is essential as not all these converters
|
|
74 |
* are capable of not echoing back the sent data.
|
|
75 |
* These parameters are ignored when using TCP!
|
|
76 |
*/
|
|
77 |
|
|
78 |
/* RETURNS: number of bytes read
|
|
79 |
* -1 on read from file/node error
|
|
80 |
* -2 on timeout
|
|
81 |
*/
|
|
82 |
int modbus_read(int *nd, /* node descriptor */
|
|
83 |
u8 **recv_data_ptr,
|
|
84 |
u16 *transaction_id,
|
|
85 |
const u8 *send_data,
|
|
86 |
int send_length,
|
|
87 |
const struct timespec *recv_timeout);
|
|
88 |
|
|
89 |
|
|
90 |
/* init the library */
|
|
91 |
int modbus_init(int nd_count, /* maximum number of nodes... */
|
|
92 |
optimization_t opt,
|
|
93 |
int *extra_bytes);
|
|
94 |
|
|
95 |
/* shutdown the library...*/
|
|
96 |
int modbus_done(void);
|
|
97 |
|
|
98 |
|
|
99 |
/* Open a node for master / slave operation.
|
|
100 |
* Returns the node descriptor, or -1 on error.
|
|
101 |
*/
|
|
102 |
int modbus_connect(node_addr_t node_addr);
|
|
103 |
int modbus_listen(node_addr_t node_addr);
|
|
104 |
|
|
105 |
/* Close a node, needs a node descriptor as argument... */
|
|
106 |
int modbus_close(int nd);
|
|
107 |
|
|
108 |
/* Tell the library that the user will probably not be communicating
|
|
109 |
* for some time...
|
|
110 |
* This will allow the library to release any resources it will not
|
|
111 |
* be needing during the silence.
|
|
112 |
* NOTE: This is onlyused by the TCP version to close down tcp connections
|
|
113 |
* when the silence will going to be longer than second.
|
|
114 |
*/
|
|
115 |
int modbus_silence_init(void);
|
|
116 |
|
|
117 |
/* determine the minmum acceptable timeout... */
|
|
118 |
/* NOTE: timeout values passed to modbus_read() lower than the value returned
|
|
119 |
* by this function may result in frames being aborted midway, since they
|
|
120 |
* take at least modbus_get_min_timeout() seconds to transmit.
|
|
121 |
*/
|
|
122 |
double modbus_get_min_timeout(int baud,
|
|
123 |
int parity,
|
|
124 |
int data_bits,
|
|
125 |
int stop_bits);
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|