I have an Arduino and I wish to automatically adjust the COM serial port number. Let’s say I have a variable names arduinoSerialPortNumber and I have the following line:
String portName = Serial.list()[arduinoSerialPortNumber ];
What can I do inbefore to fill that variable with the right port? I have seen arduino codes to do that, but never both at once that can be universalised. I would need both the code for C (Arduino) and Java (Processing).
Hi @Ardubora, That’s an interesting idea and I’ve come close to going this way myself. Ask yourself how your Processing sketch is supposed to recognise the Arduino that it wants? An easy way would be to make the Ard program print its name when it starts. Then Processing has to try all the ports in the list and when it gets the correct message that’s the right port. There’s an Ard macro to help:
Serial.begin(9600); Serial.println(__FILE__);
I’d like to know why you ask this question, and what type of Arduino(s) you have? What I find (on Windows) is that the genuine Arduinos with an FTDI chip and driver keep the same port number whatever USB socket they are connected to. If you have multiple PCs you can change the port numbers so it’s the same number on all PCs. The cheap (ebay) items use a CH340 + driver, and the port number changes with which USB socket it’s connected to. Just put it in the same one every time?
I don’t want to put it in the same oen every time. The plan is to give the Arduino to customers who don’t know anything about serial ports. We will use an Arduino UNO and an arduino NANO.
I ask myself how your idea would look like as a code. How can I make the Processing code “catch” the output of the arduino? How can I make the arduino make the serial output? I am not the best… I apologise.
import processing.serial.*;
/////////////////////////////////////////
Serial myPort;
/////////////////////////////////////////
int BaudRate=9600;
////////////////////////////////////////
void setup(){
myPort=new Serial(this,Serial.list()[0],BaudRate);
myPort.clear();
}
This [0] always works
Yeah I read it My code is automatic every time the first port xxd
The prog below tries to connect to each port, for 2 seconds prints everything that arrives. Then it relists the ports and repeats. You can see in the output it finds an Arduino at COM25. A nice optimisation would be to get hold of the port information as listed in device manager. e.g. from mine “BT Port (COM6)” and “USB Serial Port (COM25)”. (I don’t know how.) Then you could skip trying some ports. Another trick: after finding the right port, store the name for next time and start with that one.
If you are going to scan all the ports on lots of computers you have to ask what else could go wrong? A port might be connected - I’ve encountered that one, it’s alright. What if another item is connected and gets reset by your program connecting? Unlikely but possible.
import processing.serial.*;
final boolean F = false;
final boolean T = true;
Serial myPort;
String sPorts[];
String sAPort;
int iPortIndex;
String sReceived;
boolean bConnected;
int iNofPorts;
boolean bDoPrint;
void connectPort()
{
if (bConnected)
{
myPort.stop();
bConnected = F;
}
iPortIndex++;
if (iPortIndex < 0 || iPortIndex > (sPorts.length - 1))
{
sPorts = Serial.list();
iPortIndex = 0;
}
sAPort = sPorts[iPortIndex];
sReceived = "";
try
{
myPort = new Serial(this, sAPort, 9600);
bConnected = T;
}
catch (Exception except)
{
// comes here if port in use.
}
bDoPrint = T;
}
void getSerial()
{
char ch;
while (myPort.available() > 0)
{
ch = (char) myPort.read();
sReceived += ch;
bDoPrint = T;
}
}
void setup()
{
frameRate(30);
iPortIndex = -2;
bConnected = F;
bDoPrint = T;
}
void draw()
{
if ((frameCount -1) % 60 == 0)
{
connectPort();
}
getSerial();
if (bDoPrint)
{
println(String.format("%4d %2d %s %s", frameCount, iPortIndex, sAPort, sReceived));
bDoPrint = F;
}
}
Output
1 0 COM1
61 1 COM6
121 2 COM7
181 3 COM10
241 4 COM11
301 5 COM12
361 6 COM13
421 7 COM14
481 8 COM20
541 9 COM21
601 10 COM22
661 11 COM25
715 11 COM25 BT_Master_Scanner__
716 11 COM25 BT_Master_Scanner__
Sketch: C:\RDL\Arduino\BT_MAS
717 11 COM25 BT_Master_Scanner__
Sketch: C:\RDL\Arduino\BT_MAS~1\BT_Master_Scanner.ino
Load
718 11 COM25 BT_Master_Scanner__
Sketch: C:\RDL\Arduino\BT_MAS~1\BT_Master_Scanner.ino
Loaded: Jul 2 2020 21:57:45
721 12 COM42
781 13 COM43
841 14 COM44
901 15 COM45
961 16 COM46
1 Like