liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
parser.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 
28 #ifndef LIGHTMODBUS_PARSER_H
29 #define LIGHTMODBUS_PARSER_H
30 
31 // For C++
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <inttypes.h>
37 #include "libconf.h"
38 
39 
43 typedef union modbusParser
44 {
45  uint8_t frame[256];
46 
47  struct __attribute__( ( __packed__ ) )
48  {
49  uint8_t address;
50  uint8_t function;
51  } base; //Base shared bytes, common for all frames, which always have the same meaning
52 
53  struct __attribute__( ( __packed__ ) )
54  {
55  uint8_t address;
56  uint8_t function;
57  uint8_t code;
58  uint16_t crc;
60 
61  struct __attribute__( ( __packed__ ) )
62  {
63  uint8_t address;
64  uint8_t function;
65  uint16_t index;
66  uint16_t count;
67  uint16_t crc;
68  } request0102; //Read multiple coils or discrete inputs
69 
70  struct __attribute__( ( __packed__ ) )
71  {
72  uint8_t address;
73  uint8_t function;
74  uint8_t length;
75  uint8_t values[250];
76  uint16_t crc;
77  } response0102; //Read multiple coils or discrete inputs - response
78 
79  struct __attribute__( ( __packed__ ) )
80  {
81  uint8_t address;
82  uint8_t function;
83  uint16_t index;
84  uint16_t count;
85  uint16_t crc;
86  } request0304; //Read multiple holding registers or input registers
87 
88  struct __attribute__( ( __packed__ ) )
89  {
90  uint8_t address;
91  uint8_t function;
92  uint8_t length;
93  uint16_t values[125];
94  uint16_t crc;
95  } response0304; //Read multiple holding registers or input registers - response
96 
97  //TODO merge request 05 and 06
98  struct __attribute__( ( __packed__ ) )
99  {
100  uint8_t address;
101  uint8_t function;
102  uint16_t index;
103  uint16_t value;
104  uint16_t crc;
105  } request05; //Write single coil
106 
107  struct __attribute__( ( __packed__ ) )
108  {
109  uint8_t address;
110  uint8_t function;
111  uint16_t index;
112  uint16_t value;
113  uint16_t crc;
114  } response05; //Write single coil - response
115 
116  struct __attribute__( ( __packed__ ) )
117  {
118  uint8_t address;
119  uint8_t function;
120  uint16_t index;
121  uint16_t value;
122  uint16_t crc;
123  } request06; //Write single holding register
124 
125  struct __attribute__( ( __packed__ ) )
126  {
127  uint8_t address;
128  uint8_t function;
129  uint16_t index;
130  uint16_t value;
131  uint16_t crc;
132  } response06; //Write single holding register
133 
134  struct __attribute__( ( __packed__ ) )
135  {
136  uint8_t address;
137  uint8_t function;
138  uint16_t index;
139  uint16_t count;
140  uint8_t length;
141  uint8_t values[246];
142  uint16_t crc;
143  } request15; //Write multiple coils
144 
145  struct __attribute__( ( __packed__ ) )
146  {
147  uint8_t address;
148  uint8_t function;
149  uint16_t index;
150  uint16_t count;
151  uint16_t crc;
152  } response15; //Write multiple coils - response
153 
154  struct __attribute__( ( __packed__ ) )
155  {
156  uint8_t address;
157  uint8_t function;
158  uint16_t index;
159  uint16_t count;
160  uint8_t length;
161  uint16_t values[123];
162  uint16_t crc;
163  } request16; //Write multiple holding registers
164 
165  struct __attribute__( ( __packed__ ) )
166  {
167  uint8_t address;
168  uint8_t function;
169  uint16_t index;
170  uint16_t count;
171  uint16_t crc;
172  } response16; //Write multiple holding registers
173 
174  struct __attribute__( ( __packed__ ) )
175  {
176  uint8_t address;
177  uint8_t function;
178  uint16_t index;
179  uint16_t andmask;
180  uint16_t ormask;
181  uint16_t crc;
182  } request22; //Mask write single holding register
183 
184  struct __attribute__( ( __packed__ ) )
185  {
186  uint8_t address;
187  uint8_t function;
188  uint16_t index;
189  uint16_t andmask;
190  uint16_t ormask;
191  uint16_t crc;
192  } response22; //Mask write single holding register
193 } ModbusParser;
194 
195 // For C++ (closes `extern "C"` )
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif
request05
Definition: parser.h:105
base
Definition: parser.h:51
request22
Definition: parser.h:182
request06
Definition: parser.h:123
response22
Definition: parser.h:192
A big union of structures used for parsing standard Modbus requests and building responses.
Definition: parser.h:43
response05
Definition: parser.h:114
response0102
Definition: parser.h:77
response16
Definition: parser.h:172
request0304
Definition: parser.h:86
union modbusParser ModbusParser
A big union of structures used for parsing standard Modbus requests and building responses.
response06
Definition: parser.h:132
request0102
Definition: parser.h:68
request16
Definition: parser.h:163
response15
Definition: parser.h:152
request15
Definition: parser.h:143
response0304
Definition: parser.h:95
struct __attribute__((__packed__))
Definition: parser.h:47
uint8_t frame[256]
Definition: parser.h:45
exception
Definition: parser.h:59