S SimonElck Member Licensed User Feb 8, 2016 #1 I probably get this all wrong, but I send byte arrays from an Arduino and I end up receiving Character strings in B4J (???!) The source is based upon the Chat example Code: Sub AStream_NewData (Buffer() As Byte) For Each n As Byte In Buffer txtLog.Text=txtLog.Text & Asc(n) & " " Next txtLog.Text=txtLog.Text &CRLF txtLog.SetSelection(txtLog.Text.Length, txtLog.Text.Length) End Sub The relevant Arduino sketch part is: Code: byte buf1[6]={1,2,3,13,10}; Serial.write( buf1,5); I expect the result to be: 1 2 3 13 10 But I get: 49 50 51 49 49 In the arduino serial monitor everything seems like I expected. Does Astream do any translations, and how do I prevent that, or is there something else going on?
I probably get this all wrong, but I send byte arrays from an Arduino and I end up receiving Character strings in B4J (???!) The source is based upon the Chat example Code: Sub AStream_NewData (Buffer() As Byte) For Each n As Byte In Buffer txtLog.Text=txtLog.Text & Asc(n) & " " Next txtLog.Text=txtLog.Text &CRLF txtLog.SetSelection(txtLog.Text.Length, txtLog.Text.Length) End Sub The relevant Arduino sketch part is: Code: byte buf1[6]={1,2,3,13,10}; Serial.write( buf1,5); I expect the result to be: 1 2 3 13 10 But I get: 49 50 51 49 49 In the arduino serial monitor everything seems like I expected. Does Astream do any translations, and how do I prevent that, or is there something else going on?
Erel B4X founder Staff member Licensed User Longtime User Feb 8, 2016 #2 Your code takes the raw bytes, converts them to characters and then converts them to the ascii code (actually Unicode code points) of those characters. Why??? The best way to show bytes is with ByteConverter.HexFromBytes (ByteConverter is a B4A / B4J library). If you just want to show the numbers then remove the Asc keyword. Upvote 0
Your code takes the raw bytes, converts them to characters and then converts them to the ascii code (actually Unicode code points) of those characters. Why??? The best way to show bytes is with ByteConverter.HexFromBytes (ByteConverter is a B4A / B4J library). If you just want to show the numbers then remove the Asc keyword.
S SimonElck Member Licensed User Feb 8, 2016 #3 How (where) do I convert the bytes to characters? That's the part I don't get. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Feb 8, 2016 #4 The Asc keyword takes a character and returns its code point value. So if the byte value is 1 it is converted to "1" and then Asc("1") returns 49. Upvote 0
The Asc keyword takes a character and returns its code point value. So if the byte value is 1 it is converted to "1" and then Asc("1") returns 49.
S SimonElck Member Licensed User Feb 8, 2016 #5 Ok, thanks I think I understand it now, and maybe it is different from other basics Upvote 0