FB_ModbusTcpOpen
The function block opens a TCP/IP connection to a remote partner on the MODBUS/TCP port. To keep resource consumption to a minimum, the maximum number of open connections has been limited to 4. If it is necessary to establish a further connection, a connection that is not currently in use can first be closed using the FB_ModbusTcpClose function block. If successful, the FB_ModbusTcpOpen function block supplies a port number in the iMyPortNo output variable. This port number is required in order to send a MODBUS request using either of the FB_ModbusTcpRequest or FB_ModbusTcpClose function blocks, e.g. to close the connection. When opening the connection, the tTimeout input parameter is also used to specify the time-out period for the MODBUS request and for the disconnection. After about 10 seconds, a connection that has been opened but has not been used during that period is automatically closed, and must be reopened if needed.
VAR_INPUT
VAR_INPUT
bStart : BOOL;
sRemoteIPAddr: STRING(15);
tTimeOut : TIME;
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.
tTimeOut: indicates the time, which must not be exceeded during command execution.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
iErrorId : WORD;
iMyPortNo : 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.
iMyPortNo : when successful, the port number associated with the connection is returned at this output. The port number is a dynamic number, generated afresh each time the connection is established.
Error Codes | Description |
---|---|
0x8500 | Resource error |
0x8501 | No socket available |
0x8502 | Open connection error |
Sample of calling the function block in FBD:
PROGRAM MAIN
VAR
fbModbusOpen : FB_ModbusTcpOpen;
bOpen : BOOL;
bBusy : BOOL;
bError : BOOL;
nErrId : WORD;
nConnectionPort: WORD;
END_VAR
In this sample, a rising edge at the bOpen input causes a TCP/IP connection to the remote partner with network address '172.16.2.209' to be established.
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v2.7.0 and below | BC9xxx (165) firmware version >= 0xB6 | TcModbusTCPBC.Lb6 |