Skip to main content

Beckhoff TwinCAT PLC software allows the user to open TCP/IP sockets directly to a Galil Controller using their TwinCAT TCP/IP Server Communication Supplement.

This post provides a simple function block example that opens a connection to the Galil Controller, then sends commands and receives the responses at 1 second intervals.  This code allows for basic communication from the TwinCAT PLC to a Galil Controller.  The full project can be found here:

TwinCAT_TCP.zip

The code is shown below:

The IP address and command sent to the controller are defined in the MAIN (PRG) as i_sRemoteHost and sSendCommand respectively.

PROGRAM MAIN
VAR
lrVar1: LREAL := 123.45;
fbGalil: FB_Galil;
tUpdate: TIME := T#1s;
xEnable: BOOL := TRUE;
sSendCommand: STRING := 'MG TIME';
END_VAR

--------------------------------------------------------------

(* P_simple_TCPIP(); *)

fbGalil(
i_xEnable:= xEnable,
i_sSrvNetID:= '',
i_sRemoteHost:= '192.168.1.74',
i_nRemotePort:=23 ,
i_tReconnect:= T#5s ,
i_tUpdateTime:= tUpdate,
lrVar1:= lrVar1,
lrVar2:=0,
sSendCommand:=sSendCommand );

The communication is handled in the FB_Galil (FB) Function Block - code is shown below.

FUNCTION_BLOCK FB_Galil
VAR_INPUT
i_xEnable            :BOOL;
i_sSrvNetID         :STRING := '';
i_sRemoteHost     :STRING;
i_nRemotePort     :UDINT;
i_tReconnect       :TIME := t#45s;
i_tUpdateTime      :TIME := T#1s;

lrVar1                :LREAL;
lrVar2                :LREAL;
sSendCommand    :STRING;
END_VAR
VAR_OUTPUT
END_VAR
VAR
fbClientServerConnection: FB_ClientServerConnection;
fbSocketSend: FB_SocketSend;
sSendString: STRING;
fbSocketReceive: FB_SocketReceive;
iStep: INT;
rtEnable: R_TRIG;
tonUpdate: TON;
sReceiveString: STRING;
END_VAR

----------------------------------------------------------------

rtEnable(CLK:=i_xEnable);

IF i_xEnable = FALSE THEN
iStep := 0;
END_IF

CASE iStep OF
0: (* idle *)
fbClientServerConnection.bEnable:= FALSE;
fbSocketSend.bExecute := FALSE;
fbSocketReceive.bExecute := FALSE;
IF i_xEnable = TRUE THEN
iStep := 10;
END_IF

10: (* make the conection *)
fbClientServerConnection.bEnable:= TRUE;
IF fbClientServerConnection.eState = eSOCKET_CONNECTED THEN
iStep := 100;
ELSIF fbClientServerConnection.bError THEN
iStep :=0;
END_IF

100:
IF tonUpdate.Q THEN
fbSocketSend.bExecute := FALSE; (* reset send*)
fbSocketReceive.bExecute := FALSE; (*reset receive *)
iStep := 110;
tonUpdate(IN := FALSE);  (* reset timer *)
END_IF

110:
(* command to just set a variable - this would be commanded moves as well (ie PA, BG etc) *)
(*
sSendString := 'var1=TIME';
(* append a number to a variable decleration - ie var1=123.45 *)
(*        sSendString := 'var1=';
sSendString := CONCAT(sSendString,LREAL_TO_STRING(lrVar1)); *)
sSendString := CONCAT(sSendString,'$r');(*append carriage return and line feed*)
fbSocketSend.pSrc:= ADR(sSendString);
fbSocketSend.cbLen := LEN(sSendString);
fbSocketSend.bExecute := TRUE;
*)

(* command with a response *)
sSendString := sSendCommand;
sSendString := CONCAT(sSendString,'$r$n');(*append carriage return and line feed*)
fbSocketSend.pSrc:= ADR(sSendString);
fbSocketSend.cbLen := LEN(sSendString);
fbSocketSend.bExecute := TRUE;
iStep := 120;

120:
IF NOT fbSocketSend.bBusy THEN
IF NOT fbSocketSend.bError THEN
iStep := 125;
ELSE
(* hanldle error *)
iStep := 9000;
END_IF
END_IF

125:
(*clear memory for receive string and set receive to true *)
MEMSET(destAddr:=ADR(sReceiveString), fillByte:=0, n:=SIZEOF(sReceiveString));
fbSocketReceive.bExecute := TRUE;
iStep := 130;

130:
(* read receive data *)
fbSocketReceive.pDest := ADR(sReceiveString);
fbSocketReceive.cbLen := SIZEOF(sReceiveString) - 1;
iStep := 140;

140:
(* check to see if received data - if not, read again (130) *)
IF NOT fbSocketReceive.bBusy THEN
IF NOT fbSocketReceive.bError THEN
IF fbSocketReceive.nRecBytes > 0 THEN
iStep := 100;
ELSE
fbSocketReceive.bExecute := FALSE; (*reset receive *)
iStep := 125;
END_IF
ELSE
(* hanldle error *)
iStep := 9000;
END_IF
END_IF

9000: (* error step *)
iStep:=0;

END_CASE

fbClientServerConnection(
sSrvNetID:= i_sSrvNetID,
nMode:= CONNECT_MODE_ENABLEDBG,
sRemoteHost:= i_sRemoteHost,
nRemotePort:= i_nRemotePort,
tReconnect:= i_tReconnect    );

fbSocketSend(
sSrvNetId:='' ,
hSocket:= fbClientServerConnection.hSocket,
tTimeout:= t#2s);

fbSocketReceive(
sSrvNetId:='' ,
hSocket:= fbClientServerConnection.hSocket,
tTimeout:= t#2s);

tonUpdate(IN := TRUE, PT:= i_tUpdateTime);