在下面的代码中,我试图将String值转换为float,但是我得到了以下错误。" class String没有命名为toFloat" .Error出现在以下行" yaw = s.substring(5,s.indexOf(",",i))。toFloat();"。
#include <SoftwareSerial.h>
SoftwareSerial softSerial(2, 3);
String buff;
void setup() {
Serial.begin(115200);
softSerial.begin(19200);
}
void loop() {
if(softSerial.available()){
buff = "";
while(softSerial.available()){
char in = (char)softSerial.read();
buff += in;
}
Serial.println(buff);
parseData(buff);
}
if(Serial.available()){
while(Serial.available()){
softSerial.write(Serial.read());
}
}
}
void parseData(String s){
union byte2float{
byte bval[4];
float fval;
}
b2f;
float yaw;
float pitch;
float roll;
int i=0;
String temp;
//Check if ASCII or Binary (usually, binary output doesn't start with #)
if(s.substring(0,1)=="#"){
if(s.substring(1,4)=="YPR"){
Serial.println("Output is Text");
Serial.println(s.substring(5,s.indexOf(",",i)));
yaw = s.substring(5,s.indexOf(",",i)).toFloat();
i = 1 + s.indexOf(",",i) ;
Serial.println(s.substring(i,s.indexOf(",",i)));
pitch = s.substring(i,s.indexOf(",",i)).toFloat();
i = 1 + s.indexOf(",",i);
Serial.println(s.substring(i,s.indexOf(",",i)));
roll = s.substring(i,s.indexOf(",",i)).toFloat();
}
}
else{
// 1234 5678 9101112
Serial.println("Output is Binary: ");
byte b[13];
s.getBytes(b,13);
Serial.println("[#]\t[A]\t[B]\t[Ah]\t[Bh]");
for(int x=0;x<12;x++){
Serial.print("[");
Serial.print(x,DEC);
Serial.print("]\t");
Serial.print(s[x]);
Serial.print("\t");
Serial.print((char)b[x]);
Serial.print("\t");
Serial.print((byte)s[x],HEX);
Serial.print("\t");
Serial.println((byte)b[x],HEX);
}
b2f.bval[0] = b[0];
b2f.bval[1] = b[1];
b2f.bval[2] = b[2];
b2f.bval[3] = b[3];
yaw = b2f.fval;
b2f.bval[0] = b[4];
b2f.bval[1] = b[5];
b2f.bval[2] = b[6];
b2f.bval[3] = b[7];
pitch = b2f.fval;
b2f.bval[0] = b[8];
b2f.bval[1] = b[9];
b2f.bval[2] = b[10];
b2f.bval[3] = b[11];
roll = b2f.fval;
}
Serial.print("Yaw: ");
Serial.println(yaw);
Serial.print("Pitch: ");
Serial.println(pitch);
Serial.print("Roll: ");
Serial.println(roll);
}
答案 0 :(得分:0)
使用例如
float(s.substring(5,s.indexOf(",",i)))
而不是
s.substring(5,s.indexOf(",",i)).toFloat()
错误消息显示,您使用的是字符串的toFloat()
方法,该方法不存在。而是将字符串传递给全局函数float(string)
。