Define monitor port in sketch?

Is there any way of defining the monitor port in the main.cpp part of the sketch
(the same way as :
) ??

I have several devices connected to COM-ports & I would like to define both upload port & monitor port in my main.cpp sketch…
(without having to open and changing this in platformio.ini every time I upload a sketch to a different COM-port).

I’ve searched the documentation but i’ve only found the upload_port environmental definition…


You can make different environments for the different boards and declare their upload_port and monitor_port inside each environment. Per docs, also wildcard are accepted.

PlatformIO doesn’t read out its configuration parameters from the source code, that’s not possible.

Hello Maximilian.
Thank you, that is a very good idea, but if all boards are the same model, how can i specify this in main.cpp and in platformio.ini ??

If the COM port assignment is fixed and not swapped around during development, wouldn’t it be a good idea to simply duplicate the environments and specify the upload_port for the current development cycle? Then use the environment-specific tasks in VSCode to upload.

Hello again.
I dont understand this to 100%, (i’m not that advanced in platformio yet)…

Say I have three Wemos D1 mini connected to COM3, COM4 & COM5 and want to upload code to one of them by specifying the upload_port in main.cpp, (by commenting out the upload_port entry in platformio.ini & adding
#define PLATFORMIO_UPLOAD_PORT COM4 in main.cpp).

But when I run the program it Autodetects a COM port instead of selecting the defined COM-port in main.cpp, why ???

And then, how do I specify the monitor port to be the same as the upload_port ??
The monitor_speed does not have to change (it can be set to 115200 for all boards…)

Plese provide a simple example of how this should be done…

Ok, after some testing I figured out how to solve my problem (thanks to Maximilian).
I created separate unique labelnames for each of my Wemos D1 mini board in platformio.ini, with settings for comport, monitor port etc. for each board, and then I used the Project Tasks menu to select which board I wanted to upload my software to.
That works very well, thanks for the advise !!