10 REM *** 20 REM Use F2-CP128 as a Simple Modbus RTU Master 30 REM 40 REM Use FC 2 to Read RTU Input Status and put in C0 50 REM Use FC 4 to Read (3) RTU Analog Inputs and put in V2000,2001,2002 60 REM Use FC 5 to Write status of C1 to RTU Output 65 REM Use FC 16 to Write V2010-2015 to RTU Holding Registers 70 REM 80 REM Modbus Slave Address = 1 (RA) 90 REM Baud Rate = 9600 (BAUD) 100 REM Wait for First = 100 ms (WFF) 110 REM Wait for Last = 10 (3.5 Character Times Minimum) 120 REM Modbus Master Port = 2 125 REM ND=On Delay, FD=Off Delay, For Radio Communications 130 REM 140 REM *** 150 REM Allocate String Space and Configure Port 2 160 STRING 2561,255 : DIM LN(4) 162 RA=1 : BAUD=9600 : WFF=100 : WFL=10 170 SETPORT 2,BAUD,N,8,1,N 175 SETPORT 1,9600,N,8,1,N 180 REM *** 190 REM Initialize Request Strings 195 REM Read Input Status Request String - FC2/Data Address 0/1 Bit 200 $(1)=CHR$(RA)+CHR$(2)+CHR$(0)+CHR$(0)+CHR$(0)+CHR$(1) 201 LN(1)=6 205 REM Read Analog Inputs Request String - FC4/Data Address 0/3 Words 210 $(2)=CHR$(RA)+CHR$(4)+CHR$(0)+CHR$(0)+CHR$(0)+CHR$(3) 211 LN(2)=6 215 REM Write Output Status Request String - FC5/Data Address 0 220 $(3)=CHR$(RA)+CHR$(5)+CHR$(0)+CHR$(0)+CHR$(0)+CHR$(0) 221 LN(3)=6 225 REM Write Multiple Word Request String - FC16/Data Address 0/6 Words 230 $(4)=CHR$(RA)+CHR$(16)+CHR$(0)+CHR$(0)+CHR$(0)+CHR$(6)+CHR$(12) 231 LN(4)=19 290 REM *** 291 REM Configure INPUT statement 295 IF WFL<10 THEN WFL=10 : REM WFL Must be 10 or greater 297 SETINPUT 1,1,0,255,WFF,WFL 300 REM *** 310 REM Read Input and Copy to C0 320 S=1 : GOSUB 1000 330 IF ERR<>0 THEN GOTO 300 : REM Continuous Retry 340 S205_C(0)=PICK(ASC($(0),4),B(0)) 342 REM or S205_VH(40600)=ASC($(0),4) 344 REM If you use line 342 instead of 340 C0-16 will be overwritten 400 REM *** 410 REM Read Three Analog Inputs and Copy to V2000-2003 420 S=2 : GOSUB 1000 430 IF ERR<>0 THEN GOTO 400 : REM Continuous Retry 440 SHARED(128)=ASC($(0),5) 442 SHARED(129)=ASC($(0),4) 444 SHARED(130)=ASC($(0),7) 446 SHARED(131)=ASC($(0),6) 448 SHARED(132)=ASC($(0),9) 450 SHARED(133)=ASC($(0),8) 460 BMOVE W,VH(2000),VH(2003) 500 REM *** 510 REM Read C1 and write to RTU Output 520 IF S205_C(1) THEN ASC($(3),5)=255 ELSE ASC($(3),5)=0 530 S=3 : GOSUB 1000 540 IF ERR<>0 THEN GOTO 500 : REM Continuous Retry 600 REM *** 610 REM Read V2010-V2015 then write to RTU Holding Registers 620 REM V2010-V2015 and put data into request string 622 BMOVE R,VH(2010),VH(2015) 624 FOR X=0 TO 11 STEP 2 625 ASC($(4),8+X)=SHARED(X,H) 626 ASC($(4),9+X)=SHARED(X,L) 628 NEXT X 630 S=4 : GOSUB 1000 640 IF ERR<>0 THEN GOTO 600 : REM Continuous Retry 999 GOTO 300 1000 REM *** 1010 REM Send Request and Get Response in $(0) 1020 $(0)="" : INLEN2=0 1022 PRINT1 "Txd > "; 1024 FOR C=1 TO LN(S) : PRINT1 HEX$(ASC($(S),C),1)," "; : NEXT C 1026 REM PRINT1 1030 SYSTEM(2)=NOT(0) : REM Turn ON CRC 1040 DTR2=1 : DELAY ND 1050 PRINT2 USING(\LN(S)\),$(S); 1060 DELAY FD : DTR2=0 1070 REM INPUT2 ,$(4) : Z=INPLEN : REM Input RS485 Echo REM for RS232 or 422 1071 INPUT2 ,$(0) : REM Input Response Data 1080 SYSTEM(2)=0 : REM Turn OFF CRC 1082 REM PRINT1 "Echo < "; : REM this line and next for RS232 or 422 1084 REM FOR C=1 TO Z : PRINT1 HEX$(ASC($(4),C),1)," "; : NEXT C 1085 PRINT1 : PRINT1 "Rsp < "; 1086 FOR C=1 TO INPLEN : PRINT1 HEX$(ASC($(0),C),1)," "; : NEXT C 1087 PRINT1 1089 ERR=0 1090 IF INPLEN=0 THEN PRINT1 "NO RESPONSE" : ERR=NOT(0) : RETURN 1100 IF COMERR2 THEN PRINT1 "CRC-16 ERROR" : ERR=NOT(0) : RETURN 1110 RETURN