質問

I am trying to read decimal values from the serial input coming from a simulated Arduino program. To keep things simple, here is the basic gist:

Serial.write(56);//decimal value
Serial.println('1001.56');// float value

The Arduino code basically writes various integers, but I need it to occasionally print floats. When I read in the decimal outputs on my Android program, I get the following values:

056 - corresponds to the decimal
049 051 054 050 050 013 010 - is the output from the '1001.56'

I know what the 013 and 010 represent, but how do the first 5 decimal values correspond to 1001.56? I have been looking at the ASCII table all morning with no luck.

役に立ちましたか?

解決

Serial.println('1001.56');// float value

This is erroneous - in C/C++ ' ' denote a single character, while " " denote a string of characters

056 - corresponds to the decimal

It is a raw value - it could be the number 56, or it could be the character code for the digit '8'

049 051 054 050 050 013 010 - is the output from the '1001.56'

Probably not - it could be the string "13622\r\n" Or it could be some garbage value produced by the quoting error, followed by println()'s terminating carriage return and newline.

Fix your quote issue and you will have a better idea of where you stand.

However, you may still have a problem in that you will not necessarily receive data over the serial interface in the same grouped chunks you write it in, but rather may receive it in fairly arbitrary sized blocks which you will have to stitch back together and find delimitating "\r\n" sequences within.

他のヒント

13 and 10 decimal are the Carriage Return and Line Feed. Note you are using "Serial.println()" versus Serial.print().

Where the ln appends the string sent with 13d(0x03) and 10d(0x0A)

If you don't want them. Simply use Serial.print(). Where as you will likely later want them, as to indicate your end of line, to parse out the string of char's back into the float.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top