MODBUS Things with STM8 eForth

Projects (and mishaps) for talking MODBUS to STM8 eForth

Similar projects worth following
Doing things with MODBUS should be easy but using ultra-cheap Chinese MODBUS boards is anyting but. It's as much "hacking" as "reverse hacking" (engineering is to big a word) to figure out where those addressing a hobby and informal engineering market made a joke of specifications and protocols, even the most simple ones. In this project I'm having fun with these things by using them with STM8 eForth. Why not innovate a bit while we're at it?

MODBUS, especially the binary serial RTU protocol through RS485 interface is still popular - sufficiently popular to create a market for ultra-cheap (hobby-grade) components, e.g. "STM8S103 Relay Control Board" with barely usable MODBUS firmware

or the infamous Chinese "USB RS485 Adapter Black+Green" for less than $1 (including shipping).

For some time now I had fun with implementing MODBUS, testing it against frameworks and getting things running (or not) using Chinese gadgets. It turned out that finding the culprit sometimes takes time.

Lately, I played with KiCad, more for occupational stress relief than for creating something useful. But what the heck, why should this small STM8S001J3 MODBUS node disappear in the stream of the @oshpark  shared projects list, eceiving no love?

STM8S001J3_RS485_back.pngThis project shall make some attempts more visible and it I hope that it will lead to a usable platform soonish.

  • An STM8S001J3M3 "deal": 10 for $2.55, free shipping

    Thomas08/22/2020 at 11:02 3 comments

    STM8S001J3M3 chips are easy to solder and they're powerful enough for small MODBUS nodes, and for interactive STM8 eForth nodes, too.

    Since free shipping from China has almost tried up, these chips have become more expensive. A good price with free shipping is an opportunity for stocking up worth sharing.

    This chip can be used with the very small MODBUS board available from @oshpark, three of which cost just $1.65 (but at that price please order some more OSH boards or else the PCBs will feel really lonely in the large bag).

  • C0135 Relay Board: a beta release

    Thomas05/01/2020 at 08:55 0 comments

    For the C0135 relay board there is now a beta release that implements the following FCs:

    FC01     Read Coils
    FC02     Read Discrete Inputs
    FC03     Read Holding Registers
    FC04     Read Input Registers
    FC5     Write Single Coil
    FC6     Write Single (Holding) Register
    FC15     Write Multiple Coils
    FC16     Write Multiple Registers

    I ran tests with QModMaster and also made sure that the installation is feasible for people who don't care much about the implementation :-)

    The entry point for "general audience" is the link to MODBUS RTU releases on the C0135 Relay Control Board page in the STM8 eForth Wiki.

  • C0135 Relay Board: Inputs working

    Thomas04/24/2020 at 09:20 0 comments

    I just added the STM8 eForth MODBUS pre-release 0.16 that not only supports FC-2 "Read Discrete Inputs" but also actually reads C0135 "4-Relay Board" inputs (finally).

    Unlike writing board outputs with OUT! words for reading inputs have never been part of the STM8 eForth core. It's been a while since output words coded in assembler were moved into Since the new modular build supports binary GitHub releases with Forth code there is no reason anymore for coding board support words in assembler.

    I just added IN@ in Forth for supporting low-active inputs (NPN in PLC lingo):

    \ opcode: rotate C left through TOS lsb
    : RLC(1,X) $6901 ,  ; IMMEDIATE
    \ read C0135 inputs - connect INx to GND to get a "1"
    : IN@ ( -- n )
      -1  \ inputs are "NPN" (in PLC speak), start with all bits 1
      [ ( IN4 ) PC_IDR 6 ]BC RLC(1,X)  \ some simple STM8 assembly
      [ ( IN3 ) PC_IDR 7 ]BC RLC(1,X)  \ to load bits to C
      [ ( IN2 ) PD_IDR 2 ]BC RLC(1,X)  \ and rotate them into the
      [ ( IN1 ) PD_IDR 3 ]BC RLC(1,X)  \ Top Of Stack low-byte

    Ok, I have to admit that this is almost assembler but IN@ is more readable than an implementation in C. I like C but low level routines, and the code they produce, are a frequent source of concerns, compiler bug reports, #pragmas, syntax extendsions and idioms without adding the least bit to portability (even if your intent is to exchange compilers, not hardware).

    Using this is as simple as always: IN@ will return a bitmap containing the inputs.

    The following "program" will activate a relay if the corresponding input is pulled low:

    \ copy inputs to outputs
    : doit ( -- )
       IN@ ( n ) OUT! 
    ' doit BG !  \ run in background

    The background task makes this task surprisingly easy. Note: everything ( in brackets ) and after "\"  are comments.

    Adding a simple state machine already allows doing some interesting things.

  • A Binary Release for the C0135 "STM8S103 4-Relay Board"

    Thomas04/14/2020 at 06:27 0 comments

    I got a reminder from Down-Under not to leave things half-finished: the C0135 board, using the STM8 eForth SWIMCOM binary would communicate but it wouldn't switch any relay.

    I must admit that for finishing that job the learning curve for anyone "not I" is a bit too steep. Also I left the new STM8 eForth Modular Build virtually unused for a long time. Sorry to say that but for some people it takes a global pandemic to get some quality time for their hobbies.

    Long story short, the STM8 eForth MODBUS repository is in better shape than before, and due to the new automated binary release process there is, well, a binary release for the C0135.

    Some more features are planed, e.g. an old-style unified "holding register" address space for "coils", inputs, process values and EEPROM. Currently there are no inputs for the C0135 but that's just about 20 lines of code away (lazy me).

    Reminding me helps.

  • STM8S001J3M3 - RS485 V1.1 assembled and tested

    Thomas12/27/2019 at 22:08 0 comments

    I just tested the newly assembled "Mini RS485 Node" based on the smallish STM8S001J3M3 µC. The board has just 1/3 of a square-inch (and a good deal of that are connectors).

    The board has components on both side, and this time I also populated the DS1621S thermometer chip.

    Here is the "board" in profile next to my new CH340E RS485 dongle:

    The dongle provides 5V power from USB - since the MODBUS "board" has a 3.3V regulator bus powered systems measurement systems are possible.

    That's certainly not cost efficient for any series production item but I wanted to know what's the smallest stuff I can handle. To be honest, the MSOP10 chip was a bit of a challenge, more so than soldering a STM8S207RBT6C in a LQFP-64 with 0.5mm pitch (ST makes the gaps wider than the pins - 0.22mm to 0.28mm - the CH340E pins appear to be a bit wider).

  • Hand-soldering MSOP10 is more difficult than I expected

    Thomas12/26/2019 at 21:18 0 comments

    I populated and tested the first USB-RS232 board - it works! To be honest, I still can't believe that I hand-soldered the CH340E in MSOP10 without completely messing it up!

    The switch on the backside is for activating a "local echo". I'd like to have that feature for operation with e4thcom (just as any other terminal program it expects an echo!). Standard RS485 behaviour doesn't read the bus while sending. I guess that this is for compatibility of RS482 with RS422. It would have been better to read the bus while sending since this would enable collision or error detection.

  • New PCBs arrived

    Thomas11/22/2019 at 07:38 0 comments

    The PCB arrived from @oshpark , including STM8S001J3RS485 V1.1

    Timeline so fo: ordered 03/Nov, shipped 11/Nov, arrived 21/Nov (including customs).

    I'm still waiting for most of the components for the USB-dongles, ordered from Aliexpress around the same time or just before.

    The USB-dongle layout is really crammed, and there isn't much space left for aditonal components. The USB-A connector (standard item, $0.68 for 10pcs) fits the KiCad footprint perfectly!

  • Making a USB-RS485 dongle that cuts the right edges

    Thomas11/05/2019 at 20:39 0 comments

    As pointed out before, cheap Chinese "Green-Black USB-RS485 dongles" are cheats: what's the point in using an RS485 driver when driving just "ones" and replacing "zeros" with a bias circuit?

    I decided to make another small board and order from @oshpark's excellent prototyping service.

    This RS485 adapter even has an "echo" switch, which makes using RS485 for the STM8 eForth two-wire interface possible.

    Using it as a simple RS232-TTL adapter for 3-wire or 2-wire configuration is also a configuration option. More details are in theRS485-CH340E repo on GitHub.

  • Building STM8 eForth for MODBUS boards

    Thomas10/26/2019 at 06:51 0 comments

    STM8 eForth supports a range of boards. and provides a method for building new ones. The problem is that the new board needs to be added to the STM8 eForth repository or a fork of it.

    This has changed with the pre-release STM8 eForth 2.2.24.pre1: the source code is part of the release archive, and maintaining a board configuration in a "downstream" project is possible:

    The STM8 eForth MODBUS repository now has a first "board folder", and building a Forth binary for the STM8S001J3RS485 board is as simple as cloning the repository and running the following:

    make -f BOARD=STM8S001J3RS485 flash

    Of course, you also need to install STM8FLASH and the SDCC tool chain (or use a docker-sdcc).

    Work on variant build in downstream projects, and auto-build of STM8 eForth isn't complete (providing binaries with MODBUS code needs some chainges), but the main building blocks are in place.

  • STM8S001J3RS485 MODBUS Sensor Board V1.1

    Thomas10/13/2019 at 19:51 0 comments

    When doing hand soldering I learned "first hand" that the orientation of 0603 components matters, and consequently I worked a bit on the component placement of the tiny STM8S001J3RS485 PCB. Using 8 mil signal track width, 6 mil clearance and 10 mil via drills made it a lot easier compared to sticking with the KiCad defaults. The power supply now forms a rail from left to right, switching the side of the PCB about in he middle.

    The text on the silcscreen is friendlier, too (even if they make good use of the 400DPI silcscreen that @oshpark provides (i.e. the print is tiny):

    The 0603 components on the back side now are also easier to populate.

    The new board is available on the @oshpark shared project pages, and the GitHub repo has been updated.

View all 14 project logs

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates