Read Text Sent From Arduino

edited June 2015 in Arduino

Not sure where this question would fit in the forum but I'm having an issue reading strings being sent from an Arduino.

The Arduino code sends text as a string when a function is called.

Processing reads these strings and acts accordingly.

I am trying to parse the strings based on the first three letters of each string element, then perform an action based on what the rest of the element says.

Example:

LOGstim off.

Processing reads this element, sees "LOG", saves everything after "LOG" as a new string named "log".

This part works fine. What I can't figure out is how to get processing to act after it creates this new string "log". I've managed to use match() to do what I want but its very bulky and involves creating about 30 separate strings, one for each instance of match().

I don't understand why log.equals("what I need matched") does not work. Here is an example of the processing code:

import processing.serial.*;

Serial myPort;
String dataReading = "";
String[] dataOutput = {
};
String log;

void setup() {
  size(100, 100);

  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

void draw() {

}

void serialEvent(Serial myPort) {
  dataReading = myPort.readStringUntil('\n');
  println(dataReading);
  if (dataReading != null) {
    dataOutput = append(dataOutput, dataReading);
    if (dataReading.substring(0, 3).equals("LOG")) {
      log = dataReading.substring(3);
      println(log);
      if(dataReading.substring(3).equals("visual stim")) {
      //  String logVisStim = dataReading.substring(3); //here I tried to create a separate string for the substrings after LOG.
      //  println(logVisStim);//This is to test the line of code above.
        background(255);
      }else if(dataReading.substring(3).equals("vis left error")){
        background(0);
      }else{
        background(100);
      }
    }
  }
}

  void saveData() {
    println("saving to txt file...");
    saveStrings("data/data.txt", dataOutput);
  }


  void mousePressed() {
    saveData();
  }

Here is a copy of what is printed in the serial monitor.

CMDTVinitiation

LOGTVinitiation

TVinitiation

TIM279931

CMDtrialstart

LOGautostart

autostart //this is from println(log);

TIM280008

LOGstim right on

stim right on //this is from println(log);

TIM280039

LOGvisual stim

visual stim //this is from println(log);

TIM280040

TIM280140

LOGmissed your shot!

missed your shot! //this is from println(log);

TIM300008

LOGstim off

stim off //this is from println(log);

TIM300008

CMDsession stop R

CMDsession stop L

Answers

  • edited June 2015 Answer ✓

    you might just use trim() here

    see

    http://forum.processing.org/two/discussion/7293/using-arduino-buttons-to-do-something-in-processing-best-method

    also, when you defined log you can use this var from there on

            if (dataReading.substring(0, 3).equals("LOG")) {
                  log = dataReading.substring(3);
                  println(log);
                  if(log .equals("visual stim")) {
                  //  String logVisStim = log; //here I tried to create a separate string for the substrings after LOG.
                  //  println(logVisStim);//This is to test the line of code above.
                    background(255);
                  }else if(log .equals("vis left error")){
                    background(0);
                  }else{
                    background(100);
                  }
                }
    
  • Chrisir, you are a legend.

    I do get a error message saying:

    Error, disabling serialEvent() for /dev/ttyACM1
    null
    

    I'm running Processing 2.2.1 on Ubuntu and I imagine this is coming from trouble with the serial port. The error goes away once I restart the Processing sketch but it still is an annoyance I'm trying to deal with.

    I wonder if placing some more conditions for dealing with null values or establishing a handshake with the Arduino will help the situation.

    Thank you.

  • did you use

     if (myPort.available() > 0) {
    

    like in the other sketch I posted the link to

  • edited June 2015

    Ah, no. I thought:

      dataReading = trim(myPort.readStringUntil('\n'));
      //println(dataReading);
      if (dataReading != null) {
    

    would do the same thing.

  • Answer ✓

    I don't know

    ;-)

  • but in the examples they do it with if (myPort.available() > 0) { afaik

    https://www.processing.org/reference/libraries/serial/Serial_readString_.html

  • I'm using something similar to this:

     if (myPort.available() > 0) {
        if ( (val = myPort.readStringUntil(ENTER)) != null )  val = trim(val);
        else return;
        if (val != null) {
          println(val);
    

    It seems to work fine at the moment. I think the error comes when the check null statements fail. Like I said, I could get rid of the error if I restarted the sketch. This would explain the statement fail.

Sign In or Register to comment.