-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.fs
62 lines (54 loc) · 2.02 KB
/
main.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
\ Minimal MODBUS server application with an FC6 handler
\ Hint: MBSERVER implements a full MODBUS server - see C0135/board.fs
\
\ Features:
\ * implements FC06 "Write Single Register"
\ * prints debug infos to the console
\
\ Example output for 96008N1, Node=1, FC=6, Addr=5, Data=516
\
\ Write register: 5= 516
\ MODBUS rtbuf: 8
\ 80 1 6 0 5 2 4 99 68 0 0 0 0 0 0 0 0 _______h________
\ MODBUS rtbuf: 8
\ 98 1 6 0 5 2 4 99 68 0 0 0 0 0 0 0 0 _______h________
\ check if the MODBUS protocol core is already present
\ hint: the development cycle will be much faster if you PERSIST it
#require MBPROTO
\ Resetting the FC handler table can be "helpful" for development
#require WIPE
#require MBRESET
MBRESET \ Reset the MODBUS Function Code table
#require :NVM
#require 'IDLE
NVM
\ MODBUS buffer dump for demo (not needed in an application)
#require MBDUMP
\ --- FC06 handler "Write Single Register"
\ mbp1 (MODBUS parameter 1) provides the register address
\ mbp2 (MODBUS parameter 2) provides the new register value
:NVM ( -- )
\ write register address and value to the console
\ (an application likely wouldn't print anything)
." Write register:" mbp1 . ." =" mbp2 . CR
MBWR \ protocol: acknowledge FC06
;NVM ( xt ) 6 FC>XT ! \ register the FC handler
\ --- demo: show MODBUS function code
: showfc ( -- )
rtbuf 1+ C@ ." FC:" . CR
1 MBEC \ set error code
;
\ --- set everything up
: init ( -- )
0 UARTISR \ init UART handler w/ default baud rate
1 mbnode ! \ set node address
[ ' showfc ] LITERAL mbdef ! \ FC default action (optional feature)
[ ' MBDUMP ] LITERAL mbpre ! \ demo: dump RXTX buffer after receive
[ ' TXDUMP ] LITERAL mbact ! \ dump RXTX buffer before transmit
[ ' MBPROTO ] LITERAL 'IDLE ! \ register MBPROTO as the idle task
CR ." Demo STM8EF-MODBUS server FC6 'Write Single Register'"
hi
;
\ --- set init address to boot vector - run after reset
' init 'BOOT !
WIPE RAM