I have a question regarding the TCP IP communication between a IRC5 (Client) and a B&R CPU (Server).
The B&R Server is sending an UDINT array containing some cordinates and status bits. This being my first TCP IP and kind of my first real Robotstudio program, I’m having a hard time working out the details.
Is receiving an UDINT array in Robotstudio as simple as just converting the rawdata to udint array?
Following code is what I have setup right now regarding the TCP Client.
//-------------------------------------------------------------
MODULE TCP_Client
PERS dnum TransferData_ToRobot {24};
PERS dnum TransferData_FromRobot {47};
VAR socketdev ClientSocket;
VAR socketstatus SocketStat;
VAR rawbytes raw_send;
VAR rawbytes raw_receive;
VAR bool diComms_Alive;
VAR num diForceStop;
VAR num doCommsError;
PROC Main()
VAR signaldi PLCAlive;
AliasIO diComms_Alive, PLCAlive;
!wait for plc to run
WHILE PLCAlive = 0 DO
WaitTime 5;
set doCommsError;
IF (diForceStop = 1) Stop; !force stop prog execution from pendant. diforcestop is on virtual_1 unit
ENDWHILE
!reset error
Reset doCommsError;
!clear raw data
ClearRawBytes raw_send;
ClearRawBytes raw_receive;
SocketClose ClientSocket;
!get client socket status
SocketStat := SocketGetStatus(ClientSocket);
WHILE NOT (SocketStat = SOCKET_CONNECTED) DO
WaitTime 2; !wait time before closing socket
SocketClose ClientSocket;
WaitTime 2; ! wait time before opening socket
IF (PLCAlive = 0) ExitCycle; !plc not running - wait for plc to come online
ConnectToServer “10.30.1.11”, 1025; !connect to server addr and port no
SocketStat := SocketGetStatus(ClientSocket); !update client socket status
ENDWHILE
WHILE (SocketStat = SOCKET_CONNECTED) DO
IF (PLCAlive = 0) ExitCycle; !plc not running - wait for plc to come online
SendData;
ReceiveData;
ENDWHILE
ENDPROC
PROC ConnectToServer(string IPAddress, num PortNo)
VAR num RetryNo := 0;
SocketCreate ClientSocket; !create client socket
SocketConnect ClientSocket, IPAddress, PortNo; !connect to server with ip addr and portno supplied
TPErase;
TPWrite “Connection succesfull”;
ERROR
IF ERRNO = ERR_SOCK_TIMEOUT THEN
WaitTime 1;
RetryNo := RetryNo + 1;
IF RetryNo >= 3 THEN
ErrWrite\I, “Connection failed”, "Trying to connect to server failed. " \RL2 := "PP Goed to Main to check if PLC Alive. " \RL3 := “Retries is reset.”;
TPWrite “Socket connection failed after 3 retries”;
RetryNo := 0;
WaitTime 5;
ExitCycle;
ELSE
RETRY;
ENDIF
ELSEIF ERRNO = ERR_SOCK_CLOSED THEN
WaitTime 5;
ExitCycle;
ELSE
TPWrite "ERRNO = " \Num:=ERRNO;
Stop;
ENDIF
ENDPROC
PROC SendData()
ClearRawBytes raw_send; !clear raw data
!prep data to send
packet_send.x := packet_receive.x;
packet_send.y := packet_receive.y;
packet_send.z := packet_receive.z;
!etc etc
!pack pieces of data in the packet into raw_send
PackRawBytes packet_send.x, raw_send, (RawBytesLen(raw_send)+1)\Float4;
PackRawBytes packet_send.y, raw_send, (RawBytesLen(raw_send)+1)\Float4;
PackRawBytes packet_send.z, raw_send, (RawBytesLen(raw_send)+1)\Float4;
SocketSend ClientSocket \RawData := raw_send;
ERROR
IF ERRNO = ERR_SOCK_CLOSED THEN
TPWrite “Socket closed during sending data”;
ExitCycle;
ENDIF
ENDPROC
PROC ReceiveData()
ClearRawBytes raw_receive; !clear raw data
SocketReceive ClientSocket \RawData := raw_receive\Time := 10;
!unpack raw_receive and save individual values in packet_receive
UnpackRawBytes raw_receive, 1, packet_receive.x \Float4;
UnpackRawBytes raw_receive, 5, packet_receive.y \Float4;
UnpackRawBytes raw_receive, 9, packet_receive.z \Float4;
SocketStat := SocketGetStatus(ClientSocket); !update status of client socket
ERROR
IF ERRNO = ERR_SOCK_TIMEOUT OR ERRNO = ERR_SOCK_CLOSED THEN
WaitTime 5; !wait to make sure server is started first
TPWrite “Comms time out during receiving data”;
ExitCycle;
ENDIF
ENDPROC
ENDMODULE
//-------------------------------------------------------------
Any help would be appreciated!
