// This program uses the AsTCP standard library for TCP communication
PROGRAM _CYCLIC
VAR
// TCP Function Blocks
TcpClient1 : TcpOpen; // TCP client instance
TcpSend1 : TcpSend; // TCP send instance
TcpRecv1 : TcpRecv; // TCP receive instance
TcpClose1 : TcpClose; // TCP close instance
// Communication Variables
Hostname : STRING[50] := '192.168.1.10'; // Hostname for socket connection
PortNumber : UINT := 502; // Port number
ConnectionID : DINT := -1; // Connection ID for AsTCP
IsConnected : BOOL := FALSE; // Connection status
WaitForAnswer : BOOL := FALSE; // Flag for waiting for answer
Command : BYTE; // Current Command
BytesToRead : UINT := 0; // Bytes to read
SendDataBuffer : ARRAY[0..255] OF BYTE; // Buffer for sending data
RecvDataBuffer : ARRAY[0..255] OF BYTE; // Buffer for receiving data
Status : UDINT; // Status variable for TCP operations
// Application-Specific Variables
Init_Data : INIT_DATA; // Struct for initialization data
ESA_Init : ESA_INIT; // Struct for ESA data
END_VAR
// Open Socket Connection using AsTCP
IF NOT IsConnected THEN
TcpClient1.enable := TRUE;
TcpClient1.pHost := ADR(Hostname); // Pointer to the hostname or IP address
TcpClient1.port := PortNumber; // Port number
TcpClient1.flags := 0; // Default connection flags
TcpClient1.pStatus := ADR(Status); // Pointer to status variable
TcpClient1.ident := ConnectionID; // Connection ID (output)
TcpOpen(TcpClient1);
IF TcpClient1.status = 0 THEN
IsConnected := TRUE;
ConnectionID := TcpClient1.ident;
Command := 16#31; // Example Command
BytesToRead := 1;
WaitForAnswer := TRUE;
END_IF;
// Close Connection (Optional)
IF WaitForAnswer AND NOT TcpRecv1.enable THEN
TcpClose1.enable := TRUE;
TcpClose1.ident := ConnectionID;
TcpClose1.pStatus := ADR(Status);
TcpClose(TcpClose1);
IF TcpClose1.status = 0 THEN
IsConnected := FALSE;
ConnectionID := -1;
END_IF;
I have remarked that portion out and declared them manually .
now I get this issue
// Open Socket Connection using AsTCP
IF NOT IsConnected THEN
TcpClient1.enable := TRUE;
TcpClient1.pIfAddr := ADR(Hostname); // Pointer to the hostname or IP address
TcpClient1.port := PortNumber; // Port number
TcpClient1.options := 0; // Default connection flags
TcpClient1.status := ADR(OStatus); // Pointer to status variable
TcpClient1.ident := ConnectionID; // Connection ID (output)
Data type mismatch: Cannot convert UDINT to UINT.|1140|Ln: 49, Col: 21|C:\projects\OptibendESAnCYB_20241213_VALMONT_M6041\Logical\LaserCheck\LCTCP\Main.st|
No write access to variable ‘ident’ allowed.|1138|Ln: 50, Col: 20|C:\projects\OptibendESAnCYB_20241213_VALMONT_M6041\Logical\LaserCheck\LCTCP\Main.st|
Conversation you have to solve in your code. Since you can not copy longer data type to smaller one. And second problem, looks like you are trying to write ID to FUB output. This is not allowed.
OK, so you have a user application library. That is fine. Still, data type conversion is the programmer’s responsibility. And you can not write to the FUB output. So, you have to solve this in your code as well.