[英]Storing Arduino data read in from serial connection using Processing
I am trying to read in 100 sensor data entries from the Arduino via the (pseudo) serial connection using Processing. 我正在尝试使用Processing通过(伪)串行连接从Arduino读取100个传感器数据条目。 The Processing sketch I am using is as follows: 我使用的处理草图如下:
// import the serial library for Processing
import processing.serial.*;
// define a new port object
Serial port;
PrintWriter outputFile;
int i = 0;
// setup a port
void setup()
{
outputFile = createWriter("data.txt");
port = new Serial(this, "COM3", 9600);
port.bufferUntil('n');
}
void draw()
{
delay(100); //must be the same delay as used in the arduino sketch
// create a string to store the read-in values
String serialBuffer = port.readString();
if(serialBuffer != null)
{
i++;
outputFile.println(serialBuffer);
outputFile.flush();
}
if(i==99)
{
outputFile.close();
exit();
}
}
Unfortunately, I get less than 100 entries stored in my data.txt file usually, and some of the entries (about 3-5) are showing linebreaks within them. 不幸的是,通常我的data.txt文件中存储的条目少于100个,并且其中一些条目(大约3-5个)显示了换行符。 What am I doing wrong? 我究竟做错了什么? The serial monitor of the Arduino IDE is NOT open! Arduino IDE的串行监视器未打开!
In your setup()
function, I believe you mistyped the newline character: 在您的setup()
函数中,我相信您输错了换行符:
port.bufferUntil('n');
Did you mean '\\n'
? 你是说'\\n'
吗? Right now you're buffering until an 'n' comes along, which doesn't seem obvious. 现在,您正在缓冲直到出现'n',这似乎并不明显。 It seems that many of the tutorials pass it an int with the value of 10
, which is the newline character in ASCII. 似乎许多教程都将其传递给int,其值为10
,这是ASCII中的换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.