modmast.abm (F4-CP128-1)
10 REM ***
20 REM Use F4-CP128-1 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 V1400,1401,1402
60 REM Use FC 5 to Write status of C1 to RTU Output
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(3)
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(2)=6
230 REM ***
240 REM Configure INPUT statement
245 IF WFL<10 THEN WFL=10 : REM WFL Must be 10 or greater
250 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 S405_C(0)=PICK(ASC($(0),4),B(0))
342 REM or S405_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 V1400-1402
420 S=2 : GOSUB 1000
430 IF ERR<>0 THEN GOTO 400 : REM Continuous Retry
440 DPORT(0)=ASC($(0),5)
442 DPORT(1)=ASC($(0),4)
444 DPORT(2)=ASC($(0),7)
446 DPORT(3)=ASC($(0),6)
448 DPORT(4)=ASC($(0),9)
450 DPORT(5)=ASC($(0),8)
460 BMOVE W,VH(1400),VH(1403)
500 REM ***
510 REM Read C1 and write to RTU Output
520 IF S405_C(1) THEN ASC($(3),5)=255 ELSE ASC($(3),5)=0
530 S=3 : GOSUB 1000
540 GOTO 300
1000 REM ***
1010 REM Send Request and Get Response in $(0)
1020 $(0)=”” : INLEN2=0
1022 PRINT1 “> “;
1024 FOR C=1 TO LN(S) : PRINT1 HEX$(ASC($(S),C),1),” “; : NEXT C
1026 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 Input RS485 Echo – Remove or REM next 2 lines for RS232 or 422
1071 SETINPUT 1,1,0,LN(S)+2,1,1 : INPUT2 ,$(4) : Z=INPLEN
1072 SETINPUT 1,1,0,255,WFF,WFL
1075 INPUT2 ,$(0) : REM Input Response Data
1080 SYSTEM(2)=0 : REM Turn OFF CRC
1081 REM Display RS485 Echo – Remove or REM next 3 lines for RS232 or RS422
1082 PRINT1 “Echo < “;
1084 FOR C=1 TO Z : PRINT1 HEX$(ASC($(4),C),1),” “; : NEXT C
1085 PRINT1
1086 PRINT1 “Rsp < “;
1087 FOR C=1 TO INPLEN : PRINT1 HEX$(ASC($(0),C),1),” “; : NEXT C
1088 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