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)
B4X:
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:
B4X:
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.
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.
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:
B4X:
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)