The Modbus communication protocol is a commonly used protocol in industrial automation, which allows data exchange between master devices (usually clients) and slave devices (usually servers). In Modbus communication, the server (slave device) must generate appropriate responses to process requests and send exception responses when necessary. This article will delve into the generation and exception handling of Modbus responses.
ModbusResponse Type
Modbusin communication, According to the processing result of the request, Can generate two types of responses:
- affirmativeModbusResponse: This is a normal response, Indicates that the server has successfully processed the request. under these circumstances, The response function code is the same as the requested function code.
- ModbusException Response: This response is used to indicate that an error or abnormal situation has occurred. The function code for abnormal response is equal to the requested function code plus0x80, So that the client can recognize abnormal responses.
ModbusPurpose of abnormal response
ModbusThe main purpose of abnormal response is to provide the client with information related to errors detected during request processing. It helps clients understand the specific causes of communication issues, Thus taking appropriate measures to address these issues.
ModbusException code for abnormal response
The exception response contains an exception code, Used to clearly indicate the reason for errors. The following are commonModbusException codes and their meanings:
- Exception Code 01 (0x01) - Illegal function codes: Indicates that the server is not aware of or unable to process the function code in the request. Usually means that the device does not support this feature code.
- Exception Code 02 (0x02) - Illegal data address: Indicate that the data address included in the request is not within the authorized address range of the slave device, Therefore, the requested address is invalid.
- Exception Code 03 (0x03) - Illegal data values: The data value contained in the request is not within the allowed range of the slave device, Therefore, the data value is invalid.
- Exception Code 04 (0x04) - Server malfunction: Indicates that the server experienced an irreparable failure while executing the requested operation, Cannot continue operation.
- Exception Code 05 (0x05) - Confirm: Indicates that the server has received the request, But more time is needed to complete the operation. The client should wait for the server to complete processing.
- Exception Code 06 (0x06) - Server busy: Indicates that the server is busy processing other commands, The client should resend the request later.
- Exception Code 0A (0x0A) - Gateway failure: The path indicating the gateway or router is invalid, Possible gateway overload or incorrect configuration.
- Exception Code 0B (0x0B) - Gateway target device failed to respond: Indicates that the target cannot be found in the network from the device.
GenerateModbusResponse
To generateModbusResponse, The server must first parse the request, Then construct a response frame based on the content of the request and the processing result. The response frame must be inMBAPThe message header is a prefix, Generate data from transaction processing bodyMBAPMessage header. The specific steps include:
- Copy the unit identifier in the request, And store it in the body of the transaction processing.
- CalculationModbus PDUAnd the size of the unit identifier field, And set the value in the 'Length' field.
- Set the protocol identifier field to0x0000 (expressModbusagreement) .
- Set the transaction identifier field to a value related to the initial request, And store it.
generatedModbusThe response frame needs to pass throughTCPConnect and send to the correct oneModbusclient, To complete the communication process.
Summary:
Modbusin communication, Generating appropriate responses is crucial for ensuring successful communication. Both normal and abnormal responses have their specific function codes and exception codes, So that the client can understand the results of request processing and the errors that occur. The server must generate a response according to the protocol specifications, And send abnormal responses when needed, To ensure the reliability and stability of communication.
Leave a Reply