Arduino and Processing Code Error “disabling_seria

2019-08-18 17:57发布

问题:

I am facing the following error in Processing application off and on, sometimes it works perfectly.

Error, disabling serialEvent() for COM2

null

Here are the codes:

Arduino's:

    void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  Serial.print(sensorValue1);
  Serial.write("-");
  Serial.println(sensorValue2);
  delay(1);
}

Processing's:

 import processing.serial.*;

 Serial myPort;        // The serial port
 void setup () {
 size(1043, 102);
 background(255);
 myPort = new Serial(this, Serial.list()[1], 9600);
 println(Serial.list());
 myPort.bufferUntil('\n');
 }

 void draw () {
 // everything happens in the serialEvent()
 }

 void serialEvent (Serial myPort) {
 background(255);
 String inString = myPort.readStringUntil('\n');
 if (inString != null) {
 inString = trim(inString);
// String inByte = inString;
 int[] inStr = int(split(inString,'-'));
 println(inStr);

fill(0);
rect(10,2,inStr[0],46);

rect(10,52,inStr[1],46);

fill(255);
rect(400,14,245,21); 

fill(0);
textAlign(CENTER);
textSize(14);
text("1st value: "+inStr[0]+" 2nd value: "+inStr[1],width/2,30);
 }
 }

When I remove following part of code, the application works fine.

fill(0);
rect(10,2,inStr[0],46);

rect(10,52,inStr[1],46);

fill(255);
rect(400,14,245,21); 

fill(0);
textAlign(CENTER);
textSize(14);
text("1st value: "+inStr[0]+" 2nd value: "+inStr[1],width/2,30);

I am using Win7, Processing ver2.2.1 & Arduino ver1.0.5-r2.

I'm new to all serial communication stuff..

回答1:

You have to surround your serialEvent method body in try-catch:

void serialEvent (Serial myPort) {

  try {
    ...
    your code
    ...
  }
  catch(RuntimeException e) {
    e.printStackTrace();
  }

}

There is probably an exception thrown from your serialEvent implementation.



回答2:

Put your code inside the draw() function, just use serialEvent() to change the value of variables (remember to initialize them).

Make inStr a global variable, you can define its length and initialize it maybe with 0.

Should be something like:

import processing.serial.*;

Serial myPort;        // The serial port
int[] inStr = {0,0};

void setup () {
  size(1043, 102);
  background(255);
  myPort = new Serial(this, Serial.list()[1], 9600);
  println(Serial.list());
  myPort.bufferUntil('\n');
}

void draw () {
  fill(0);
  rect(10,2,inStr[0],46);

  rect(10,52,inStr[1],46);

  fill(255);
  rect(400,14,245,21); 

  fill(0);
  textAlign(CENTER);
  textSize(14);
  text("1st value: "+inStr[0]+" 2nd value: "+inStr[1],width/2,30);  
}

void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    inStr = int(split(inString,'-'));
    println(inStr);
  }
}

In the future try not to use serialEvent to do the work of draw()



回答3:

// at the top of the program:
float xPos = 400; // horizontal position of the graph
float yPos = 300; // vertical position of the graph
float zPos = 300;
float regiypos = yPos;
float regizpos = zPos;
int par = 5;

import processing.serial.*;
Serial myPort;        // The serial port

int lf = 10;    // Linefeed in ASCII
String myString = null;
float num;  
void setup () {

  size(800, 600);        // (Width, Height) window size

  myPort = new Serial(this, "COM4", 115200);

  background(#081640);
  noLoop();                                                //-- Ne fussál folyamatosan

}

void draw () {
   // draw the line in a pretty color:
  stroke(#A8D9A7);
  line(xPos-par,regiypos, xPos, yPos);                        // (x1,y1,x2,y2) Origó a bal felső sarokban  
  regiypos = yPos;                                        // ami most új volt azt megőrzi, hogy legyen mivel összekötni

  stroke(#ff00A7);
  line(xPos-par,regizpos, xPos, zPos);         
  regizpos = zPos;

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    // clear the screen by resetting the background:
    background(#EDA430);
    background(#081640);
  }
  else {
    // increment the horizontal position for the next reading:
    xPos+=par;
  }
}

void serialEvent (Serial myPort) {                      //-- Nem fut folyamatosan, hanem csak akkor ha adat érkezik, mert akkor meghívodik ez a fv.
  // get the byte:
//  float inByte = myPort.read();
  // print it:
//  println(inByte);
  myString = myPort.readStringUntil(lf);
  if (myString != null) {
      print(myString);  // Prints String
      String[] q = splitTokens(myString,":");
      if (q.length==2){
          print(q[0]);  // Acceltoroll print
          print(q[1]);  // Gyrotoroll print

          num=float(q[0]);  // Converts and prints float
          yPos = height/2 - num;
          num=float(q[1]);  // Converts and prints float
          zPos = height/2 - num;
          redraw();             //-- Melynek a végén van a redraw() parancs, mely egyszer lefutattaja a draw()-ban lévő részt.
      }
  }  
}

And the highlighted part is this:

if (q.length==2) {

          print(q[0]);  // Acceltoroll print
          print(q[1]);  // Gyrotoroll print

          num=float(q[0]);  // Converts and prints float
          yPos = height/2 - num;
          num=float(q[1]);  // Converts and prints float
          zPos = height/2 - num;
          redraw();             //-- Melynek a végén van a redraw() parancs, mely egyszer lefutattaja a draw()-ban lévő részt.
}

This will solve your problem. The problem is that q[1] may not exist if the buffer is full and begins to overwrite the data...

Just paste the code to notepad++ to get a better view.