Skip to content

Instantly share code, notes, and snippets.

@TG9541
Last active January 2, 2019 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TG9541/c12e81eb22288c0de4723811a9bf85fe to your computer and use it in GitHub Desktop.
Save TG9541/c12e81eb22288c0de4723811a9bf85fe to your computer and use it in GitHub Desktop.
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