As may be other people interested in this TCP/IP communication between LV and RobotStudio, i’m putting here some code I used to create in Rapid the sockets needed to handle this communication.
The first one belongs to the initializing funcion and is just related to the calling of the open connnection routines. The other ones are for effectively open, send and receive data. I tried to put all in english, but as I’m brazilian, maybe there are some text I did not realize that were left in poprtuguese. Sory in advance!
! Open TCP/IP connections
OpenTCPServerConnection;
OpenDebugServerConnection;
IF SocketGetStatus(Client_Socket) <> SOCKET_CONNECTED THEN
OpenTCPClientConnection;
ENDIF
IF SocketGetStatus(ClientDebug_Socket) <> SOCKET_CONNECTED THEN
OpenDebugClientConnection;
ENDIF
WriteDebugString “Debug connection activated.AD”;
!=========================================================================
PROC OpenTCPServerConnection()
! DescriA?A?o: Creates a server socket to the main connection
! Entradas: None
! SaA-das: None
! AlteraA?A?es: Flex screen
!*************************************************************************
! Close eventualy oppened connections
SocketClose Server_Socket;
SocketClose Client_Socket;
! Creates o socket server
SocketCreate Server_Socket;
SocketBind Server_Socket, server_ip, TCP_Port;
! Listen to and waits for the client connection (LV)
TPErase;
TPWrite “Waiting for conncetion at address: " + server_ip +”, porta: " Num := TCP_Port;
SocketListen Server_Socket;
SocketAccept Server_Socket, Client_Socket ClientAddress := client_ip;
TPWrite "Connection succeeded at address " + client_ip;
ENDPROC
!=========================================================================
!=========================================================================
PROC OpenDebugServerConnection()
! DescriA?A?o: Creates a server socket to a debug channel
! Entradas: Nenhuma
! SaA-das: Nenhuma
! AlteraA?A?es: Flex screen
!*************************************************************************
! Close eventually oppened connections
SocketClose ServerDebug_Socket;
SocketClose ClientDebug_Socket;
! Creates socket seriver
SocketCreate ServerDebug_Socket;
SocketBind ServerDebug_Socket, server_ip, Debug_Port;
! Listen and wait for the client connection
TPWrite “Waiting for conncetion at address: " + server_ip +”, porta: " Num := Debug_Port;
SocketListen ServerDebug_Socket;
SocketAccept ServerDebug_Socket, ClientDebug_Socket ClientAddress := client_ip;
TPWrite "Connection succeeded at address " + client_ip;
ENDPROC
!=========================================================================
!=========================================================================
PROC ReadTCPData()
! DescriA?A?o: Reads incoming data from the TCP connection.
! Entradas: None
! SaA-das: None
! AlteraA?A?es: None
!*************************************************************************
! Clears input buffer.
ClearRawBytes InputBuffer;
! Reads data from the oppened connection.
SocketReceive Client_SocketRawData:= InputBufferTime:=TPC_Timeout;
! Got an error?
ERROR
IF ERRNO = 0 THEN
int_data1 := 0;
ELSEIF ERRNO=ERR_SOCK_TIMEOUT THEN
SkipWarn;
ClearRawBytes InputBuffer;
!TPWrite “Receive Timeout”;
RETURN;
ELSEIF ERRNO = ERR_SOCK_CLOSED THEN
SkipWarn;
ClearRawBytes InputBuffer;
SocketClose Client_Socket;
SocketClose Server_Socket;
TPWrite “Socket closed error. Program terminated.”;
EXIT;
ENDIF
ENDPROC
!=========================================================================
!=========================================================================
PROC SendFloat4(num DT, num Data)
! DescriA?A?o: Sends a 4 bytes float value trhough the TCP connection.
! Entradas: DT: 8 bits code that identifies DataType of the value. Ex: X coordinate, Y coordinate
Quaternion value, etc.
Data: 4 bytes float value to be sent.
! SaA-das: None
! AlteraA?A?es: None
!*************************************************************************
! Preparing data pack:
! Clear output buffer
ClearRawBytes OutputBuffer;
! Add the StatOfPacket marker (#)
PackRawBytes PackStart, OutputBuffer, PackStartOffsIntX:= USINT;
! Add DataType field
PackRawBytes DT, OutputBuffer, DataTypeOffsIntX:= USINT;
! Add Data field
PackRawBytes Data, OutputBuffer, DataOffs Float4;
! PackRawBytes Data, OutputBuffer, (RawBytesLen(OutputBuffer)+1) Float4;
! Send data pack
SocketSend Client_Socket RawData := OutputBuffer;
ENDPROC
!=========================================================================
The TCP/IP connection goups together data received in a certain period in a big bulky data frame.
That’s why I used a start of packet marker (#) preceeding the data type identifier and the sent value.
The sent packet gets like this: |#|DataType|Value|. The receiver buffer, after two sending opperations,
seems like this: #a$fA?A?#A35n’A?
Another thing that is important to be said is that IRC5 input buffer seems to suport large data.
On the other hand, you can only read 1024 bytes at once, because that’s the RawBytes size limitation.
So, you will need to handle your communicatoin cycle with special care, because if you receive more than 1024 bytes, you will have a reading index error.
I think this is good satarting point. If anything else is needed, feel free to ask me.
Best regards
Leo