canalrun Well-Known Member Licensed User Longtime User May 22, 2016 #1 I am using the Serial Connector Tutorial to build a plotting window. Works great except ... I don't think I am using GetBytes properly. I thought the follow two snippets would give the same buf(): Code: Dim spts As String = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" Dim buf() As Byte = spts.GetBytes and Code: Dim buf() As Byte = Array As Byte( _ 0x70,0x6c,0x6f,0x74,0x3d,0x30,0x2e,0x39,0x2c,0x30, _ 0x2e,0x36,0x2c,0x30,0x2e,0x33,0x2c,0x30,0x2e,0x31, _ 0x2c,0x30,0x2e,0x32,0x2c,0x30,0x2e,0x33,0x2c,0x30, _ 0x2e,0x34,0x2c,0x30,0x2e,0x35,0x2c,0x30,0x2e,0x36) The second one gives what I expect, the first doesn't. What am i doing wrong? Barry
I am using the Serial Connector Tutorial to build a plotting window. Works great except ... I don't think I am using GetBytes properly. I thought the follow two snippets would give the same buf(): Code: Dim spts As String = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" Dim buf() As Byte = spts.GetBytes and Code: Dim buf() As Byte = Array As Byte( _ 0x70,0x6c,0x6f,0x74,0x3d,0x30,0x2e,0x39,0x2c,0x30, _ 0x2e,0x36,0x2c,0x30,0x2e,0x33,0x2c,0x30,0x2e,0x31, _ 0x2c,0x30,0x2e,0x32,0x2c,0x30,0x2e,0x33,0x2c,0x30, _ 0x2e,0x34,0x2c,0x30,0x2e,0x35,0x2c,0x30,0x2e,0x36) The second one gives what I expect, the first doesn't. What am i doing wrong? Barry
Erel B4X founder Staff member Licensed User Longtime User May 22, 2016 #2 What is the expected value? What is the purpose of this array? Upvote 0
canalrun Well-Known Member Licensed User Longtime User May 22, 2016 #3 I am basically using your serial connector tutorial. When the serial connector communicates between the Arduino and B4J application it wants to send a buffer of bytes rather than a string. What I would like to do is send the string, "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6", encoded as a buffer of bytes, to my B4J application from the Arduino. My B4J application will recognize this as a command to plot the nine values on a bar graph. The buffer of bytes it should send is equivalent to: (the data below is just the string above encoded as bytes) Code: Dim buf() As Byte = Array As Byte( _ 0x70,0x6c,0x6f,0x74,0x3d,0x30,0x2e,0x39,0x2c,0x30, _ 0x2e,0x36,0x2c,0x30,0x2e,0x33,0x2c,0x30,0x2e,0x31, _ 0x2c,0x30,0x2e,0x32,0x2c,0x30,0x2e,0x33,0x2c,0x30, _ 0x2e,0x34,0x2c,0x30,0x2e,0x35,0x2c,0x30,0x2e,0x36) But, within B4J I can look at the actual buffer that is received when I encode the string using GetBytes (or the byte converter equivalent). It looks like the data was read from some random location in memory – not even close to what it should be. (kind of like a pointer error). My B4J plot application works if I send the explicitly defined buf() shown just above. But, just replacing my explicitly defined buf() with: Code: dim str as string = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" dim buf() as byte = str.GetBytes it does not work. The question boils down to, "how do I convert a string to a buffer of bytes?" I tried GetBytes, but that doesn't seem to work or I'm not using it correctly. Barry. Last edited: May 22, 2016 Upvote 0
I am basically using your serial connector tutorial. When the serial connector communicates between the Arduino and B4J application it wants to send a buffer of bytes rather than a string. What I would like to do is send the string, "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6", encoded as a buffer of bytes, to my B4J application from the Arduino. My B4J application will recognize this as a command to plot the nine values on a bar graph. The buffer of bytes it should send is equivalent to: (the data below is just the string above encoded as bytes) Code: Dim buf() As Byte = Array As Byte( _ 0x70,0x6c,0x6f,0x74,0x3d,0x30,0x2e,0x39,0x2c,0x30, _ 0x2e,0x36,0x2c,0x30,0x2e,0x33,0x2c,0x30,0x2e,0x31, _ 0x2c,0x30,0x2e,0x32,0x2c,0x30,0x2e,0x33,0x2c,0x30, _ 0x2e,0x34,0x2c,0x30,0x2e,0x35,0x2c,0x30,0x2e,0x36) But, within B4J I can look at the actual buffer that is received when I encode the string using GetBytes (or the byte converter equivalent). It looks like the data was read from some random location in memory – not even close to what it should be. (kind of like a pointer error). My B4J plot application works if I send the explicitly defined buf() shown just above. But, just replacing my explicitly defined buf() with: Code: dim str as string = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" dim buf() as byte = str.GetBytes it does not work. The question boils down to, "how do I convert a string to a buffer of bytes?" I tried GetBytes, but that doesn't seem to work or I'm not using it correctly. Barry.
Erel B4X founder Staff member Licensed User Longtime User May 22, 2016 #4 Your B4R code is correct. Code: dim str as string = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" dim buf() as byte = str.GetBytes You can check it with Log(buf). Upvote 0
Your B4R code is correct. Code: dim str as string = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" dim buf() as byte = str.GetBytes You can check it with Log(buf).
canalrun Well-Known Member Licensed User Longtime User May 22, 2016 #5 Hello, Thanks. If the code looks correct, I just thought of something else that may be causing the problem. I keep saying Arduino, but I'm actually using a WeMos D1 R2. I have run the B4R Blink tutorial and the unmodified Serial Connector tutorial – they both work fine. But I'm wondering if this is due to some strange incompatibility. Tomorrow I will try this on my Arduino Mega board. Is there another way I could iterate along each character of a string and copy the value into a byte buffer – maybe using a for loop? I was looking for the str.charat() method, but this seems to not exist in B4R. Barry. Upvote 0
Hello, Thanks. If the code looks correct, I just thought of something else that may be causing the problem. I keep saying Arduino, but I'm actually using a WeMos D1 R2. I have run the B4R Blink tutorial and the unmodified Serial Connector tutorial – they both work fine. But I'm wondering if this is due to some strange incompatibility. Tomorrow I will try this on my Arduino Mega board. Is there another way I could iterate along each character of a string and copy the value into a byte buffer – maybe using a for loop? I was looking for the str.charat() method, but this seems to not exist in B4R. Barry.
Erel B4X founder Staff member Licensed User Longtime User May 22, 2016 #6 String.CharAt is equivalent to: Code: Dim str() As Byte = "abc" For i = 0 To str.Length - 1 Log(str(i)) Next Make sure to go over this tutorial: Strings and Bytes Upvote 0
String.CharAt is equivalent to: Code: Dim str() As Byte = "abc" For i = 0 To str.Length - 1 Log(str(i)) Next Make sure to go over this tutorial: Strings and Bytes
canalrun Well-Known Member Licensed User Longtime User May 22, 2016 #7 Thanks. I tried with the Arduino Mega today - same code works. The problem must be related to a WeMos D1 R2 difference. Here is the output of my B4J modified Serial Connector: Here is the relevant B4R Code: Code: Sub Astream_NewData (Buffer() As Byte) Dim cb() As Byte = "go" If (cmpBytes(Buffer, cb)) Then Dim lm() As Byte = "log=Got go" Log (lm) Dim buf() As Byte = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" Log(buf) Else Log(Buffer) End If End Sub I find it sometimes easier to visualize data as a graph (FFT output for example) I will post all the source code later. Barry. Upvote 0
Thanks. I tried with the Arduino Mega today - same code works. The problem must be related to a WeMos D1 R2 difference. Here is the output of my B4J modified Serial Connector: Here is the relevant B4R Code: Code: Sub Astream_NewData (Buffer() As Byte) Dim cb() As Byte = "go" If (cmpBytes(Buffer, cb)) Then Dim lm() As Byte = "log=Got go" Log (lm) Dim buf() As Byte = "plot=0.9,0.6,0.3,0.1,0.2,0.3,0.4,0.5,0.6" Log(buf) Else Log(Buffer) End If End Sub I find it sometimes easier to visualize data as a graph (FFT output for example) I will post all the source code later. Barry.
canalrun Well-Known Member Licensed User Longtime User May 23, 2016 #8 I have uploaded the source for the Serial Connector Graph to: https://www.b4x.com/android/forum/threads/serial-connector-graph.67174/ Barry. Upvote 0
I have uploaded the source for the Serial Connector Graph to: https://www.b4x.com/android/forum/threads/serial-connector-graph.67174/ Barry.
Erel B4X founder Staff member Licensed User Longtime User May 23, 2016 #9 Note that you don't need to use cmpBytes. You can write If Buffer = cb Then ... Upvote 0