serial-core-mcu.py

Arduino Portenta X8 · File : serial-core-mcu.py · Language : Python · Use case : Dialoguer entre le Linux et le cœur Arduino via série interne

The X8 brain duo talks over an internal serial port: this script sends the Linux clock to the Arduino core and decodes its JSON messages line by line. Text protocol is the common denominator of the two worlds.

Hardware

An Arduino Portenta X8 (ST STM32MP1 module: quad Cortex-A7 Linux + dual Cortex-M4 real-time core, 8 GB eMMC, 2 GB LPDDR4, on-board Wi-Fi/BT, optional LTE). None: the link is internal to the module.

Wiring

Nothing to wire. Identify the right /dev/tty* device of the MCU link (dmesg after boot).

How it works

pyserial opens the internal port at 115200; the script sends a timestamped JSON per second and readline() reads the core replies, decoded when valid.

Running it

Linux side: SSH access (routed USB-C or network) on the Portenta Linux image, bash/Python scripts. Real-time side: Arduino IDE with the « Arduino Mbed OS Portenta X8 Boards » core, application M7 core, flashing via mboot from Linux.

Variants

Build a command protocol (light LED, read MCU-side sensor), or relay to MQTT on the Linux side.

serial-core-mcu.py

#!/usr/bin/env python3
# Pont Linux <-> core Arduino : trames texte sur le lien serie interne.
# sudo apt install python3-serial
# Lister les ports : ls /dev/tty* puis dmesg pour trouver le lien MCU.
import serial
import time
import json

PORT = "/dev/ttyMCU"      # nom du port interne : voir dmesg/ls /dev

lien = serial.Serial(PORT, 115200, timeout=1)
print("Lien serie vers le core Arduino ouvert", PORT)

try:
    while True:
        # 1) Envoyer l'heure Linux au core.
        trame = json.dumps({"t": int(time.time())}) + "\n"
        lien.write(trame.encode())

        # 2) Lire les messages du core (JSON par ligne).
        ligne = lien.readline().decode(errors="ignore").strip()
        if ligne:
            try:
                print("core dit :", json.loads(ligne))
            except json.JSONDecodeError:
                print("core (brut) :", ligne)
        time.sleep(1)
except KeyboardInterrupt:
    lien.close()

# Cote Arduino (X8-M7) : lire/ecouter le lien serie interne
# expose par le core (documente dans la doc Portenta X8).

← Back to the board