# Accessing Two USB Ports in One Program

**URL:** <https://discourse.processing.org/t/accessing-two-usb-ports-in-one-program/12790>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [July 18, 2019, 1:11pm UTC](https://discourse.processing.org/t/accessing-two-usb-ports-in-one-program/12790 "2019-07-18T13:11:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![YellowGTM](https://avatars.discourse-cdn.com/v4/letter/y/a87d85/32.png) [@YellowGTM](https://discourse.processing.org/u/YellowGTM)\
**Post date:** [July 18, 2019, 1:11pm UTC](https://discourse.processing.org/t/accessing-two-usb-ports-in-one-program/12790/1 "2019-07-18T13:11:30Z")

</div>

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!

```auto

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();
  
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [July 18, 2019, 2:41pm UTC](https://discourse.processing.org/t/accessing-two-usb-ports-in-one-program/12790/2 "2019-07-18T14:41:17Z")

</div>

possibly

> serialEvent2()

is wrong, but see [Read multiple serial port - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/98070/#Comment_98070) here
