Read multiple serial port

Sorry kfrajer for my post, publication is correct now

Hi,

i've this code for to read in processing two arduino's separately.

First arduino is a multiples sensors, second arduino have an gps.

My serial data have this format:

For arduino sensors:

    85,89,120

For arduino gps have this format (gps string)

$GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13,309.62,120598,,*10

Here my code but don't run properly, because i've just Serial portOne run impossible to run two ports...

    import processing.serial.*;

    Serial portOne;    // the first serial port
    Serial portTwo;    // the second serial port


    void setup() {
      size(800, 480);
      // list the serial ports
      println(Serial.list());
      // open the serial ports:
      portOne = new Serial(this, Serial.list()[0], 115200);
      portTwo = new Serial(this, Serial.list()[1], 115200);

      portOne.bufferUntil('\n');
      portTwo.bufferUntil('\n');
    }

    void draw() {

    }

    void serialEvent(Serial thisPort) {
      // read the incoming serial data:
      String input1 = thisPort.readStringUntil('\n');

      if (input1 != null) {
      input1 = trim(input1);
      String[] values1 = split(input1, ",");

        // if the string came from serial port one:
        if (thisPort == portOne) {
          if(values1.length == 3){
      int a1 = int(values1[0]); 
      int a2 = int(values1[1]); 
      int a3 = int(values1[2]);

      print(a1); 
      print(","); 
      print(a2); 
      print(","); 
      println(a3); 

       } 
        }

        // read the incoming serial data:
      String input2 = thisPort.readStringUntil('\n');

       if (input2 != null) {
      input2 = trim(input2);
      String[] values2 = split(input2, ",");

        // if the string came from serial port two:
        if (thisPort == portTwo) {
          if(values2.length == 3){
      int b1 = int(values2[0]); 
      int b2 = int(values2[1]); 
      int b3 = int(values2[2]);

      print(b1); 
      print(","); 
      print(b2); 
      print(","); 
      println(b3); 

       } 
        }

       }
      }
    }

If someone has an idea please help me.

Regards, and sorry for my english....

Alfred

Answers

  • Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

  • edited May 2017

    Try the concept below. When you do println(portOne.list); in setup(), do you get both ports?

    Also if you try running each port by itself, does it run without any problems?

    Kf

     void serialEvent(Serial thisPort) {
    
      if(thisPort==portOne){
          ...GOOD NEWS: data from serial 1
      }
    
      if(thisPort==portTwo){
          ...GOOD NEWS: GPS data just arrived!
      }
    
    }
    
  • KF, When I put println (Serial.list ()); In the setup, I see COM4 COM11, but likewise there is only one port (the first) that works ...println (portOne.list); error

  • edited May 2017 Answer ✓
    I found the solution, for what it concerns here is the code:
    I have done tests and finally it works
    
    import processing.serial.*;
    
    Serial PortPFD;    
    Serial PortGPS;    
    
    PFont myFont;     
    
    String StringPFD = "";
    String StringGPS = "";
    
    int a1;
    int a2;
    int a3;
    int b1;
    int b2;
    int b3;
    
    void setup() {
      size(300,300); 
      background(0);
    
      println(Serial.list());
      PortPFD = new Serial(this, Serial.list()[0], 115200);
      PortGPS = new Serial(this, Serial.list()[1], 115200);
      delay(100);
    
      PortPFD.bufferUntil('\n');
      PortGPS.bufferUntil('\n');
      delay(100);
    }
    
    void draw() {
      background(0); 
    
    textSize(22);
    text(StringPFD, 5,30); 
    text(StringGPS, 5,60); 
    
    text(a1, 5,120); 
    text(a2, 5,140);
    text(a3, 5,160); 
    
    text(b1, 5,200);
    text(b2, 5,220); 
    text(b3, 5,240);
    
    delay(50);    
    }
    
    void serialEvent(Serial thisPort) {
    
    // Port 1 (COM 4) PFD
       if (thisPort == PortPFD) {
    // StringPFD = "PFD: " + thisPort.readString();
       StringPFD = thisPort.readString();
       if(StringPFD != null){
       StringPFD = trim(StringPFD);
       String[] ValuesPFD = split(StringPFD, ",");
       if(ValuesPFD.length == 3){
       a1 = int(ValuesPFD[0]); 
       a2 = int(ValuesPFD[1]); 
       a3 = int(ValuesPFD[2]); 
    // println(StringPFD);
       }
         }      
          }    
    
    // Port 2 (COM 11) GPS
       if (thisPort == PortGPS) {
    // StringGPS = "GPS: " + thisPort.readString();
       StringGPS = thisPort.readString();
       if(StringGPS != null){
       StringGPS = trim(StringGPS);
       String[] ValuesGPS = split(StringGPS, ",");
       if(ValuesGPS.length == 3){
       b1 = int(ValuesGPS[0]); 
       b2 = int(ValuesGPS[1]); 
       b3 = int(ValuesGPS[2]); 
    // println(StringGPS);      
       }
        }
         } 
    }
    
    
    Thank you kfrajer.
    
  • Answer ✓

    @bbjodel Just to clarify on your solution, you got it to work by checking which port to process the data from? Good to her is working. Thanks for sharing your solution.

    Kf

Sign In or Register to comment.