1#ifndef LIGHTMODBUS_MASTER_FUNC_IMPL_H
2#define LIGHTMODBUS_MASTER_FUNC_IMPL_H
32 const uint8_t *requestPDU,
33 uint8_t requestLength,
34 const uint8_t *responsePDU,
35 uint8_t responseLength)
75 uint16_t index =
modbusRBE(&requestPDU[1]);
76 uint16_t count =
modbusRBE(&requestPDU[3]);
79 if (count == 0 || count > maxCount)
91 if (responsePDU[1] != expected || responseLength != expected + 2)
104 for (uint16_t i = 0; i < count; i++)
106 cargs.
index = index + i;
136 const uint8_t *requestPDU,
137 uint8_t requestLength,
138 const uint8_t *responsePDU,
139 uint8_t responseLength)
176 const uint8_t *requestPDU,
177 uint8_t requestLength,
178 const uint8_t *responsePDU,
179 uint8_t responseLength)
185 uint16_t index =
modbusRBE(&requestPDU[1]);
186 uint16_t count =
modbusRBE(&requestPDU[3]);
197 uint16_t maxCount = (function == 15) ? 1968 : 123;
198 if (count == 0 || count > maxCount)
203 if (requestLength != expected + 6)
230 const uint8_t *requestPDU,
231 uint8_t requestLength,
232 const uint8_t *responsePDU,
233 uint8_t responseLength)
241 for (uint8_t i = 0; ok && i < 7; i++)
242 ok = ok && (responsePDU[i] == requestPDU[i]);
285 if (count == 0 || count > maxCount)
317 if (function != 5 && function != 6)
322 value = value ? 0xff00 : 0;
348 const uint8_t *values)
351 if (count == 0 || count > 1968)
364 uint8_t n = count >> 3;
365 uint8_t r = count & 7;
368 for (uint8_t i = 0; i < n; i++)
375 for (uint8_t i = 0; i < r; i++)
404 const uint16_t *values)
407 if (count == 0 || count > 123)
414 uint8_t dataLength = count << 1;
420 for (uint8_t i = 0; i < (uint8_t)count; i++)
#define MODBUS_REQUEST_ERROR(e)
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_REQUESTL and the error code i...
Definition base.h:100
#define MODBUS_RESPONSE_ERROR(e)
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_RESPONSE and the error code i...
Definition base.h:107
ModbusDataType
Represents different Modbus data types.
Definition base.h:244
@ MODBUS_DISCRETE_INPUT
Discrete input.
Definition base.h:248
@ MODBUS_HOLDING_REGISTER
Holding register.
Definition base.h:245
@ MODBUS_INPUT_REGISTER
Input register.
Definition base.h:246
@ MODBUS_COIL
Coil.
Definition base.h:247
#define MODBUS_NO_ERROR()
Construcs a ModbusErrorInfo object for which modbusIsOK() is guaranteed to return true.
Definition base.h:86
static void modbusMaskWrite(uint8_t *mask, uint16_t n, uint8_t value)
Writes n-th bit in an array.
Definition base.h:344
static uint8_t modbusCheckRangeU16(uint16_t index, uint16_t count)
Checks whether provided address range causes an uint16_t overflow.
Definition base.h:408
static uint16_t modbusWBE(uint8_t *p, uint16_t val)
Safely writes a big-endian 16-bit word to provided pointer.
Definition base.h:395
#define LIGHTMODBUS_RET_ERROR
Return type for library functions returning ModbusErrorInfo that should be handled properly.
Definition base.h:49
#define MODBUS_GENERAL_ERROR(e)
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_GENERAL and the error code is...
Definition base.h:93
static uint16_t modbusRBE(const uint8_t *p)
Safely reads a big-endian 16-bit word from provided pointer.
Definition base.h:385
static uint16_t modbusBitsToBytes(uint16_t n)
Returns number of bytes necessary to hold given number of bits.
Definition base.h:357
static uint8_t modbusMaskRead(const uint8_t *mask, uint16_t n)
Reads n-th bit from an array.
Definition base.h:333
Master's types and basic functions (header)
static ModbusError modbusMasterAllocateRequest(ModbusMaster *status, uint16_t pduSize)
Allocates memory for the request frame.
Definition master.h:162
Master's functions for building requests and parsing responses (header)
ModbusErrorInfo modbusParseResponse1516(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to requests 15 and 16 (write mutliple regsiters/coils)
Definition master_func.impl.h:172
ModbusErrorInfo modbusParseResponse0506(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to requests 05 and 06.
Definition master_func.impl.h:132
ModbusErrorInfo modbusBuildRequest0506(ModbusMaster *status, uint8_t function, uint16_t index, uint16_t value)
Write single coil/holding register.
Definition master_func.impl.h:311
ModbusErrorInfo modbusBuildRequest22(ModbusMaster *status, uint16_t index, uint16_t andmask, uint16_t ormask)
Mask write register request.
Definition master_func.impl.h:438
ModbusErrorInfo modbusParseResponse22(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to request 22.
Definition master_func.impl.h:226
ModbusErrorInfo modbusBuildRequest16(ModbusMaster *status, uint16_t index, uint16_t count, const uint16_t *values)
Write multiple holding registers.
Definition master_func.impl.h:400
ModbusErrorInfo modbusBuildRequest01020304(ModbusMaster *status, uint8_t function, uint16_t index, uint16_t count)
Read mutiple coils/discrete inputs/holding registers/input registers.
Definition master_func.impl.h:261
ModbusErrorInfo modbusParseResponse01020304(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to requests 01, 02, 03 and 04.
Definition master_func.impl.h:28
ModbusErrorInfo modbusBuildRequest15(ModbusMaster *status, uint16_t index, uint16_t count, const uint8_t *values)
Write multiple coils.
Definition master_func.impl.h:344
uint8_t * pdu
A pointer to the PDU section of the frame.
Definition base.h:281
Arguments for the data callback.
Definition master.h:40
uint16_t value
Value of the register.
Definition master.h:43
uint16_t index
Index of the register.
Definition master.h:42
ModbusDataType type
Type of Modbus register.
Definition master.h:41
Master device status.
Definition master.h:73
ModbusDataCallback dataCallback
A pointer to data callback (required)
Definition master.h:74
ModbusBuffer request
Stores master's request for slave.
Definition master.h:81