#main
REM RIO ip address = 192.168.2.38
ip0= 192;ip1= 168;ip2= 2;ip3= 38
REM open modus connection to RIO
JS #connect
REM query inputs and set outputs on RIO
MG "DI1 on RIO = ",@IN[8001]
MG "set DO1 on RIO";SB 8001
MG "AN1 on RIO = ",@AN[8001]
MG "set AO1 on RIO to 5V";AO 8001,5
EN

REM subroutine for establishing a modbus connection 
REM to a device with IP address ip0.ip1.ip2.ip3
REM ip0-ip3 are variables that must be defined 
REM prior to call of subroutine
#connect
REM close H handle
#close
IHH= -2
WT 100
JP #close,_IHH2<>0
REM open handle to IP address set by ip0-ip3
REM modbus handle <502>2
i= 0;fail= 0;'number of tries and fail flag
REM make connection to RIO
#con
IHH= ip0,ip1,ip2,ip3<502>2
REM wait while attempting to establish TCP/IP handle
#wtih;WT 10;JP #wtih,_IHH2=-6
REM try 5 times, if not established, then indicated failure and stop
i= i+1
IF ((i=5)&(_IHH2=0))
 fail= 1
 MG "Failed Connection to ",ip0{F3.0},ip1,ip2,ip3
 TH
ENDIF
JP #con,((_IHH2=0)&(fail=0))
REM indicate if TCP connection was successful
IF (_IHH2=-2)
 MG "Connection established to ",ip0{F3.0},ip1,ip2,ip3
ENDIF
MW 1;'enable modbus wait functionality
EN

#TCPERR
MG "TCPERR, Reconnecting to RIO - error code",_TC1
JS #connect
IF fail
 MG "Connection to ",ip0{F3.0},ip1,ip2,ip3," Failed"
 MG "Stopping Program"
 HX
ENDIF
RE

#CMDERR
MG "Command Error on line",_ED
MG "Error",_TC
IF ((_TC>=120)&(_TC<=129));'communication error
 MG "Communication Error, Reconnecting to RIO"
 JS #connect
 IF fail
  MG "Connection to ",ip0{F3.0},ip1,ip2,ip3," Failed"
  MG "Stopping Program"
  HX
 ENDIF
ENDIF
RE