Find active serial port from processing
in
Integration and Hardware
•
2 years ago
I have an arduino that prints 'T' on its serial port every 100 millisecs. I want to write a processing program that looks at all serial ports on the PC and find the one that the arduino is attached to. I'm sure someone would have done this before but couldn't find anything in my searches. The code I am trying looks like the following:
long millisStart;
int i = 0;
int len=Serial.list().length;
println(Serial.list());
int i = 0;
int len=Serial.list().length;
println(Serial.list());
println("Serial Port Count = " + len);
for ( i = 0; i < len; i++)
{
arduinoPort = new Serial(this, Serial.list()[i], 9600); // start arduino communications
millisStart = millis();
println("Testing port " + i);
while ((millis() - millisStart) < 10000) ;
arduinoPort.clear();
arduinoPort.bufferUntil('T');
if(arduinoPort.available()>0)
{
println("Char available");
int c = arduinoPort.readChar();
println("Char = " + c);
if(c == 'T')
{
arduinoPort.write('G');
break;
}
}
else
{
arduinoPort.stop();
}
}
{
arduinoPort = new Serial(this, Serial.list()[i], 9600); // start arduino communications
millisStart = millis();
println("Testing port " + i);
while ((millis() - millisStart) < 10000) ;
arduinoPort.clear();
arduinoPort.bufferUntil('T');
if(arduinoPort.available()>0)
{
println("Char available");
int c = arduinoPort.readChar();
println("Char = " + c);
if(c == 'T')
{
arduinoPort.write('G');
break;
}
}
else
{
arduinoPort.stop();
}
}
1