1
- <h1 >Modbus library for modern c ++</h1 >
1
+ <h1 >Modbus library for modern C ++</h1 >
2
2
3
- Modbus library for high level frame manipulation with modern C++17.
3
+ Library for high level Modbus frame manipulation, including encoding and decoding, written in modern C++17.
4
4
5
- Contains simple linux TCP and RTU implementation.
5
+ Additionally, library contains simple linux TCP and RTU implementation,
6
+ that you can use as a starting point for you application !
6
7
7
- # Contents
8
+ # Table of content
8
9
- [ Why] ( #why )
9
10
- [ Important Concept] ( #important-concept )
10
11
- [ Possibilities] ( #possibilities )
@@ -14,17 +15,22 @@ Contains simple linux TCP and RTU implementation.
14
15
- [ Api] ( #api )
15
16
16
17
# Why
17
- When I was working on my last project and tried to find a good c++ Modbus library (other than Qt) I was unable to find it.
18
- That is why I have decided to share my own implementation of it.
18
+
19
+ When I was working on my last project and tried to find a good C++ Modbus library (other than Qt) I was unable to find it.
20
+ That is why I have decided to share my own implementation of Modbus.
19
21
20
22
# Important Concept
21
- This library is ** mainly** for providing Modbus logic, it doesnt aim to have best communiaction implementation.
22
- It gives user ability to create Modbus frames in high level api and convert them to raw bytes or show them as string.
23
- That is why * Modbus Core* is OS independent and can be easily used with other communication frameworks.
24
23
25
- It does have communiaction module which is ** enabled** by default, and works pretty well on linux.
24
+ This library is ** mainly** for providing Modbus logic.
25
+ It doesnt aim to have best communiaction implementation, as it is usually HW-specific.
26
+ It gives user ability to create Modbus frames using high level api and convert them to raw bytes or show them as string.
27
+ That is why * Modbus Core* is OS independent and can be easily used with other communication frameworks,
28
+ assuming that you compiler supports at least C++17.
29
+
30
+ Modbus communiaction module is ** enabled** by default and works pretty well on linux.
31
+ If you are using your own communication, be sure to disable it via cmake variable.
26
32
27
- # Possibilities
33
+ # Possibilities
28
34
29
35
Quick example of what Modbus Core can do:
30
36
@@ -81,14 +87,17 @@ Stringed Request 2 after rawed: Read from output coils, from slave 1, starting f
81
87
```
82
88
83
89
# Dependencies
90
+
91
+ Library core is dependency free - except for C++ standard library.
92
+
93
+ For communication module, you may need:
94
+
84
95
- libnet - only for tcp communication (not needed if communication is disabled)
85
96
86
97
# STATUS
87
98
88
99
Currently Modbus Core is fully functional and (I belive) it doesn't have any bugs.
89
100
90
- API for it is in progress.
91
-
92
101
Modbus Communication is working * currently* only for linux, it works well on TCP and Serial (tested on raspberry pi).
93
102
94
103
# How to learn Modbus ?
@@ -117,201 +126,6 @@ You should be able to use library.
117
126
If you are on other os then gnu/linux you should disable communication part of modbus via cmake variable MODBUS_COMMUNICATION.
118
127
119
128
# API
120
- [ link] ( https://raw.githack.com/Mazurel/Modbus/master/docs/html/index.html )
121
-
122
- ## It is the best to use docs generated by doxygen
123
-
124
- You can read it in [ docs/html] ( https://raw.githack.com/Mazurel/Modbus/master/docs/html/index.html ) or generate it yourself via:
125
- ``` bash
126
- doxygen Doxyfile
127
- ```
128
-
129
- ## The below API is not finished (propably wont be), it is preffered to use doxygen for code documentation.
130
-
131
- - [ Enums] ( #enums )
132
- - [ Methods] ( #methods )
133
- - [ Classes] ( #classes )
134
-
135
- ## Enums
136
- Below each enum there are all values of enum.
137
- - ` MB::utils::MBErrorCode ` - Enum that contains all the standard Modbus error Codes as well as Modbus library specific errors.
138
- ``` c++
139
- // Documentation modbus errors:
140
- IllegalFunction = 0x01
141
- IllegalDataAddress = 0x02
142
- IllegalDataValue = 0x03
143
- SlaveDeviceFailure = 0x04
144
- Acknowledge = 0x05
145
- SlaveDeviceBusy = 0x06
146
- NegativeAcknowledge = 0x07
147
- MemoryParityError = 0x08
148
- GatewayPathUnavailable = 0x10
149
- GatewayTargetDeviceFailedToRespond = 0x11
150
-
151
- // Custom modbus errors:
152
- ErrorCodeCRCError = 0b0111111
153
- InvalidCRC = 0b01111110
154
- InvalidByteOrder = 0b01111101
155
- InvalidMessageID = 0b01111100
156
- ProtocolError = 0b01111011
157
- ConnectionClosed = 0b01111010
158
- Timeout = 0b01111001
159
- ```
160
- - `MB::utils::MBFunctionCode` - Enum that contains all Modbus function codes.
161
- ```c++
162
- // Reading functions
163
- ReadDiscreteOutputCoils = 0x01
164
- ReadDiscreteInputContacts = 0x02
165
- ReadAnalogOutputHoldingRegisters = 0x03
166
- ReadAnalogInputRegisters = 0x04
167
-
168
- // Single write functions
169
- WriteSingleDiscreteOutputCoil = 0x05
170
- WriteSingleAnalogOutputRegister = 0x06
171
-
172
- // Multiple write functions
173
- WriteMultipleDiscreteOutputCoils = 0x0F
174
- WriteMultipleAnalogOutputHoldingRegisters = 0x10
175
-
176
- // Custom
177
- Undefined = 0x00
178
- ```
179
- - `MB::utils::MBFunctionType` - Enum that contains function types.
180
- ```c++
181
- Read
182
- WriteSingle
183
- WriteMultiple
184
- ```
185
- - `MB::utils::MBFunctionRegisters` - Enum that contains all register types.
186
- ```c++
187
- OutputCoils
188
- InputContacts
189
- HoldingRegisters
190
- InputRegisters
191
- ```
192
-
193
- ## Methods
194
-
195
- - `bool MB::utils::isStandardErrorCode (MBErrorCode code)` -
196
- Returns true if specified code is a Modbus standard error code.
197
- - ` std::string MB::utils::mbErrorCodeToStr(MBErrorCode code) ` -
198
- Returns stringed name of a specified Modbus error code.
199
- - ` MBFunctionType MB::utils::functionType(const MBFunctionCode code) ` -
200
- Get functions type based on function code.
201
- - ` MBFunctionRegisters MB::utils::functionRegister(const MBFunctionCode code) ` -
202
- Get functions register based on function code.
203
- - ` uint16_t MB::utils::bigEndianConv(const uint8_t *buf) ` -
204
- Creates uint16_t number from uint8_t buffer of two bytes (used when reading modbus frames).
205
- - ` uint16_t MB::utils::calculateCRC(const uint8_t *buff, size_t len) `
206
-
207
- ` uint16_t MB::utils::calculateCRC(const std::vector<uint8_t>& buffer) ` -
208
- Pretty self explanatory.
209
- ## Classes
210
-
211
- > For each getter and setter field there is:
212
- >
213
- > \< name\> () const - that gets the value
214
- >
215
- > set\< Name\> (value) - that sets value
216
-
217
- #### ModbusException
218
-
219
- Its prupose is to represent Modbus exception, either frame or c++ exception
220
-
221
- - Constructor:
222
- - ` ModbusException(const std::vector<uint8_t>& inputData, bool CRC = false); ` -
223
- Creates ModbusException from raw bytes, with CRC check based on parameter.
224
- - `ModbusException(utils::MBErrorCode errorCode,
225
- uint8_t slaveId = 0xFF,
226
- utils::MBFunctionCode functionCode = utils::Undefined)
227
- noexcept :
228
- _ slaveId(slaveId),
229
- _ validSlave(true),
230
- _ errorCode(errorCode),
231
- _ functionCode(functionCode)
232
- {}` -
233
- Creates Modbus exception based on it's properties.
234
- - Methods:
235
- - ` static ModbusException::exist(const std::vector<uint8_t>& inputData) ` -
236
- Checks if there is exception in modbus frame.
237
- - ` std::string toString() ` -
238
- Returns string representation of exception.
239
- - ` std::vector<uint8_t> toRaw() ` -
240
- Retruns raw frame represenation of a excaption.
241
- - Getters and setters:
242
- - functionCode
243
- - slaveID
244
-
245
- #### ModbusRequest
246
-
247
- Its purpose is to represent modbus request frame.
248
-
249
- - Constructors:
250
- - ` static ModbusRequest(std::vector<uint8_t> inputData, bool CRC = false) ` -
251
- Creates Modbus request based on raw bytes and CRC boolean. If CRC is ON and the check fails constructor throws exception.
252
- - ` static ModbusRequest::fromRaw(const std::vector<uint8_t>& inputData) ` -
253
- Creates ModbusRequest from raw bytes.
254
- - ` static ModbusRequest::fromRawCRC(const std::vector<uint8_t>& inputData) ` -
255
- Creates ModbusRequest from raw bytes and checks CRC.
256
- When CRC is invalid throws InvalidCRC exception.
257
- - `ModbusRequest(uint8_t slaveId = 0,
258
- utils::MBFunctionCode functionCode = static_cast< utils::MBFunctionCode > (0),
259
- uint16_t address = 0,
260
- uint16_t registersNumber = 0,
261
- std::vector<ModbusCell > values = {})` -
262
- Self explanatory.
263
- - Methods:
264
- - ` std::string ModbusRequest::toString() ` -
265
- Returns string representation of a request.
266
- - ` std::vector<uint8_t> ModbusRequest::toRaw() ` -
267
- Converts ModbusRequest to raw bytes.
268
- - ` MB::utils::MBFunctionType functionType() const ` -
269
- Gets function type for current function code.
270
- - ` MB::utils::MBFunctionRegisters functionRegisters() const ` -
271
- Gets function register for current function code.
272
- - Getters and setters:
273
- - slaveID
274
- - functionCode
275
- - registerAddress
276
- - numberOfRegisters
277
- - registerValues
278
-
279
- #### ModbusResponse
280
-
281
- Its purpose is to represent response frame.
282
-
283
- - Constructors:
284
- - ` static ModbusResponse(std::vector<uint8_t> inputData, bool CRC = false) ` -
285
- Creates Modbus response based on raw bytes and CRC boolean. If CRC is ON and the check fails constructor throws exception.
286
- - ` static ModbusResponse::fromRaw(const std::vector<uint8_t>&) ` -
287
- Creates ModbusResponse from raw bytes
288
- - ` static ModbusResponse::fromRawCRC(const std::vector<uint8_t>&) ` -
289
- Creates ModbusResponse from raw bytes and checks CRC.
290
- When CRC is invalid throws InvalidCRC exception.
291
- - `ModbusResponse(uint8_t slaveId = 0,
292
- utils::MBFunctionCode functionCode = 0x00,
293
- uint16_t address = 0,
294
- uint16_t registersNumber = 0,
295
- std::vector<ModbusCell > values = {})`
296
- -
297
- Self explanatory constructor.
298
- - Methods:
299
- - ` std::string toString() ` -
300
- Returns ModbusResponse string representation.
301
- - ` std::vector<uint8_t> toRaw() ` -
302
- Converts ModbusResponse to vector of raw bytes.
303
- - ` void from(const ModbusRequest&) ` -
304
- Fills ModbusResponse with the request.
305
- Needed if you want ModbusResponse to have all the data.
306
- This method is needed when you create object from raw.
307
- - ` MB::utils::MBFunctionType functionType() const ` -
308
- Gets function type for current function code.
309
- - ` MB::utils::MBFunctionRegisters functionRegisters() const ` -
310
- Gets function register for current function code.
311
- - Getters and setters:
312
- - slaveID
313
- - functionCode
314
- - registerAddress
315
- - numberOfRegisters
316
- - registerValues
317
129
130
+ API documentation is generated using [ Doxygen] ( https://www.doxygen.nl ) and it is available online under this [ link] ( https://mazurel.github.io/docs/modbus/index.html ) .
131
+ If you want, you can also generate it yourself !
0 commit comments