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.

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.

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