简体   繁体   中英

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. 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. What am I doing wrong? The serial monitor of the Arduino IDE is NOT open!

In your setup() function, I believe you mistyped the newline character:

port.bufferUntil('n');

Did you mean '\\n' ? Right now you're buffering until an 'n' comes along, which doesn't seem obvious. It seems that many of the tutorials pass it an int with the value of 10 , which is the newline character in ASCII.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM