Still have NaN result
in
Programming Questions
•
1 month ago
Hi, i have a stream of data receiving from serial port (Arduino), and want to convert this stream of data to flaot numbers, but i faced NaN problem and tried to solve it in several ways but no update, i hope you can help me to know where's my misstake and how to solve it. below is my code and then the result.
Thanks alot
- import processing.serial.*;
- import java.text.*;
- import java.util.*;
- PrintWriter output;
- DateFormat fnameFormat= new SimpleDateFormat("yyyyMMdd");
- String fileName;
- Serial myPort;
- short portIndex = 0;
- float floatvalue;
- String stringvalue;
- void setup()
- {
- size(200, 200);
- // Open whatever serial port is connected to Arduino.
- String portName = Serial.list()[portIndex];
- println(Serial.list());
- println(" Connecting to -> " + Serial.list()[portIndex]);
- println(portName);
- myPort = new Serial(this, portName, 9600);
- Date now = new Date();
- fileName = fnameFormat.format(now);
- println(fileName);
- output = createWriter(fileName+".txt"); // save the file in the sketch folder
- }
- void draw()
- {
- while(myPort.available()>0)
- {
- stringvalue=myPort.readString();
- floatvalue=float(stringvalue);
- if(floatvalue!=Float.NaN)
- {
- output.println(floatvalue);
- }
- }
- }
- void keyPressed() {
- output.flush();
- output.close();
- exit();
- }
and the result
1.0
2.0
3.0
NaN
4.0
NaN
5.0
NaN
6.0
NaN
7.0
8.0
9.0
10.0
NaN
1.0
1.0
1.0
2.0
13.0
14.0
NaN
15.0
NaN
16.0
NaN
17.0
NaN
1.0
8.0
1