1 #ifndef MODBUS_H_INCLUDED 2 #define MODBUS_H_INCLUDED 88 #define MB_SLAVEID_BUF_LEN 32 90 #define MB_SER_PDU_SIZE_MAX 255 92 #define MB_TFILE_TARGET_SIZE 31 93 #define MB_TFILE_FNAMELEN_MAX 32
94 #define MB_TFILE_LAST_PACKET (1 << 31)
95 #define MB_TFILE_PART_REGADDR 0xFF00
101 #define MB_ADDRESS_BROADCAST ( 0 ) 102 #define MB_ADDRESS_MIN ( 1 )
103 #define MB_ADDRESS_MAX ( 247 )
104 #define MB_TCP_PSEUDO_ADDRESS ( 255 )
110 #define MB_FUNC_NONE (0x00) 111 #define MB_FUNC_READ_COILS (0x01)
112 #define MB_FUNC_READ_DISCRETE_INPUTS (0x02)
113 #define MB_FUNC_READ_HOLDING_REGISTER (0x03)
114 #define MB_FUNC_READ_INPUT_REGISTER (0x04)
115 #define MB_FUNC_WRITE_SINGLE_COIL (0x05)
116 #define MB_FUNC_WRITE_REGISTER (0x06)
117 #define MB_FUNC_DIAG_READ_EXCEPTION (0x07)
118 #define MB_FUNC_DIAG_DIAGNOSTIC (0x08)
119 #define MB_FUNC_DIAG_GET_COM_EVENT_CNT (0x0B)
120 #define MB_FUNC_DIAG_GET_COM_EVENT_LOG (0x0C)
121 #define MB_FUNC_WRITE_MULTIPLE_COILS (0x0F)
122 #define MB_FUNC_WRITE_MULTIPLE_REGISTERS (0x10)
123 #define MB_FUNC_REPORT_SERVER_ID (0x11)
124 #define MB_FUNC_READ_FILE_RECORD (0x14)
125 #define MB_FUNC_WRITE_FILE_RECORD (0x15)
126 #define MB_FUNC_READWRITE_MULTIPLE_REGISTERS (0x17)
127 #define MB_FUNC_TRANSFER_FILE (0x64)
128 #define MB_FUNC_TRANSFER_FILE_COMPLETED (0x65)
129 #define MB_FUNC_NEW_FILE (0x66)
130 #define MB_FUNC_ERROR (0x80)
422 struct process Process;
574 uint16_t RegAddr, uint16_t RegData, int32_t TimeOut);
599 uint16_t RegAddr, uint16_t NRegs,
600 uint16_t* DataBuffer, int32_t TimeOut);
624 uint16_t RegAddr, uint16_t NRegs, int32_t TimeOut);
650 uint16_t FileNum, uint16_t RecNum,
651 uint8_t* Data, uint16_t DataLen,
678 uint32_t FileSize, uint32_t Crc32,
679 uint8_t* FileName, int32_t TimeOut);
706 uint32_t PartNumber, uint8_t PartSize,
707 uint8_t* Data, int32_t TimeOut);
730 uint32_t PartNumber, int32_t TimeOut);
734 #endif // MODBUS_H_INCLUDED uint32_t RespTODefault
Default client' timeout value in ms (can overrides in each command)
modbus_state_t structure holds internal state of the Modbus driver.
uint8_t * MbFrame
Current pointer to the processing/assembling Modbus frame.
uint8_t Active
Transfer file operation is in progress (even if the actual function is read reg)
Header file for the EVE millisecond-scale work scheduling.
MbException_t(* MbVfuncStart_t)(const struct modbus_t *Mb, void *data)
Callback specific to the protocol and called on the protocol start.
Modbus instance was not initialised. Use MbInit().
ModbusLib INTERNAL exception: Master is busy (awaiting response) or UART is busy (sending/receiving d...
const struct modbus_t * Module
Pointer to the Modbus driver instance structure.
Modbus event: Frame received.
MbException_t MbReqTransferFileData(const struct modbus_t *Mb, uint32_t PartNumber, uint8_t PartSize, uint8_t *Data, int32_t TimeOut)
Request to transfer file data.
MbException_t MbReqWriteHolding(const struct modbus_t *Mb, uint8_t DestAddr, uint16_t RegAddr, uint16_t RegData, int32_t TimeOut)
Request to write holding register.
uint8_t RcvState
UART receive state for RTU and ASCII.
#define MB_SLAVEID_BUF_LEN
Number of bytes which should be allocated for the Report Slave ID command.
MbException_t MbInit(const struct modbus_t *Mb, uint8_t MbAddress)
Initialise resources used by the protocol stack instance.
uint8_t LastFunction
For client only: function of last sent packet.
MbRole_t Role
Instance role on the Modbus: Client, Server or Sniffer.
Modbus event: Frame sent.
Continue file transfer procedure.
Modbus instance is busy with data transfer, or (for client) awaiting reply.
mb_transfer_file_t TransferFile
Variables related to Transfer File function.
MbState_t
Modbus driver state.
uint16_t UartSndBufCount
Counter of bytes sent from the UartBuf.
MbSessionClosed_t MbSessionClosedCb
Callback being called on the command processing end (reporting overall success/error) ...
MbVfuncStop_t MbStop
Function to call on the protocol stop.
Modbus event: Startup finished.
Structure for holding a number of protocol specific virtual functions. UART related functions are the...
MbException_t(* MbHoldingHandler_t)(const struct modbus_t *Mb, uint16_t SlaveAddr, uint16_t RegAddr, uint16_t *Value)
Callbacks specific to the Read/Write Holding Register(s) command. They're called when Modbus command ...
ModbusLib INTERNAL exception: Transfer File function is done.
An unrecoverable error occurred while the server was attempting to perform the requested action...
MbException_t MbReqWriteFile(const struct modbus_t *Mb, uint8_t DestAddr, uint16_t FileNum, uint16_t RecNum, uint8_t *Data, uint16_t DataLen, int32_t TimeOut)
Request to write file.
ModbusLib INTERNAL exception: protocol stack in illegal state.
#define MB_SER_PDU_SIZE_MAX
Maximum size of a Modbus RTU frame. 1 byte less than standard because of internal UART limitation...
uint8_t * UartSndBufCur
Current sending pointer in the UartBuf.
The client should retransmit the message later when the server is free.
uint8_t DestAddress
Slave address of the last sent Modbus frame (client only)
uint32_t FileSize
Total file size.
Modbus instance is listening remote part and ready for transmission.
MbException_t(* MbVfuncReceive_t)(const struct modbus_t *Mb)
Callbacks specific to the protocol and called when the driver wants to receive data.
uint16_t SlaveIdLen
Length of the Slave ID string.
uint8_t RcvAddress
Slave address of the currently processing Modbus frame.
uint16_t UartRcvBufPos
Counter of bytes received to the UartBuf.
MbException_t Exception
Last communication status.
MbRole_t
Modbus device role (MASTER/SLAVE/SNIFFER).
The server has accepted the request and is processing it, but a long duration of time will be require...
MbException_t(* MbTransferFileHandler_t)(const struct modbus_t *Mb, uint16_t SlaveAddr, mb_transfer_file_t *TransferFile)
Callback specific to the Transfer File command. It is called when Modbus command requests to write a ...
void * Transport
Pointer to the structure holding transport instance (UART, TCP, TLS...)
MbException_t MbReqWriteMultiple(const struct modbus_t *Mb, uint8_t DestAddr, uint16_t RegAddr, uint16_t NRegs, uint16_t *DataBuffer, int32_t TimeOut)
Request to write multiple holding registers.
In conjunction with gateways: indicates that the gateway was unable to allocate an internal communica...
MbVfuncStart_t MbStart
Function to call on the protocol start.
uint8_t PartLen
Current packet file part length.
void * FunctionHandler
Function to call. Function type must follow the driver plugin specification for the command...
const struct MbVirtualfunc_t MbVirtualFuncRtu
Virtual functions for the UART protocol. Included in the driver and ready to be included in modbus_t ...
uint16_t DestReg
First destination register of the last sent Read command (client only)
uint32_t FilePtr
Bytes already transmitted. Used by client to determine the size of the last part. ...
const struct MbFunctionDescriptor_t * FuncHandlers
Callbacks specific to the command and being called during the command processing to perform command/u...
MbException_t
Modbus standard exception codes plus internal error codes used by all function in the protocol stack...
Modbus instance was initialised but currently disabled. Use MbEnable().
uint8_t MyAddress
Slave address of the interface on the Modbus (semistatic, assigned in MbInit() function call) ...
MbException_t MbClose(const struct modbus_t *Mb)
Release resources used by the protocol stack instance.
#define MB_TFILE_FNAMELEN_MAX
Length of the buffer holding filename for Transfer File function. As file name is null-terminated...
MbException_t(* MbVfuncStop_t)(const struct modbus_t *Mb)
Callback specific to the protocol and called on the protocol stop.
uint32_t PartNum
Last file part transmitted (for client) or received (for server). It is 0 for file header...
void(* MbNewFileHandler_t)(const struct modbus_t *Mb, uint8_t *FileName)
Callback specific to the Transfer File command. It is called after a new file arrives to server or sn...
MbException_t(* MbTransferFileCompletedHandler_t)(const struct modbus_t *Mb, uint16_t SlaveAddr, uint32_t PartNumber)
Callback specific to the Transfer File Completed command.
uint8_t FunctionCode
Modbus command code as per Modbus spec.
The function code received in the query is not an allowable action for the server.
MbMode_t
Modbus serial transmission modes (RTU/ASCII/TCP).
Header file for the EVE microsecond-scale work scheduling.
In conjunction with gateways: Specialized use in conjunction with gateways, indicates that no respons...
struct modbus_state_t * State
Pointer to the Modbus driver instance state structure.
MbException_t(* MbSessionClosed_t)(const struct modbus_t *Mb, MbException_t Error)
Callback called on the command processing end (reporting overall success/error)
const struct modbus_t * MbClient
For sniffer only. Modbus client instance on the same device (for confirmations sending) ...
const struct MbVirtualfunc_t MbVirtualFuncTcp
Virtual functions for the standard unsecured TCP protocol (using socket API). Included in the driver ...
In conjunction with function codes 20 and 21: The server attempted to read record file...
const struct MbVirtualfunc_t * Virtualfunc
Callbacks specific to the protocol to start/stop the protocol, receive/send data. For UART these func...
#define MB_TFILE_TARGET_SIZE
Length of bit vector of targets for Transfer File function.
uint8_t data[USBNET_RX_BUF_SIZE]
Modbus event: Request to send frame.
uint32_t TimerT35
UART 3.5 chars timeout value in us.
ModbusLib INTERNAL exception: Communication error (unexpected or invalid frame).
MbEvent_t
Modbus events used in ModbusServerProcess and ModbusClientProcess.
Modbus event: Data exchange ended.
uint32_t CurCrc32
CRC32 of file name + file length + part of data received so far. Used by server to discover possible ...
MbException_t MbReqTransferFileCompleted(const struct modbus_t *Mb, uint8_t MyAddress, uint32_t PartNumber, int32_t TimeOut)
Request to send "transfer file complete" message.
uint32_t CurSize
Bytes already received. Used by server to discover possible file size error.
MbException_t MbReqTransferFile(const struct modbus_t *Mb, uint8_t *Targets, uint32_t FileSize, uint32_t Crc32, uint8_t *FileName, int32_t TimeOut)
Request to transfer file header.
uint8_t SndState
UART send state for RTU and ASCII.
Structure for holding a callback serving one of Modbus commands. A pointer of array of these structur...
uint8_t * PartData
Pointer to current part data.
illegal register address.
ModbusLib INTERNAL exception: timeout error occurred.
MbVfuncReceive_t MbReceiveData
Function to call in order to send data (see MbVfuncReceive_t)
uint32_t RespTO
Client' timeout value in ms.
modbus_t structure is used for configuration of Modbus instance. The structure must be initialized be...
3.5 chars timeout expired
MbException_t(* MbVfuncSend_t)(const struct modbus_t *Mb)
Callbacks specific to the protocol and called when the driver wants to send data. ...
uint16_t MbFrameLen
Current length of the processing/assembling Modbus frame.
MbException_t MbReqReadHolding(const struct modbus_t *Mb, uint8_t DestAddr, uint16_t RegAddr, uint16_t NRegs, int32_t TimeOut)
Request to read holding registers.
MbException_t MbDisable(const struct modbus_t *Mb)
Disable the Modbus protocol stack instance.
MbMode_t Protocol
Protocol type for the instance: TCP, RTU or ASCII.
uint32_t FileCrc32
CRC32 of file name (w/o trailing zeroes) + file length + file data. Computed by crc32_compute() funct...
MbException_t MbEnable(const struct modbus_t *Mb, void *Data)
Enable the Modbus protocol stack instance.
MbVfuncSend_t MbSendData
Function to call in order to send data (see MbVfuncSend_t)
MbState_t MbState
State of the instance (disables/enabled/busy)
Structure for holding variables related to Transfer File function.