bufferuntil('\ n)不会使用eclipse触发serialevent处理

时间:2013-04-25 17:56:00

标签: eclipse arduino processing processing-ide

我想做最简单的事情来绘制Arduino串口的图形和处理软件。我用eclipse。

我按照教程说的插件做了。我也复制了arduino网站的代码:

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph

void setup () {

  // set the window size:
  size(400, 300);        

  // List all the available serial ports
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);

  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');

  // set inital background:
   background(0);

}

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

void serialEvent (Serial myPort) {

  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {

    // trim off any whitespace:
    inString = trim(inString);

    // convert to an int and map to the screen height:
    float inByte = float(inString);
    inByte = map(inByte, 0, 1023, 0, height);

    // draw the line:
    stroke(127,34,255);
    line(xPos, height, xPos, height - inByte);

    // at the edge of the screen, go back to the beginning:
    if (xPos >= width) {

      xPos = 0;
      background(0);

    }
    else {

       // increment the horizontal position:
       xPos++;

    }

  }

}

有一个问题是bufferUntil('\ n')不会触发serialevent。

我知道有一个错误案例,你试图将一个8位的int设置为一个32位int,它会变成地狱。

处理ide工作得很好tho.Eclipse根本没有触发。有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

请注意,bufferUntil('\ n')采用整数值。你给它一个字符。至少尝试一下bufferUntil(10)只是为了看看是否有一些奇怪的事情发生在那里,但是可能值得简单地打印看到myPort上的值,看看当你发送换行符时会发生什么。