FB_ModbusTcpClose
The FB_ModbusTcpClose function block closes a connection to a remote partner that was opened with the FB_ModbusTcpOpen function block. The number of IP connections that may be open is restricted to four, in order to maintain the use of resources at a minimum. If it is necessary to establish a further connection, a connection that is not currently in use must first be closed. A connection is automatically closed after about 10 seconds if it is not used during that period. A confirmation from the remote partner is not expected when a connection is closed. This means that under certain circumstances (e.g., if network communication has been interrupted) it can happen that a connection still exists. The remote partner should then close any such open connections. A Bus Controller operating as a remote partner closes such open connections automatically.
VAR_INPUT
VAR_INPUT
bStart : BOOL;
sRemoteIPAddr: STRING(15);
iMyPortNo : 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 whose connection is to be closed.
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.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
iErrorId : 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.
Error Codes | Description |
---|---|
0x8400 | Connection is already closed. |
0x8401 | Connection close timeout. |
0x8402 | Connection close resource error. |
Sample of calling the function block in FBD:
PROGRAM MAIN
VAR
fbModbusClose : FB_ModbusTcpClose;
bOpen : BOOL;
bBusy : BOOL;
bError : BOOL;
nErrId : WORD;
nConnectionPort: WORD;
END_VAR
In this sample, a rising edge at the bClose input causes a TCP/IP connection to the remote partner with network address '172.16.2.209' to be closed.
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v2.7.0 and below | BC9xxx (165) firmware version >= 0xB6 | TcModbusTCPBC.Lb6 |