Parsing data from a Serial Port.
in
Programming Questions
•
1 year ago
Hello all, I am using the following code to display incoming data from an Arduino from my Serial Port. I have tried a dozen ways to parse through this data looking for the word "Good" in order to trigger an action, however I can't figure it out.
Can someone propose a way to parse the incoming data and find "Good". Here is the format of the incoming data:
+CMT, "xxxxxxxxx"
Good
Here is the code:
- import processing.serial.*;
- Serial myPort; // the serial port you're using
- String portnum; // name of the serial port
- String outString = ""; // the string being sent out the serial port
- String inString = ""; // the string coming in from the serial port
- int receivedLines = 0; // how many lines have been received in the serial port
- int bufferedLines = 10; // number of incoming lines to keep
- void setup() {
- size(600, 800); // window size
- // create a font with the second font available to the system:
- PFont myFont = createFont(PFont.list()[2], 14);
- textFont(myFont);
- // list all the serial ports:
- println(Serial.list());
- // based on the list of serial ports printed from the
- //previous command, change the 0 to your port's number:
- portnum = Serial.list()[1];
- // initialize the serial port:
- myPort = new Serial(this, portnum, 9600);
- }
- void draw() {
- // clear the screen:
- background(0);
- // print the name of the serial port:
- text("Serial port: " + portnum, 10, 20);
- // Print out what you get:
- text("typed: " + outString, 10, 40);
- text("received:\n" + inString, 10, 80);
- }
- void serialEvent(Serial myPort) {
- // read the next byte from the serial port:
- int inByte = myPort.read();
- // add it to inString:
- inString += char(inByte);
- if (inByte == '\r') {
- // if the byte is a carriage return, print
- // a newline and carriage return:
- inString += '\n';
- // count the number of newlines:
- receivedLines++;
- // if there are more than 10 lines, delete the first one:
- if (receivedLines > bufferedLines) {
- deleteFirstLine();
- }
- }
- }
- void deleteFirstLine() {
- // find the first newline:
- int firstChar = inString.indexOf('\n');
- // delete it:
- inString= inString.substring(firstChar+1);
- }
1