1 | /*
|
---|
2 | * libmodbusWrapper.cpp
|
---|
3 | * ModBusKit
|
---|
4 | */
|
---|
5 | // Copyright 2008 Volitans Software and R Engineering, Inc.
|
---|
6 | /*
|
---|
7 | This library is free software; you can redistribute it and/or
|
---|
8 | modify it under the terms of the GNU Lesser General Public
|
---|
9 | License as published by the Free Software Foundation; either
|
---|
10 | version 2 of the License, or (at your option) any later version.
|
---|
11 |
|
---|
12 | This library is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | Lesser General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU Lesser General Public
|
---|
18 | License along with this library; if not, write to the Free Software
|
---|
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
---|
20 |
|
---|
21 | These library of functions are designed to enable a program send and
|
---|
22 | receive data from a device that communicates using the Modbus protocol.
|
---|
23 | */
|
---|
24 | //
|
---|
25 | // This is an Objective-C++ wrapper for libmodbus
|
---|
26 |
|
---|
27 | #include "libmodbusWrapper.h"
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 | bool libmodbusWrapper::lmbInitSerial(const char* device, int baud, int parity, int data_bit, int stop_bit)
|
---|
31 | {
|
---|
32 | if (data_bit<5 || data_bit>8)
|
---|
33 | return(FALSE);
|
---|
34 | if (stop_bit<1 || stop_bit>2)
|
---|
35 | return(FALSE);
|
---|
36 |
|
---|
37 | switch(parity)
|
---|
38 | {
|
---|
39 | case k_none:
|
---|
40 | modbus_init_rtu(&mb_param, device, baud, "none", data_bit, stop_bit);
|
---|
41 | return(TRUE);
|
---|
42 | case k_even:
|
---|
43 | modbus_init_rtu(&mb_param, device, baud, "even", data_bit, stop_bit);
|
---|
44 | return(TRUE);
|
---|
45 | case k_odd:
|
---|
46 | modbus_init_rtu(&mb_param, device, baud, "odd", data_bit, stop_bit);
|
---|
47 | return(TRUE);
|
---|
48 | default:
|
---|
49 | return(FALSE);
|
---|
50 | }
|
---|
51 | return(FALSE);
|
---|
52 | }
|
---|
53 |
|
---|
54 | bool libmodbusWrapper::lmbInitTCP(const char* device, int port)
|
---|
55 | {
|
---|
56 | if (port<=0)
|
---|
57 | modbus_init_tcp(&mb_param, device, MODBUS_TCP_DEFAULT_PORT);
|
---|
58 | else
|
---|
59 | modbus_init_tcp(&mb_param, device, port);
|
---|
60 |
|
---|
61 | return(true);
|
---|
62 | }
|
---|
63 |
|
---|
64 | int libmodbusWrapper::lmbConnect()
|
---|
65 | {
|
---|
66 | return(modbus_connect(&mb_param));
|
---|
67 | }
|
---|
68 |
|
---|
69 | void libmodbusWrapper::lmbDisconnect()
|
---|
70 | {
|
---|
71 | modbus_close(&mb_param);
|
---|
72 | }
|
---|
73 |
|
---|
74 | bool libmodbusWrapper::lmbReadDiscreteInput(int slaveID, int startAddress, int inputCount, uint8_t *data)
|
---|
75 | {
|
---|
76 | int error;
|
---|
77 |
|
---|
78 | error=read_input_status(&mb_param, slaveID, startAddress, inputCount, data);
|
---|
79 |
|
---|
80 | if (error>=0)
|
---|
81 | {
|
---|
82 | errorCode=0;
|
---|
83 | return(true);
|
---|
84 | }
|
---|
85 |
|
---|
86 | errorCode=error;
|
---|
87 | return(false);
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool libmodbusWrapper::lmbReadCoil(int slaveID, int startAddress, int coilCount, uint8_t *data)
|
---|
91 | {
|
---|
92 | int error;
|
---|
93 |
|
---|
94 | error=read_coil_status(&mb_param, slaveID, startAddress, coilCount, data);
|
---|
95 |
|
---|
96 | if (error>=0)
|
---|
97 | {
|
---|
98 | errorCode=0;
|
---|
99 | return(true);
|
---|
100 | }
|
---|
101 |
|
---|
102 | errorCode=error;
|
---|
103 | return(false);
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool libmodbusWrapper::lmbWriteSingleCoil(int slaveID, int coilAddress, bool state)
|
---|
107 | {
|
---|
108 | int error;
|
---|
109 |
|
---|
110 | error=force_single_coil(&mb_param, slaveID, coilAddress, state);
|
---|
111 |
|
---|
112 | if (error>=0)
|
---|
113 | {
|
---|
114 | errorCode=0;
|
---|
115 | return(true);
|
---|
116 | }
|
---|
117 |
|
---|
118 | errorCode=error;
|
---|
119 | return(false);
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool libmodbusWrapper::lmbWriteMultipleCoils(int slaveID, int startAddress, int coilCount, uint8_t *states)
|
---|
123 | {
|
---|
124 | int error;
|
---|
125 |
|
---|
126 | error=force_multiple_coils(&mb_param, slaveID, startAddress, coilCount, states);
|
---|
127 |
|
---|
128 | if (error>=0)
|
---|
129 | {
|
---|
130 | errorCode=0;
|
---|
131 | return(true);
|
---|
132 | }
|
---|
133 |
|
---|
134 | errorCode=error;
|
---|
135 | return(false);
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool libmodbusWrapper::lmbReadInputRegisters(int slaveID, int startAddress, int registerCount, uint16_t *data)
|
---|
139 | {
|
---|
140 | int error;
|
---|
141 |
|
---|
142 | error=read_input_registers(&mb_param, slaveID, startAddress, registerCount, data);
|
---|
143 |
|
---|
144 | if (error>=0)
|
---|
145 | {
|
---|
146 | errorCode=0;
|
---|
147 | return(true);
|
---|
148 | }
|
---|
149 |
|
---|
150 | errorCode=error;
|
---|
151 | return(false);
|
---|
152 | }
|
---|
153 |
|
---|
154 | bool libmodbusWrapper::lmbReadHoldingRegisters(int slaveID, int startAddress, int registerCount, uint16_t *data)
|
---|
155 | {
|
---|
156 | int error;
|
---|
157 |
|
---|
158 | error=read_holding_registers(&mb_param, slaveID, startAddress, registerCount, data);
|
---|
159 |
|
---|
160 | if (error>=0)
|
---|
161 | {
|
---|
162 | errorCode=0;
|
---|
163 | return(true);
|
---|
164 | }
|
---|
165 |
|
---|
166 | errorCode=error;
|
---|
167 | return(false);
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool libmodbusWrapper::lmbWriteSingleHoldingRegister(int slaveID, int registerAddress, uint16_t value)
|
---|
171 | {
|
---|
172 | int error;
|
---|
173 |
|
---|
174 | error=preset_single_register(&mb_param, slaveID, registerAddress, value);
|
---|
175 |
|
---|
176 | if (error>=0)
|
---|
177 | {
|
---|
178 | errorCode=0;
|
---|
179 | return(true);
|
---|
180 | }
|
---|
181 |
|
---|
182 | errorCode=error;
|
---|
183 | return(false);
|
---|
184 | }
|
---|
185 |
|
---|
186 | bool libmodbusWrapper::lmbWriteMultipleHoldingRegisters(int slaveID, int startAddress, int registerCount, uint16_t *values)
|
---|
187 | {
|
---|
188 | int error;
|
---|
189 |
|
---|
190 | error=preset_multiple_registers(&mb_param, slaveID, startAddress, registerCount, values);
|
---|
191 |
|
---|
192 | if (error>=0)
|
---|
193 | {
|
---|
194 | errorCode=0;
|
---|
195 | return(true);
|
---|
196 | }
|
---|
197 |
|
---|
198 | errorCode=error;
|
---|
199 | return(false);
|
---|
200 | }
|
---|
201 |
|
---|
202 | int libmodbusWrapper::lmbErrorCode()
|
---|
203 | {
|
---|
204 | return(errorCode);
|
---|
205 | }
|
---|
206 |
|
---|
207 | void libmodbusWrapper::lmbSetDebug(bool state)
|
---|
208 | {
|
---|
209 | modbus_set_debug(&mb_param, (int)state);
|
---|
210 | debug=state;
|
---|
211 | }
|
---|
212 |
|
---|
213 | void libmodbusWrapper::lmbSetRawDataSave(bool state)
|
---|
214 | {
|
---|
215 | modbus_set_raw_data_save(&mb_param, (int)state);
|
---|
216 | }
|
---|
217 |
|
---|
218 | int libmodbusWrapper::lmbLastRawQuery(uint8_t *rawQuery)
|
---|
219 | {
|
---|
220 | return(modbus_last_raw_query(&mb_param, rawQuery));
|
---|
221 | }
|
---|
222 |
|
---|
223 | int libmodbusWrapper::lmbLastRawResponse(uint8_t *rawResponse)
|
---|
224 | {
|
---|
225 | return(modbus_last_raw_response(&mb_param, rawResponse));
|
---|
226 | }
|
---|
227 |
|
---|
228 | int libmodbusWrapper::lmbLastRawQueryLength()
|
---|
229 | {
|
---|
230 | return(modbus_last_raw_query_length(&mb_param));
|
---|
231 | }
|
---|
232 |
|
---|
233 | int libmodbusWrapper::lmbLastRawResponseLength()
|
---|
234 | {
|
---|
235 | return(modbus_last_raw_response_length(&mb_param));
|
---|
236 | }
|
---|
237 |
|
---|