FB_ModbusTcpRequest
The FB_ModbusTcpRequest function block can send a MODBUS request to a remote partner. The connection required for this must first have been opened with the FB_ModbusTcpOpen function block. The dynamic port number returned as the connection is established is passed as an input parameter to the iMyPortNo variable. Connections that are not used are automatically closed after about 10 seconds, or may be closed by the remote partner. If there is no connection, error code 0x8305 is returned, for example, to the iErrorId output. The FB_ModbusTcpRequest function block transmits the entire MODBUS frame, beginning with the UNIT ID. The TCP section is inserted by the BC9xxx Bus Controller at the start.
VAR_INPUT
VAR_INPUT
bStart : BOOL;
sRemoteIPAddr: STRING(15);
iMyPortNo : WORD;
pReqBuff : DWORD;
cbReqLen : WORD;
pResBuff : DWORD;
cbResLen : WORD;
END_VAR
bStart: the function block is activated by a rising edge at this input.
sRemoteIPAddr: this variable is a string containing the network address (IP address) of the remote partner.
iMyPortNo: the port number associated with the connection is passed here as a parameter. The port number is generated afresh (dynamically) each time the connection is established with the FB_ModbusTcpOpen function block.
pReqBuff : contains the address of the buffer with the MODBUS request data. It is the programmer who is responsible for dimensioning the buffer appropriately so that cbReqLen bytes can be removed from it. The buffer can be a single variable, an array or a structure, whose address can be found with the ADR operator.
cbReqLen : byte length of the MODBUS request data in the pReqBuff-buffer.
pResBuff : contains the address of the buffer with the MODBUS response data. The programmer is himself responsible for dimensioning the buffer to a size that can accept cbResLen bytes. The buffer can be a single variable, an array or a structure, whose address can be found with the ADR operator.
cbResLen : byte length of the MODBUS response buffer. It is the programmer who is responsible for dimensioning the buffer to such a size that all the response data can be accepted.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
iErrorId : WORD;
cbResponse : WORD;
END_VAR
bBusy: if the function block is activated, this output is set. It remains set until a feedback is received.
bError: if an error should occur during the execution of the command, then this output is set, after the bBusy output has been reset.
iErrorId: supplies the command-specific error code (table) or the device-specific error code if the bError output is set.
cbResponse : the byte length of the response data actually returned in the response buffer.
Error Codes | Description |
---|---|
0x8300 | Tx error. Timeout error. |
0x8301 | Tx error. Remote shutdown. |
0x8302 | Tx error. Internal error. |
0x8303 | Tx error. Resource error. |
0x8305 | Tx error. No remote connection. |
0x8100 | Rx error. Wrong transaction ID. |
0x8101 | Rx error. Wrong protocol ID. |
0x8102 | Rx error. Wrong length. |
0x8103 | Rx error. Exceeding length. |
0x8104 | Rx error. Wrong unit ID. |
Sample of calling the function block in FBD:
PROGRAM MAIN
VAR
fbModbusRequest : FB_ModbusTcpRequest;
bExecute : BOOL;
bExecute : nConnectionPort;
SendBuffer : ARRAY[0..5] OF BYTE := 1,6,64,64,0,0;
ReceiveBuffer : ARRAY[0..255] OF BYTE;
bBusy : BOOL;
bError : BOOL;
nErrId : WORD;
cbReceived : WORD;
END_VAR
In this sample, a rising edge at the bExecute input causes a request to be sent to the remote partner with network address '172.16.2.209'.
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v2.7.0 and below | BC9xxx (165) firmware version >= 0xB6 | TcModbusTCPBC.Lb6 |