1#ifndef LIGHTMODBUS_SLAVE_FUNC_IMPL_H
2#define LIGHTMODBUS_SLAVE_FUNC_IMPL_H
24 const uint8_t *requestPDU,
25 uint8_t requestLength)
28 if (requestLength != 5)
65 uint16_t index =
modbusRBE(&requestPDU[1]);
66 uint16_t count =
modbusRBE(&requestPDU[3]);
69 if (count == 0 || count > maxCount)
87 for (uint16_t i = 0; i < count; i++)
89 cargs.
index = index + i;
105 for (uint8_t i = 0; i < dataLength; i++)
109 for (uint16_t i = 0; i < count; i++)
111 cargs.
index = index + i;
134 const uint8_t *requestPDU,
135 uint8_t requestLength)
138 if (requestLength != 5)
143 uint16_t index =
modbusRBE(&requestPDU[1]);
144 uint16_t value =
modbusRBE(&requestPDU[3]);
147 if (datatype ==
MODBUS_COIL && value != 0x0000 && value != 0xFF00)
156 .value = (uint16_t)((datatype ==
MODBUS_COIL) ? (value != 0) : value),
157 .function = function,
193 const uint8_t *requestPDU,
194 uint8_t requestLength)
197 if (requestLength < 6)
202 uint16_t maxCount = datatype ==
MODBUS_COIL ? 1968 : 123;
203 uint16_t index =
modbusRBE(&requestPDU[1]);
204 uint16_t count =
modbusRBE(&requestPDU[3]);
205 uint8_t declaredLength = requestPDU[5];
208 if (declaredLength == 0 || declaredLength != requestLength - 6)
228 .function = function,
232 for (uint16_t i = 0; i < count; i++)
234 cargs.
index = index + i;
243 for (uint16_t i = 0; i < count; i++)
245 cargs.
index = index + i;
273 const uint8_t *requestPDU,
274 uint8_t requestLength)
277 if (requestLength != 7)
281 uint16_t index =
modbusRBE(&requestPDU[1]);
282 uint16_t andmask =
modbusRBE(&requestPDU[3]);
283 uint16_t ormask =
modbusRBE(&requestPDU[5]);
292 .function = function,
304 uint16_t value = cres.
value;
307 value = (value & andmask) | (ormask & ~andmask);
@ MODBUS_EXCEP_ILLEGAL_ADDRESS
Illegal data address.
Definition base.h:233
@ MODBUS_EXCEP_SLAVE_FAILURE
Slave could not process the request.
Definition base.h:235
@ MODBUS_EXCEP_ILLEGAL_VALUE
Illegal data value.
Definition base.h:234
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
ModbusError
Represtents different kinds of errors.
Definition base.h:137
#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
Slave's types and basic functions (header)
ModbusErrorInfo modbusBuildException(ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
Builds an exception response frame.
Definition slave.impl.h:118
static ModbusError modbusSlaveAllocateResponse(ModbusSlave *status, uint16_t pduSize)
Allocates memory for slave's response frame.
Definition slave.h:184
@ MODBUS_REGQ_W_CHECK
Request for write access.
Definition slave.h:39
@ MODBUS_REGQ_W
Write request.
Definition slave.h:41
@ MODBUS_REGQ_R_CHECK
Request for read access.
Definition slave.h:38
@ MODBUS_REGQ_R
Read request.
Definition slave.h:40
Slave's functions for parsing requests (header)
ModbusErrorInfo modbusParseRequest1516(ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)
Handles requests 15 and 16 (Write Multiple XX) and generates response.
Definition slave_func.impl.h:190
ModbusErrorInfo modbusParseRequest0506(ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)
Handles requests 05 and 06 (Write Single XX) and generates response.
Definition slave_func.impl.h:131
ModbusErrorInfo modbusParseRequest22(ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)
Handles request 22 (Mask Write Register) and generates response.
Definition slave_func.impl.h:270
ModbusErrorInfo modbusParseRequest01020304(ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)
Handles requests 01, 02, 03 and 04 (Read Multiple XX) and generates response.
Definition slave_func.impl.h:21
uint8_t * pdu
A pointer to the PDU section of the frame.
Definition base.h:281
Contains arguments for the register callback function.
Definition slave.h:48
uint16_t value
Value of the register.
Definition slave.h:52
ModbusDataType type
Type of accessed data.
Definition slave.h:49
ModbusRegisterQuery query
Type of request made to the register.
Definition slave.h:50
uint16_t index
Index of the register.
Definition slave.h:51
Contains values returned by the slave register callback.
Definition slave.h:60
uint16_t value
Register/coil value.
Definition slave.h:62
ModbusExceptionCode exceptionCode
Exception to be reported.
Definition slave.h:61
Slave device status.
Definition slave.h:90
ModbusRegisterCallback registerCallback
A pointer to register callback (required)
Definition slave.h:91
ModbusBuffer response
Stores slave's response to master.
Definition slave.h:97