Serial port void serialEvent Arduino-处理以避免延迟

Serial port void serialEvent Arduino-处理以避免延迟,serial-port,arduino,processing,Serial Port,Arduino,Processing,我在将数据从Arduino发送到processing时遇到问题,我可以在draw循环中接收数据,但速度非常慢,因此我包含了一个serialEvent()函数来尝试消除一些滞后现象,但现在它似乎没有读取serialEvent函数中的数据,我终生都不明白为什么 我经常错过愚蠢的错误,如果是这样的话,我很抱歉 谢谢 import themidibus.*; import processing.serial.*; int end = 10; String serial; Serial port;

我在将数据从Arduino发送到processing时遇到问题,我可以在draw循环中接收数据,但速度非常慢,因此我包含了一个serialEvent()函数来尝试消除一些滞后现象,但现在它似乎没有读取serialEvent函数中的数据,我终生都不明白为什么

我经常错过愚蠢的错误,如果是这样的话,我很抱歉

谢谢

import themidibus.*;
import processing.serial.*;   
int end = 10;
String serial;
Serial port;
MidiBus myBus;
//incoming data
int c;
int d;
int e;
int f;
int g;
int a;
int b;
int C;
int pitchPot;
int tonalityPot;
int noteVol;
int pan;
int reverb;
int dlay;
int distort;
int octave;
//ellipse parameters
int noteOn = 0;
int col1 = 0;
int col2 = 0;
int col3 = 0;

int passTime;
int saveTime;
int expand = 0;
String[] A = new String[18];
String[] B = new String[18];

boolean cPress = false;
boolean dPress = false;
boolean ePress = false;
boolean fPress = false;
boolean gPress = false;
boolean aPress = false;
boolean bPress = false;
boolean CPress = false;

void setup() {
  size(600,600, P3D);
  frameRate(10);
  smooth();
  port = new Serial(this, Serial.list()[6], 115200);
  port.clear();
  serial = port.readStringUntil(end);
  serial = null; // initially, the string will be null (empty)
  myBus = new MidiBus(this, -1, "Java Sound Synthesizer");
}

void draw() {

  if ((c == 1) && (cPress == false)){
    col1 = 255;
    col2 = 0;
    col3 = 0;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (60 + pitchPot + octave), noteVol);
    cPress = true;
  }
  if ((c == 0) && (cPress == true)){
    myBus.sendNoteOff(1, (60 + pitchPot + octave), noteVol);
    cPress = false;
  }
 //D

  if ((d == 1) && (dPress == false)){
    col1 = 0;
    col2 = 0;
    col3 = 255;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (62 + pitchPot + octave), noteVol);
    dPress = true;
  }
  if ((d == 0) && (dPress == true)){
    myBus.sendNoteOff(1, (62 + pitchPot + octave), noteVol);
    dPress = false;
  }

  if ((e == 1) && (ePress == false)){
    col1 = 0;
    col2 = 255;
    col3 = 0;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (64 + pitchPot + tonalityPot  + octave), noteVol);
    ePress = true;
  }
  if ((e == 0) && (ePress == true)){
    myBus.sendNoteOff(1, (64 + pitchPot  + tonalityPot + octave), noteVol);
    ePress = false;
  }

  if ((f == 1) && (fPress == false)){
    col1 = 255;
    col2 = 0;
    col3 = 255;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (65 + pitchPot + octave), noteVol);
    fPress = true;
  }
  if ((f == 0) && (fPress == true)){
    myBus.sendNoteOff(1, (65 + pitchPot + octave), noteVol);
    fPress = false;
  }

  if ((g == 1) && (gPress == false)){
    col1 = 255;
    col2 = 255;
    col3 = 0;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (67 + pitchPot + octave), noteVol);
    gPress = true;
  }
  if ((g == 0) && (gPress == true)){
    myBus.sendNoteOff(1, (67 + pitchPot + octave), noteVol);
    gPress = false;
  }

  if ((a == 1) && (aPress == false)){
    col1 = 0;
    col2 = 255;
    col3 = 255;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (69 + pitchPot + tonalityPot  + octave), noteVol);
    aPress = true;
  }
  if ((a == 0) && (aPress == true)){
    myBus.sendNoteOff(1, (69 + pitchPot + tonalityPot  + octave), noteVol);
    aPress = false;
  }

  if ((b == 1) && (bPress == false)){
    col1 = 50;
    col2 = 250;
    col3 = 130;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (71 + pitchPot + octave), noteVol);
    bPress = true;
  }
  if ((b == 0) && (bPress == true)){
    myBus.sendNoteOff(1, (71 + pitchPot + octave), noteVol);
    bPress = false;
  }

  if ((C == 1) && (CPress == false)){
    col1 = 200;
    col2 = 90;
    col3 = 75;
    myBus.sendControllerChange(1, 10, pan);
    myBus.sendNoteOn(1, (72 + pitchPot + octave), noteVol);
    CPress = true;
  }

  if ((C == 0) && (CPress == true)){
    myBus.sendNoteOff(1, (72 + pitchPot + octave), noteVol);
    CPress = false;
  }



if(c == 1 || d == 1 || e == 1 || f == 1 || g == 1 || a == 1 || b == 1 || C == 1){
  noteOn = 1;
}
else {
  noteOn = 0;
}



if (dlay == 1){
 if (noteOn == 1){
  passTime=millis() - saveTime;
  if (passTime > 1){
    expand+= 15;
     saveTime = millis();
  }
    strokeWeight(10);
    stroke(col1,col2,col3);
    noFill();
    ellipse((300+pan), 300 , expand, expand);

 }
    else {
      expand = noteVol*2;
      noteOn = 0;
    }


    fill(0, 0, 0, 100);
    rect(0,0, 600,600);
    fill(col1,col2, col3); 
    lights();
    pushMatrix();
    translate(300+pan, 300);
    noStroke();
    sphere(noteOn*noteVol);
    popMatrix();
 }

 else if (reverb == 1){
   fill(0, 0, 0, 100);
    rect(0,0, 600,600);
    fill(col1,col2, col3); 
    lights();
   pushMatrix();
   translate(300, 300);
    rotate(frameCount / 50.0);
    star(0, 0, (noteOn*noteVol* 20), ((noteOn*noteVol)/20), 40); 
    popMatrix();
 }

 else {
 fill(0, 0, 0, 100);
    rect(0,0, 600,600);
    fill(col1,col2, col3); 
    lights();
    pushMatrix();
    translate(300+pan, 300);
    noStroke();
    sphere(noteOn*noteVol);
    popMatrix();

 }
 }




void serialEvent(Serial port){
    while (port.available() > 0) {
    serial = port.readStringUntil(end);

}
if (serial != null) {

  A = split(serial, ',');
  B = trim(A);
  c = Integer.parseInt(B[1]);
  d = Integer.parseInt(B[1]);
  e = Integer.parseInt(B[2]);
  f = Integer.parseInt(B[3]);
  g = Integer.parseInt(B[4]);
  a = Integer.parseInt(B[5]);
  b = Integer.parseInt(B[6]);
  C = Integer.parseInt(B[7]);

  pitchPot = Integer.parseInt(B[9]);
  tonalityPot = Integer.parseInt(B[10]);
  noteVol = Integer.parseInt(B[11]);
  pan = Integer.parseInt(B[12]);
  dlay = Integer.parseInt(B[13]);
  reverb = Integer.parseInt(B[14]);
  distort = Integer.parseInt(B[15]);
  octave = Integer.parseInt(B[16]);

}
}

这是一个老问题,但它首先出现在谷歌搜索中,所以这是我的理解

serialEvent()似乎不像文档中所说的那样被串行事件调用,至少在我使用的Arduino平台上是这样。它可能在某些方面起作用。如果不起作用,请尝试在主循环中调用serialEvent。我认为这是等效的,因为串行数据实际上没有中断。您只需阅读它,它的想法是serialEvent函数将在每个循环中调用,但它似乎不是这样工作的

SerialEvent Arduino文档说明:
“注意:目前,serialEvent()与Esplora、Leonardo或Micro不兼容”,但我怀疑列表应该更长。

这是很多代码。如果你被卡住了,还没有得到答案,试着去掉与问题无关的部分——你能通过串口读取任何东西吗?不要猜测整个程序中的问题在哪里,试着去掉部分直到你有更好的想法。我同意kevinsa5。取出除SerialEvent函数之外的所有内容,然后开始重新添加内容。您还可以尝试在设置中使用“port.bufferUntil(end);”,然后在SerialEvent函数中使用“readString()”。