diff --git a/MBSERVER b/MBSERVER index 5eece14..5a3f271 100644 --- a/MBSERVER +++ b/MBSERVER @@ -104,10 +104,10 @@ NVM \ compile to Flash memory from here on \ --- FC05 handler "Write Single Coil" :NVM ( -- ) - mbp1 1- ( #b ) DUP 0 [ COILCELLS 16 * ] LITERAL WITHIN IF + mbp1 ( #b ) DUP 0 [ COILCELLS 16 * ] LITERAL WITHIN IF mbp2 $FF00 = - ( #b f ) coils B>L - ( #b f a ) ROT ( f a #b ) B! + ( #b f ) coils + ( #b f a ) ROT ( f a #b ) BF! MBWR \ MODBUS write response ELSE DROP 2 MBEC diff --git a/README.md b/README.md index 0296f7e..9b72b1c 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Please refer to the [Installation Instructions](https://github.com/TG9541/stm8ef ## Console -While MODBUS communication uses the STM8S UART, the Forth console communicates through a half-duplex simulated RS232 interface through the `PD1/SWIM` GPIO pin (and a diode). This is made possible by the SWIMCOM STM8 eForth "stock binary" which the makefile pulls from the STM8 eForth Releases. Other CLI communication options, e.g. using simulated full-duplex RxD-TxD lines, require building a custom STM8 eForth binary. It's also possible to use an STM8S High Density device with two UARTs like the STM8S207RBT6. +The STM8S UART is used by [UARTISR](https://github.com/TG9541/stm8ef-modbus/blob/master/UARTISR) for MODBUS RTU communication. The Forth console communicates through a half-duplex simulated RS232 two-wire interface on the `PD1/SWIM` GPIO pin. For adding a standard USB-TTL converter only a diode is needed. Other CLI communication options are easy to implment, e.g. using simulated full-duplex RxD-TxD lines (e.g. using PA1 and PA2 after removing the C0135 8MHz crystal). It's also possible to use an STM8S High Density device with two UARTs, e.g. the STM8S207RBT6. Please refer to the [STM8 eForth Wiki](https://github.com/TG9541/stm8ef/wiki/STM8S-Value-Line-Gadgets#other-target-boards) to learn more about half-duplex CLI communication options and preferred terminal programs. diff --git a/test/FC05 b/test/FC05 new file mode 100644 index 0000000..d07ca1e --- /dev/null +++ b/test/FC05 @@ -0,0 +1,59 @@ +\ Minimal MODBUS server with FC05 "Write Single Coil" handler +\ Features: +\ - prints debug infos to the console +\ - interactive tests from the console with, e.g. "coils 10 DUMP" + +\ check if the MODBUS protocol core is already present +\ hint: the development cycle will be 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 ALIAS +#require :NVM +#require 'IDLE +#require .OK + +NVM + +#require MBDUMP +#require BF! + + 4 CONSTANT COILCELLS + VARIABLE coils COILCELLS 1- 2* ALLOT + + \ --- FC05 handler "Write Single Coil" + :NVM ( -- ) + \ write register address and value to the console + ." Write register: A:" mbp1 . ." F:" mbp2 . CR + + mbp1 ( #b ) DUP 0 [ COILCELLS 16 * ] LITERAL WITHIN IF + mbp2 $FF00 = + ( #b f ) coils + ( #b f a ) ROT ( f a #b ) BF! + MBWR \ MODBUS write response + ELSE + DROP 2 MBEC + THEN + ;NVM ( xth ) 5 FC>XT ! + + \ custom default action handler + : showfc ( -- ) + rxbuf C@ ." FC:" . CR + 1 MBEC \ set error code + ; + + : 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 mbact ! \ show buffers (debug demo) + [ ' MBPROTO ] LITERAL 'IDLE ! \ run MB protocol handler as idle task + .OK + ; + + ' init 'BOOT ! +WIPE RAM