Any method available for defining serial port for uploading in Arduino sketch?

In Arduino, usually we select the serial port and type of board for uploading the program by choosing the right one from tools -> port -> ttyACM*, like that.
May I know is there any way I can define the serial port for uploading in Arduino sketch itself? I mean, a pre-processor directive to define the type of board and the serial port it connected to. Pardon me if my question is too naive.

The short answer is NO.

I use the Geany editor for all my program code.

I have written a simple Python program to compile and upload Arduino code using the command-line IDE. It can be used without Geany.

At the top of my Arduino programs I have some comments like this

// python-build-start
// action, upload
// board, arduino:avr:mega:cpu=atmega2560
// port, /dev/ttyACM0
// ide, 1.8.6
// python-build-end

which the Python code uses for setting the parameters for its call to the IDE


A lot of people have asked about this…
AFAIK, there isn’t a way within the IDE to ‘record’ the environment - which seems odd.

A simple ‘specially formatted‘ #pragma header line is all it could take if the IDE devs were inclined.

A lot of people have asked about this...
AFAIK, there isn’t a way within the IDE to ‘record’ the environment - which seems odd.

A simple ‘specially formatted‘ #pragma header line is all it could take if the IDE devs were inclined.

I'm curious how that would work. I develop some sketch on my computer, and upload it to my Arduino, using COM4. Then, I save the sketch, with the (not yet supported) #pragma statement saying to use COM4.

I put the sketch on a thumb drive, and use another computer to compile/link/upload, after making some changes. On that computer, my Arduino is connected to COM6.

Which port is the uploader supposed to use?

Of course, you’ve just answered your own question.
If you’re working in a different hardware/OS environment, of course it’s highly unlikely the same port configuration would work.

Where it could work - is - for example...
I currently have four separate (radio linked) controllers on my desk.
One MEGA, and four UNOs, all on separate USB-serial ports.
Three separate INO files.

So i could preconfigure two sketches with #pragma statements for ‘board’ and ‘port’ - that find their own targets, but the ‘shared’ sketch would always need to be assigned to one of the two common targets which use the same code.

(The differences at runtime on those are pulled from EEPROM, so the code picks up its operating personality from the hardware - not the basic code.)

Now to make it more interesting, we could have the #pragmas in a separate #included header file (different on each desktop), then the build environment would pick,up the target devices from those persistent ‘local’ pragma headers..

Most of the time I do my programming on the same (linux) laptop and the first USB serial port always opens as ttyACM0. And when I change to a different laptop with much the same linux installation the same will be true.

If there is something else already using ttyACM0 it is very simple to change the reference at the top of the Arduino program.

I find the system of including the serial port in the program especially useful when I am developing code for two different Arduinos as there is no confusion about which code gets uploaded to which Arduino.


The Arduino Web Editor does associate a specific board and port with each sketch.

The Arduino IDE does not do this, though it has been requested many times. I think the Arduino developers are receptive to someone submitting a pull request to do this in a reasonable manner, but Arduino doen't consider it a high priority to pay someone to do it.