我想做最简单的事情,用Processing软件从Arduino的串行端口绘制图形。
我按照教程说的做了插件。我还从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 the initial 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.
我知道有一个bug。如果你试图将一个8位整型设置为一个32位整型,它就会出问题。
处理IDE工作得很好。Eclipse根本不会触发。有解决办法吗?
注意bufferUntil('n')接受一个整数值。你把它弄焦了。至少尝试一下bufferUntil(10),看看是否有一些奇怪的事情发生,但是简单地打印myPort上的值,看看当您发送换行符时发生了什么可能是值得的。