Skip to content

Instantly share code, notes, and snippets.

@TG9541 TG9541/MBTEST
Last active Jan 2, 2019

Embed
What would you like to do?
Minimal MODBUS test program
#require MBCRC
#require 'IDLE
VARIABLE coils
: process ( -- )
rxp @ 2- ( a1 ) DUP rxbuf ( a1 a1 a0 ) MBCRC
( a1 crc-le ) SWAP @ =
CR CR
( crc-ok ) IF
mbslv DUP ." SLAVE: " . CR txc+
mbfc DUP ." FC: " . CR DUP txc+ ( fc )
mbp1 ." R0: " . CR
mbp2 ." R#: " . CR
( fc ) DUP 1 = IF
mbp2 1- 8 / 1+ txc+
mbp1 mbp2 OVER + SWAP DO
I 0= IF coils @ ELSE 0 THEN
txc+
8 +LOOP
THEN
( fc ) DUP 2 = IF
mbp2 1- 8 / 1+ txc+
mbp1 mbp2 OVER + SWAP DO
I txc+
8 +LOOP
THEN
( fc ) DUP 5 = IF
mbp1 1- ( #b) DUP 0 8 WITHIN IF
mbp2 $FF00 =
( #b f )coils 1+
( #b f a ) ROT ( f a #b ) B!
ELSE
DROP
THEN
mbp1 tx+ mbp2 tx+
THEN
( fc ) 16 = IF
mbp1 tx+ mbp2 tx+
THEN
tbp @ txbuf ( a1 a0 ) MBCRC ( CRC-LE ) tx+
ELSE
." CRC error!" CR
THEN
;
: itask ( -- )
rxbuf rxp @ - IF
P2H
3 TIM tstamp @ - < IF
process
bufdump
start-tx rxres
THEN
P2L
THEN
;
: init ( -- )
UARTISR
0 coils !
[ ' itask ] LITERAL 'IDLE !
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.