liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
lightmodbus.h
Go to the documentation of this file.
1 /*
2  liblightmodbus - a lightweight, multiplatform Modbus library
3  Copyright (C) 2017 Jacek Wieczorek <mrjjot@gmail.com>
4 
5  This file is part of liblightmodbus.
6 
7  Liblightmodbus is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Liblightmodbus 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
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
29 #ifndef LIGHTMODBUS_H
30 #define LIGHTMODBUS_H
31 
32 // For C++
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 //Include proper header files
38 #include <inttypes.h>
39 #include "libconf.h"
40 
41 //Some protection
42 #if defined(LIGHTMODBUS_BIG_ENDIAN) && defined(LIGHTMODBUS_LITTLE_ENDIAN)
43 #error LIGHTMODBUS_BIG_ENDIAN and LIGHTMODBUS_LITTLE_ENDIAN cannot be used at once!
44 #endif
45 
49 typedef enum modbusError
50 {
52 
74 
84 } ModbusError;
85 
91 typedef enum modbusFrameError
92 {
112 
114 
119 {
127 
131 typedef enum modbusDataType
132 {
138 
144 static inline uint16_t modbusBitsToBytes( uint16_t n )
145 {
146  return n != 0 ? ( 1 + ( ( n - 1 ) >> 3 ) ) : 0;
147 }
148 
158 static inline uint16_t modbusSwapEndian( uint16_t data ) { return ( data << 8 ) | ( data >> 8 ); }
159 
169 #ifdef LIGHTMODBUS_BIG_ENDIAN
170  static inline uint16_t modbusMatchEndian( uint16_t data ) { return data; }
171 #else
172  static inline uint16_t modbusMatchEndian( uint16_t data ) { return modbusSwapEndian( data ); }
173 #endif
174 
183 extern uint8_t modbusMaskRead( const uint8_t *mask, uint16_t maskLength, uint16_t bit );
184 
194 extern uint8_t modbusMaskWrite( uint8_t *mask, uint16_t maskLength, uint16_t bit, uint8_t value );
195 
203 extern uint16_t modbusCRC( const uint8_t *data, uint16_t length );
204 
205 //For user convenience
206 #include "master.h"
207 #include "slave.h"
208 
209 // For C++ (closes `extern "C"` )
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif
Index value request-response mismatch.
Definition: lightmodbus.h:107
Input register.
Definition: lightmodbus.h:134
Illegal function code.
Definition: lightmodbus.h:120
static uint16_t modbusBitsToBytes(uint16_t n)
Converts number of bits to number of bytes required to store them.
Definition: lightmodbus.h:144
Data value request-response mismatch.
Definition: lightmodbus.h:109
Modbus frame OK. No error.
Definition: lightmodbus.h:93
Holding register.
Definition: lightmodbus.h:133
General Modbus slave functions.
modbusFrameError
Provides more information on frame building/parsing error.
Definition: lightmodbus.h:91
Invalid frame length.
Definition: lightmodbus.h:95
Memory problem.
Definition: lightmodbus.h:71
A NULL pointer provided as some crucial parameter.
Definition: lightmodbus.h:73
Indicates that slave had thrown an exception.
Definition: lightmodbus.h:64
Frame building error occurred - check ModbusMaster::buildError.
Definition: lightmodbus.h:82
Illegal data value.
Definition: lightmodbus.h:122
Invalid CRC.
Definition: lightmodbus.h:94
Acknowledge.
Definition: lightmodbus.h:124
Function request-response mismatch.
Definition: lightmodbus.h:105
modbusError
Represents a library runtime error code.
Definition: lightmodbus.h:49
uint16_t modbusCRC(const uint8_t *data, uint16_t length)
Calculates 16-bit Modbus CRC of provided data.
Definition: lightmodbus.c:46
Other reason causing the function to abort (eg. bad function parameter)
Definition: lightmodbus.h:72
No write access to one of requested regsiters.
Definition: lightmodbus.h:101
No read access to at least one of requested regsiters.
Definition: lightmodbus.h:100
General Modbus master functions.
uint8_t modbusMaskWrite(uint8_t *mask, uint16_t maskLength, uint16_t bit, uint8_t value)
Writes n-th bit in an array.
Definition: lightmodbus.c:33
enum modbusExceptionCode ModbusExceptionCode
Represents a Modbus exception code, defined by the standart.
enum modbusFrameError ModbusFrameError
Provides more information on frame building/parsing error.
Invalid register range.
Definition: lightmodbus.h:98
Count value request-response mismatch.
Definition: lightmodbus.h:108
Slave address request-response mismatch.
Definition: lightmodbus.h:106
static uint16_t modbusMatchEndian(uint16_t data)
Swaps endianness of provided 16-bit data portion if needed.
Definition: lightmodbus.h:170
Negative acknowledge.
Definition: lightmodbus.h:125
Received response for broadcast message.
Definition: lightmodbus.h:111
Definition: lightmodbus.h:81
modbusExceptionCode
Represents a Modbus exception code, defined by the standart.
Definition: lightmodbus.h:118
Coil.
Definition: lightmodbus.h:135
Slave could not process the request.
Definition: lightmodbus.h:123
No error.
Definition: lightmodbus.h:51
Illegal data address.
Definition: lightmodbus.h:121
modbusDataType
Stores information about Modbus data types.
Definition: lightmodbus.h:131
No error. Alias of MODBUS_ERROR_OK.
Definition: lightmodbus.h:83
Illegal data value (eg. when writing a single coil)
Definition: lightmodbus.h:97
Requested a parsing function to parse a frame with wrong function code.
Definition: lightmodbus.h:103
uint8_t modbusMaskRead(const uint8_t *mask, uint16_t maskLength, uint16_t bit)
Reads n-th bit from an array.
Definition: lightmodbus.c:24
static uint16_t modbusSwapEndian(uint16_t data)
Swaps endianness of provided 16-bit data portion.
Definition: lightmodbus.h:158
Discrete input.
Definition: lightmodbus.h:136
There&#39;s neither callback function nor value array provided for this data type.
Definition: lightmodbus.h:99
Invalid declared data item count.
Definition: lightmodbus.h:96
Function not supported.
Definition: lightmodbus.h:102
Mask value request-response mismatch.
Definition: lightmodbus.h:110
Function overriden by user with NULL pointer.
Definition: lightmodbus.h:104
enum modbusDataType ModbusDataType
Stores information about Modbus data types.
enum modbusError ModbusError
Represents a library runtime error code.