Arduino + Proccesing code error “disabling_serialevent()”

本小妞迷上赌 提交于 2020-01-15 08:20:24

问题


I have some problems in my code, i can run it because i always get this error message

Error, disabling serialEvent() for COM3 null

import processing.serial.*;
Serial port;
String c = " ";
String d = " ";
String data = " ";
PFont font;
int index = 0;

void setup() {
    size(2024, 1024);
    port = new Serial(this, "COM3", 9600);

    port.bufferUntil('.');
    font = loadFont("run.vlw");
    textFont(font, 60);
}

void draw() {
    background(150, 50, 200);
    fill(46, 20, 2);
    text(c, 70, 175);
    fill(46, 20, 2);
    text(d, 70, 215);
}

void serialEvent(Serial port) {
    data = port.readStringUntil('.');
    data = data.substring(0, data.length() - 1);
    index = data.indexOf(",");
    c = data.substring(0, index);
    d = data.substring(index + 1, data.length());
}

I am new in this thing, sorry if i made a big mistake.


回答1:


Your serialEvent() code doesn't handle exceptions. It is likely that your handling of the serial data contains an error which subsequently causes an unhandled StringIndexOutOfBoundsException, so the program crashes. To be sure, we would need knowledge of the actual data that gets transmitted via COM3. So here are two suggestions:

  1. Put the whole code inside of serialEvent() in a try/catch block and print out the exception's stack trace. This should give you more hints about what causes the crash.
  2. Edit your question and add a snippet of your Arduino Serial Monitor's output.

Can't comment yet, so I have to post this as an answer.



来源:https://stackoverflow.com/questions/42618052/arduino-proccesing-code-error-disabling-serialevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!