There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

Tell me about glue please: How do you connect a microcontroller that conditionally executes code in different languages over USB serial on a typical desktop distro?

This is something that perplexed me a few years ago with Flash Forth on a PIC18/PIC24/Arduino Uno. I was using the Python serial emulator S-Term because it is simple in the source code and worked. I really wanted a way to load more structured Words into the FF dictionary with bookmarks in a way that made sense structurally. That lead to a desire to execute code from the uC on the host system, but I never wrapped my head around how to do this in practice.

As a random simple example, let’s say I set up an interrupt based on the internal temperature sensor of the PIC18. Once triggered the uC must call a Python script on the host system and this script defines a new FF word on the uC by defining the Word in the interpreter.

How do you connect these dots to make this work at the simplest ‘hello world’ level? I tried modifying S-Term at one point, but I didn’t get anywhere useful with my efforts.

CalcProgrammer1 ,
@CalcProgrammer1@lemmy.ml avatar

I’m not sure about FF specifically, but 99% of the time you’re connecting a microcontroller to a PC you’re doing so over a serial port (UART) of some sort. It may be a physical COM port or it may be a USB to serial adapter or even a purely virtual serial port over a USB connection, but the methodology is all the same. Unless you are running a serial terminal on that port (as in, a commandline on your PC served on the given /dev/ttyX interface, not a terminal emulator letting you read/write from the port), the microcontroller can’t just run scripts on the PC. Instead, you will want to write a script/program that opens the port and waits for a command to be sent from the microcontroller, then that listener script can execute whatever functionality you require. Note that only one application can have the port active at a time, so if your listener is a separate program from your event handler, you will have to close the port on the listener before running the handler, then reopen the port on the listener once the handler is done so it can start listening for the next event. Better to just make it all one program that is always running on the PC and does both listening for events and handling them so there’s only one program that needs access to the serial port.

MarauderIIC ,

Yup, pretty much this. I have a small touchscreen Arduino device that I keep plugged into my PC. When I hit a button on the touchscreen it sends some text over serial (which is really a USB wire). I have a Python script on the PC that continuously reads from the serial port and parses the received data to figure out what to do. Specifically it tells my Discord bot to play a sound :)

skullgiver ,
@skullgiver@popplesburger.hilciferous.nl avatar

My approach would be to have the Python script read the serial console of the microcontroller in a loop and parse the text data exchanged through it. Binary can work too but then you’d need to implement the necessary logic to figure out if you’re done reading or not.

Through things like udev rules you can automatically start such a script when the device is detected (i.e. the cable is plugged in).

Something like this should work, depending on the settings of your serial connection:


<span style="color:#323232;">#!/usr/bin/env python3
</span><span style="color:#323232;">import serial
</span><span style="color:#323232;">
</span><span style="color:#323232;">ser = serial.Serial(
</span><span style="color:#323232;">    port='/dev/ttyUSB0,
</span><span style="color:#323232;">    baudrate=57600,
</span><span style="color:#323232;">    parity=serial.PARITY_ODD,
</span><span style="color:#323232;">    stopbits=serial.STOPBITS_TWO,
</span><span style="color:#323232;">    bytesize=serial.SEVENBITS
</span><span style="color:#323232;">)
</span><span style="color:#323232;">
</span><span style="color:#323232;">while(True):
</span><span style="color:#323232;">    line = ser.readline()
</span><span style="color:#323232;">    # Do something with the line of text that was just read
</span><span style="color:#323232;">    print(line)
</span><span style="color:#323232;">
</span><span style="color:#323232;">ser.close()
</span>

Coupled with a udev rule like:


<span style="color:#323232;">ACTION=="add", SUBSYSTEM=="usb", ENV{ID_VENDOR_ID}=="0123", ENV{ID_MODEL_ID}=="4567", RUN+="/path/to/your/script.py"
</span>

The udev rule depends on how you connect to the device, of course. Also make sure to mark the script as executable if you call it directly. First improvement I’d make is to figure out how to detect the device (instead of hard coding ttyUSB0) and the correct parity settings.

bloodfart ,

From the uc side, open a serial connection, send data. On the host side, prepare a place for received data. On the host side, walk through the received data until the thing you’re looking for is found, then execute some action (in your case a python script).

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • [email protected]
  • lifeLocal
  • goranko
  • All magazines