Accessing Two USB Ports in One Program

Hello Everyone,

I’d like to perfect/enhance my ability to access data from two different USB devices. My code works fine, and I’m able to grab data from both devices, but I know it’s a novice approach.

Is there a method to incorporate a second serialEvent() and have two separate data strings?
I’m not able to get it working. Attempt shown below.

Processing 3.3
Win 7 and Win 10
Arudino MEGA 2560
GlobalSat GPS BU-353S4

Thank you for any tips!


import processing.serial.*;
Serial arduPort;
Serial gpsPort;

int lf = 10;

float voltage, lat, lon, gpsSpeed, gpsHeading, gpsAltitude;

String inString, inString2;  

void setup() {

  fullScreen(SPAN);  

  arduPort = new Serial(this, Serial.list()[0], 115200);  // COM10
  arduPort.bufferUntil(lf);

  gpsPort = new Serial(this, Serial.list()[1], 4800);  // COM11  
  gpsPort.bufferUntil('\n');      

  delay(1000); 
 
}

void draw() {

  background(0);
  
  String[] gpsData = split(inString, ',');   
  float[] arduData = float(split(inString2,',')); 


  String block = gpsData[0];
  String str1 = "$GPGGA", str2 = "$GPRMC";

  if (str1.equals(block) == true) {
    float[] gpsData2 = float(split(inString, ','));
    gpsAltitude = gpsData2[7];
  }

  else if (str2.equals(block) == true) {  
    float[] gpsData2 = float(split(inString, ','));  
    lat = gpsData2[3];  
    lon = gpsData2[5];
    gpsSpeed = gpsData2[7];  
    gpsHeading = gpsData2[8];
  }
   
  else {   

    float[] adruData = float(split(inString2, ','));  
    if(arduData[0] > 0){
      voltage = arduData[0];  
      gpsSpeed = arduData[1];  
      gpsHeading = arduData[2];
    }
  }

  delay(5);

  textSize(22);
  text("GPS Data 0 Header: " + gpsData[0], 500, 325);  //as a float (see float(split)

  text("GPS Data 2 Lat: " + lat, 500, 385);
  text("GPS Data 3 Lon: " + lon, 500, 405);

  text("GPS Data 1 Spd: " + gpsSpeed, 500, 460);  //as a float (see float(split)
  text("GPS Data 7 Alt: " + gpsAltitude, 500, 495);
  text("GPS Data 6 HDG: " + gpsHeading, 500, 530);

  text("Volts A0: " + voltage, 100, 530);
  
} //end Draw function


void serialEvent(Serial p) { 

  inString = p.readString();
  
}


void serialEvent2(Serial p2) { 

  inString2 = p2.readString();
  
}
1 Like

possibly

serialEvent2()

is wrong, but see Read multiple serial port - Processing 2.x and 3.x Forum here

2 Likes