Filtering on board serial (stm32) for the monitor port

is it possible to get somehow the monitor port depend on the boards serial (stm32)? Similar to the technique used for uploading with hla_serial?
Is there generally some mechanisms that can traverse on uart device list and filter only the device with right serial number?

Or is it possible to open monitor_port with another extra_script?


Every environment can have its own monitor_port which it can also dynamically build.

Seems like you pretty much want Add support for multiple ST-Link adapters · Issue #564 · platformio/platform-ststm32 · GitHub.

Yes, it is exactly this.
But the question how to replace that monster line?
monitor_port = /dev/serial/by-id/usb-STMicroelectronics_STM32_STLink_${env:f103.custom_hla_serial}-if02
By something more simple and platform independent. (mac dont have serial folder in dev)

Does maybe extra_script also work here?
Some variable like MONITOR_PORT?

Hm yes, I see the point. Probably one could use a script to identify all serial ports with their STLink serial number and thus make it not only work on Linux. MONITOR_PORT might work, I have not tested it.

As for today there is no out of box solution for that problem. I have studied the codebase of platformio core. SCons and extra_scripts are run only in RUN and UPLOAD mode, but not in monitor. Monitor does just forwarding of monitor_port to, without invoking scons.

On the other side it is not possible to simply include scons in platformio/commands/device/ because scons is a bunch of steps that at some moment loads all the extra_scripts. This means also that if try to load this in platformio/commands/device/ it will carry a lot of steps that are not necessary for monitor, but designed for build process.

Of course it would be very nice to have a MONITOR_PORT in some extra_script that loads with pio device monitor. This way the process of finding individual serial devices would be highly flexible and straightforward. Maybe more simple solution would be to create kind of monitor_script in [env:name] that is not depent on scons but that filter on some board unique way the serial device. So it is still flexible and not hardcoded.