字符串转换为浮动发生在处理NaN的错误 [英] Convert string to float occurs NaN error in Processing
问题描述
我满足处理一个问题,当我的值(字符串)转换为浮动,第一个值是好的,但其余为所有的 NaN的
。我无法找到一个方法来解决这个问题。而我打印的字符串值测试。它是正确的,但之后,我把它转换成浮动。这将是 NaN的
。
PS:该值从串行,我连我的Arduino与Proceesing
以下是codeS的一部分
而(myport.available()0)
{
myString的= myport.readString(); //读取串口字符串
NUM =浮动(myString的); //转换成字符串浮动
打印(NUM); //打印NUM(浮动),但第一
//值好,休息都是`NaN`。
//打印(myString的); //打印字符串,所有的价值观都是好的
打印('');
如果(的myString!= NULL)
{
// NUM =浮动(myString的);
storeData(myString的);
//的println(myString的);
//打印(数据[I - 1]);
//的println('');
延迟(1000);
}
}
以下是结果
转换finshed:
不转换,只打印字符串值
以下是Arduino的code
和=开方(XG * XG + YG * YG + ZG * ZG);
总和* = 10;
总和=(地图(总和,0,1024,0,5000)/10.0);
Serial.println(总和);
延迟(100);
我发现我的问题是Arduino的code里面,所以我改变了从Arduino的发送数据的方式。我使用了的println()
来发送数据。而这点导致 NaN的
。我serached在谷歌,然后我测试了不同的方式来改变的方式发送,直到我瑶池此链接:<一个href=\"http://www.varesano.net/blog/fabio/sending-float-variables-over-serial-without-loss-$p$pcision-arduino-and-processing\" rel=\"nofollow\">http://www.varesano.net/blog/fabio/sending-float-variables-over-serial-without-loss-$p$pcision-arduino-and-processing
和感谢法比奥的博客,他的博客的introudces解决这个问题的好办法。如果你有同样的烦恼,也许你可以通过这个固定的。
I meet a problem in the Processing, and when i convert the value(string) into float, the first value is good, but the rests are all NaN
. I could not find a way to solve this. And i print the string value for test. And it is correct, but after i convert it into float. It will be NaN
.
ps: the value is from the serial, i connected my Arduino with Proceesing.
following is a part of codes
while(myport.available() > 0)
{
myString = myport.readString(); //read the string from serial
num = float(myString); // convert the string into float
print(num); // print the num(float), but the first
// value is good, rests are all `NaN` .
//print(myString); // print string, all the values are good
print(' ');
if(myString != null)
{
//num = float(myString);
storeData(myString);
//println(myString);
//print(data[i - 1]);
//println(' ');
delay(1000);
}
}
following is the result
conversion finshed:
not convert, only print string value
following is arduino code
sum = sqrt(Xg*Xg + Yg*Yg + Zg * Zg);
sum *= 10;
sum = (map(sum, 0, 1024, 0, 5000)/10.0);
Serial.println(sum);
delay(100);
I find my problem is inside the Arduino code, so i changed the way to send the data from Arduino. I used the println()
to send the data. And that's the point lead to NaN
. I serached on google, and then i tested different ways to change the way to send until i finded this link:http://www.varesano.net/blog/fabio/sending-float-variables-over-serial-without-loss-precision-arduino-and-processing
And thanks fabio's blog, his blog's introudces a good way to solve this problem. If you have the same trouble, maybe you can fixed by this.
这篇关于字符串转换为浮动发生在处理NaN的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!