liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
master.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_MASTER_H
29 #define LIGHTMODBUS_MASTER_H
30 
31 // For C++
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <inttypes.h>
37 #include "parser.h"
38 #include "libconf.h"
39 #include "lightmodbus.h"
40 
41 #ifdef LIGHTMODBUS_MASTER_BASE
42 
43 #if defined( LIGHTMODBUS_MASTER_INVASIVE_PARSING ) && !defined( LIGHTMODBUS_NO_MASTER_DATA_BUFFER )
44  #warning LIGHTMODBUS_MASTER_INVASIVE_PARSING has no effect if LIGHTMODBUS_NO_MASTER_DATA_BUFFER is not defined
45 #endif
46 
47 #if defined( LIGHTMODBUS_NO_MASTER_DATA_BUFFER ) && !defined( LIGHTMODBUS_EXPERIMENTAL )
48  #error Disabling exclusive master data buffer is an experimental feature that may cause problems. Please define LIGHTMODBUS_EXPERIMENTAL to dismiss this error message, but please make sure your system permits unaligned memory acces beforehand.
49 #endif
50 
51 #if defined( LIGHTMODBUS_NO_MASTER_DATA_BUFFER ) && defined( LIGHTMODBUS_STATIC_MEM_MASTER_DATA )
52  #error LIGHTMODBUS_STATIC_MEM_MASTER_DATA and LIGHTMODBUS_NO_MASTER_DATA_BUFFER cannot be used at once. Please make up your mind.
53 #endif
54 
55 #ifdef LIGHTMODBUS_MASTER_USER_FUNCTIONS
56  struct modbusMaster;
57 
61  typedef struct modbusMasterUserFunction
62  {
63  uint8_t function;
64  ModbusError ( *handler )( struct modbusMaster *status, ModbusParser *parser, ModbusParser *requestParser );
66 #endif
67 
71 typedef struct modbusMaster
72 {
75 
85  struct
86  {
87  #ifdef LIGHTMODBUS_STATIC_MEM_MASTER_REQUEST
88  uint8_t frame[LIGHTMODBUS_STATIC_MEM_MASTER_REQUEST];
90  #else
91  uint8_t *frame;
93  #endif
94 
96  uint8_t length;
97  } request;
98 
108  struct //Response from slave should be put here
109  {
110  #ifdef LIGHTMODBUS_STATIC_MEM_MASTER_RESPONSE
111  uint8_t frame[LIGHTMODBUS_STATIC_MEM_MASTER_RESPONSE];
113  #else
114  const uint8_t *frame;
116  #endif
117 
119  uint8_t length;
120  } response;
121 
128  struct
129  {
130  uint8_t address;
131  uint16_t index;
132  uint16_t count;
133  uint8_t length;
135  uint8_t function;
136 
137  #ifdef LIGHTMODBUS_STATIC_MEM_MASTER_DATA
138  union
139  {
147  uint8_t coils[LIGHTMODBUS_STATIC_MEM_MASTER_DATA];
148 
149 
155  uint16_t regs[LIGHTMODBUS_STATIC_MEM_MASTER_DATA >> 1];
156  };
157  #else
158  //Two separate pointers are used in case pointer size differed between types (possible on some weird architectures)
159 
166  uint8_t *coils;
167 
173  uint16_t *regs;
174  #endif
175  } data;
176 
183  struct
184  {
185  uint8_t address;
186  uint8_t function;
188  } exception;
189 
192 
195 
196  //User defined functions
197  #ifdef LIGHTMODBUS_MASTER_USER_FUNCTIONS
198 
203  uint16_t userFunctionCount;
204  #endif
205 
206 } ModbusMaster;
207 #endif
208 
209 #include "master/mbregs.h"
210 #include "master/mbcoils.h"
211 
212 #ifdef LIGHTMODBUS_MASTER_BASE
213 
241 
247 extern ModbusError modbusMasterInit( ModbusMaster *status );
248 
254 extern ModbusError modbusMasterEnd( ModbusMaster *status );
255 #endif
256 
257 // For C++ (closes `extern "C"` )
258 #ifdef __cplusplus
259 }
260 #endif
261 
262 #endif
Associates user-defined response parser function with the function ID.
Definition: master.h:61
Master&#39;s coil-related frame building functions.
ModbusMasterUserFunction * userFunctions
A pointer to an array of user-defined Modbus functions.
Definition: master.h:202
ModbusError modbusMasterInit(ModbusMaster *status)
Performs initialization of the ModbusMaster structure.
Definition: master.c:216
uint8_t length
Length of the request frame in bytes.
Definition: master.h:96
uint16_t count
Number of data units (coils, registers, etc.)
Definition: master.h:132
uint8_t predictedResponseLength
Predicted number of response bytes to be received from slave upon succesful request.
Definition: master.h:74
uint16_t index
Modbus address of the first register/coil.
Definition: master.h:131
struct modbusMaster ModbusMaster
Represents Modbus master device&#39;s status and configuration.
A big union of structures used for parsing standard Modbus requests and building responses.
Definition: parser.h:43
Contains the modbusParser union used during frame creation and parsing.
enum modbusExceptionCode ModbusExceptionCode
Represents a Modbus exception code, defined by the standart.
enum modbusFrameError ModbusFrameError
Provides more information on frame building/parsing error.
uint16_t userFunctionCount
Number of the user functions in the array.
Definition: master.h:203
ModbusFrameError buildError
More precise information according encountered frame building error.
Definition: master.h:194
uint8_t * coils
A pointer to dynamically allocated memory for the received coils data.
Definition: master.h:166
ModbusDataType type
Type of data.
Definition: master.h:134
ModbusExceptionCode code
Exception code.
Definition: master.h:187
Master&#39;s register-related frame building functions.
ModbusFrameError parseError
More precise information according encountered frame parsing error.
Definition: master.h:191
ModbusError(* handler)(struct modbusMaster *status, ModbusParser *parser, ModbusParser *requestParser)
Pointer to the user defined parsing function.
Definition: master.h:64
uint8_t address
Addres of slave that sent in the data.
Definition: master.h:130
struct modbusMasterUserFunction ModbusMasterUserFunction
Associates user-defined response parser function with the function ID.
Core Modbus functions.
ModbusError modbusParseResponse(ModbusMaster *status)
Interprets the incoming response frame located in the master structure.
Definition: master.c:47
Represents Modbus master device&#39;s status and configuration.
Definition: master.h:71
uint16_t * regs
A pointer to dynamically allocated memory for the received registers data.
Definition: master.h:173
ModbusError modbusMasterEnd(ModbusMaster *status)
Frees memory used by the ModbusMaster structure, previously initialized with modbusMasterInit.
Definition: master.c:258
enum modbusDataType ModbusDataType
Stores information about Modbus data types.
enum modbusError ModbusError
Represents a library runtime error code.