首页 文章

使用Processing存储从串行连接读入的Arduino数据

提问于
浏览
0

我试图通过使用Processing的(伪)串行连接从Arduino读取100个传感器数据条目 . 我正在使用的处理草图如下:

// 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();
  }
}

不幸的是,我通常会在我的data.txt文件中存储少于100个条目,并且一些条目(大约3-5个)显示在其中的换行符 . 我究竟做错了什么? Arduino IDE的串行监视器未打开!

1 回答

  • 1

    在你的 setup() 函数中,我认为你错误地输入了换行符:

    port.bufferUntil('n');
    

    你的意思是 '\n' ?现在你're buffering until an ' n ' comes along, which doesn' t似乎很明显 . 似乎很多教程都传递了一个值为 10 的int,这是ASCII中的换行符 .

相关问题