error with serial

edited February 2017 in Arduino

Hi, I've successfully done this long time ago but for some reason now it's not working. I'm just trying to read data from arduino via serial. I'm sending a string with 3 values (range 0-360), but I'm getting "Error, disabling serialEvent() for COM3 null" in Processing and can't figure out the issue.

Here's my code

import processing.serial.*; 

Serial myPort;

void setup() {
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

} 

void draw() { 

}

void serialEvent(Serial myPort){
  String val = myPort.readStringUntil('\n');
  val = trim(val);

  int gyro[] = int(split(val, ','));

  print("X: " + gyro[0]);
  print(", Y: " + gyro[1]);
  println(", Z: " + gyro[2]);
}

It's pretty much the same code I've used before but no idea what's wrong..help?

here's my arduino printing code

  //the vars are integers range 0 -360
  Serial.print(GyX);
  Serial.print(',');
  Serial.print(GyY);
  Serial.print(',');
  Serial.print(GyZ);
  Serial.print('\n');

thanks!

Tagged:

Answers

  • thanks..it works..but

    • I still don't understand exactly what's wrong in my previous code..?

    • it seems I'm not able to access the element of the array separately?

    if I do

    print(gyro[1]);

    it will say that the index is out of bound..but gyro is an array and the split function is supposed to split the incoming string and store each value in a different cell of the array right?

    import processing.serial.*; 
    
    Serial myPort;
    int[] gyro;
    
    void setup() {
      String portName = Serial.list()[0];
      new Serial(this, portName, 9600).bufferUntil(ENTER);
    
    } 
    
    void draw() { 
      println(gyro[1]);
    }
    
    void serialEvent(final Serial s) {
      gyro = int(splitTokens(s.readString()));
    }
    
  • Check that var is no null before splitting it. For debugging add after line 16 in your first post:

    println("Received: \""+val+"\" after [msec]:"+millis());
    

    Kf

  • edited February 2017 Answer ✓

    println(gyro[1]);

    That statement fails b/c gyro[] array is still null. @-)
    It is only initialized for the 1st time when serialEvent() is called back by the Serial's Thread. =;
    In order to fix that premature access, just initialize gyro[] array w/ some arbitrary length: *-:)

    import processing.serial.Serial; 
    
    static final int VALUES = 3, PORT_IDX = 0;
    int[] gyro = new int[VALUES];
    
    void setup() {
      noLoop();
      final String portName = Serial.list()[PORT_IDX];
      new Serial(this, portName, 9600).bufferUntil(ENTER);
    } 
    
    void draw() { 
      print(gyro[1], "- ");
    }
    
    void serialEvent(final Serial s) {
      gyro = int(splitTokens(s.readString()));
      redraw = true;
    }
    
  • right :D..now working..just out of curiosity would you be able to tell why my initial code wasn't working? thanks!!

    void serialEvent(Serial myPort){
      String val = myPort.readStringUntil('\n');
      val = trim(val);
    
      int gyro[] = int(split(val, ','));
    
      print("X: " + gyro[0]);
      print(", Y: " + gyro[1]);
      println(", Z: " + gyro[2]);
    }
    
  • edited February 2017 Answer ✓

    @ your statement String val = myPort.readStringUntil('\n');, val can be null if '\n' isn't found! :-S

    That's why we should always use bufferUntil():
    https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html

    Doing so, serialEvent() is only called back when the specified character arrives: <:-P
    https://Processing.org/reference/libraries/serial/serialEvent_.html

  • ok..makes sense..bust how's that possible? I was sending it every time

    Serial.print(GyX);
    Serial.print(',');
    Serial.print(GyY);
    Serial.print(',');
    Serial.print(GyZ);
    Serial.print('\n');
    
  • By default, serialEvent() is triggered by every single byte arrived! :-&
    Again, that's why we need to customize it w/ bufferUntil() or buffer(). :-B

  • @plux

    There is no need to initialize the container size. Consider the following example. I commented out the init of the array and I handle two very important situations. This is very simple error handling and one ways to do this.

    Kf

    @GoToLoop

    static final int VALUES = 3, PORT_IDX = 0;
    int[] gyro;// = new int[VALUES];
    
    void setup() {
      noLoop();
    } 
    
    void draw() { 
      showString(" -45 -90 -300");  //Prints -90
      showString(" -45 a -300");    //prints 0
      showString(null);                   //Skipped
      showString(" -45");               //Skipped
    }
    
    boolean showString(String s) {
    
      if (s==null) return false;  //LINE TO PROCESS null entries... first step
    
      gyro = int(splitTokens(s)); 
    
      if (gyro.length<2) return false;  //LINE TO ENSURE requested entry exists
    
      println("Returning value@1 => " + gyro[1] +" out of "+gyro.length);
    
      return true;
    }
    
  • Why would n1 exchange a single statement like print(gyro[1], "- "); for some extra big function like showString()? :-\"

    While a single new int[VALUES]; initialization would completely avoid such addendum? :(|)

Sign In or Register to comment.