Modbus protocol support

Modbus over RS-485

Modbus over TCP/IP

MWEB creates a TCP/IP socket, which listens on the port 502 and waits for the for the Modbus connection request from the server.

Currently only function reading registers is supported.
Modbus receiver tasks waits for the incoming packet of 12 bytes as defined by following data structures:


typedef struct {
WORD Trans_Id;
WORD Protocol_Id;
WORD Length;
BYTE Unit_Id;
}__attribute__ ((packed)) st_MBAP;

typedef struct {
BYTE Function;
WORD Start_Ad;
WORD NbReg;
}__attribute__ ((packed)) st_RdHoldReg;

typedef struct {
st_MBAP mbap;
st_RdHoldReg rdholdreg;
}__attribute__ ((packed)) st_TCPRdHoldReg;

Upper byte of the Start_Ad defines the C-Box number for the MWEB systems with multiple collector boxes.
Valid C-Box number is between 1 and 63.
The lower byte of the Start_Ad defines the element number and it can be any value between 1 and 126, except of the value 99 reserved for the broadcast address.

As a response MWEB sends a packet with following data structure:

typedef struct {
WORD Trans_Id;
WORD Protocol_Id;
WORD Length;
BYTE Unit_Id;
}__attribute__ ((packed)) st_MBAP;

typedef struct {
BYTE FctCode;
BYTE ByteCnt;
BYTE Status[250];
}__attribute__ ((packed)) st_WrOk;

typedef struct {
st_MBAP mbap;
st_WrOk WrOk;
}__attribute__ ((packed)) st_TCPWrOk;

For each device there are two bytes sent, first byte is always zero, and second byte represets device status according to the following enumaration:


enum modb {
MB_NOT_USED = 0
,MB_NO_INFORMATION
,MB_ELEMENT_NOT_SUPERVISED
,MB_CBOX_ERROR
,MB_ELEMENT_COMM_ERROR
,MB_BATTERY_DISCONNECTED
,MB_BATTERY_BAD
,MB_LAMPE_BAD
,MB_ELEMENT_OK
,MB_EMERGENCY_MODE
,MB_NOT_READY_FOR_TEST
,MB_IN_TEST
};

If there is an error in the incoming packet, an error response is sent with the following data packet.

typedef struct {
WORD Trans_Id;
WORD Protocol_Id;
WORD Length;
BYTE Unit_Id;
}__attribute__ ((packed)) st_MBAP;

typedef struct {
BYTE ErrCode;
BYTE Exception;
}__attribute__ ((packed)) st_WrErr;

typedef struct {
st_MBAP mbap;
st_WrErr WrErr;
}__attribute__ ((packed)) st_TCPWrErr;

Error codes are defined by followinf enumeration

enum errorcode {
ERR_FUNCTION_CODE = 1
,ERR_ADDRESS
,ERR_LENGTH
};

Sander Elektronik AG © 2023 Frontier Theme