How to decode binary stream of words to an array?

positrom2

Active Member
Licensed User
Longtime User
I want to receive 16bit word data that are sent from a microcontroller in binary format. The binary stream would look like MSBLSB...MSBLSBMSBLSB....CHRLF (a sequence of 16bit words). How could I receive and decode the stream to an (integer) array in B4A?

At present I am transmitting each word as ascii value and receiving it using readline. The reduction of transmitted characters by sending binary data would be enormous.

Thanks, positrom2
 

positrom2

Active Member
Licensed User
Longtime User
Thanks, Erel,
but I am stuck. Example for the received data:
(above the HL order was in error).

The number 16706=66+256*65 looks in Hex= 4241, as string: BA. So I tried:
B4X:
Dim Bytes(0) As Byte
Dim stringg As String
stringg="BA"
Bytes=Conv.StringToBytes(stringg,"LittleEndian")
But Bytes stays empty: []. What' the mistake?

Thanks, positrom2
 
Last edited:
Upvote 0

pbmi

Member
Licensed User
Longtime User
In B4A, array declarations are 1-based, ie for a 1 element:

Dim Bytes(1) as Byte

Sent from my GT-I9100 using Tapatalk 2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
In B4A, array declarations are 1-based
I am not so sure about that. I took the declaration "Dim Bytes(0) as Byte" from the ByteConverterDemo.b4a distributed with the agraham's library. The demo works fine. I also tried with larger dimensioning Bytes, but Bytes stays empty ([]).

What I have to do is:
1. Receive a stream of 16bit words (order LH) without separators using readline into a string.
2. Convert the string to a byte array
3. convert the byte array to a short array (hopefully the order of bytes LH would be right for using Conv.ShortsFromBytes).
Or is there an alternative?

Regards, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
This line declares an empty array.
So the final (fixed) dimension is set by first assignement?

Back to my conversion problem:
I learned meanwhile that it is unwise to use string for holding binary numbers.
But how to get receiving and conversion mission finished remains in dark despite having spent all day yesterday reading related posts and documentions.
Any further help appreciated.
positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Is it a stream of bytes?
Yes, LHLH..., 256 LH pairs of acquired data numbers (range: 0...65000), sent via bluetooth (BT connection works fine) from µcontroller (i.e. 256 numbers @ 16bit) in one bunch followed by CRLF (or whatever were needed).
Thanks, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
ByteConverter.ShortsFromBytes
Yes I tried, but it did not work since I was using readline which becomes stuck when non-ascii characters are coming in. So I guess one has to switch to asyncstreams which I did not manage. The byte order (here LH) that ByteConverter.ShortsFromBytes needs would be right?
Regards, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
What is needed (initialize, etc?)

Dim InputStream1 As InputStream
i = InputStream1.ReadBytes(buffer, 0,2)

The error is:
java.lang.RuntimeException: Object should first be initialized (InputStream).

But I cannot figure out how to do, inputstream1 does not allow to be initialized.
positrom2
 
Last edited:
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Where is the data coming from?
From a BT module. In some tutorials I remember to have seen "socket1.inputstream.".

"socket1" has to be replaced by something else to refer to the existing BT connection?
Thanks, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
I recommend you to go over the Network tutorial and AsyncStreams tutorial.
As I mentioned in previous posts I had spent two days by now studying those and various other tutorials and the documentations. Unfortunately, with all the information I was not able to to get my application working.

Apparently inputstream has to be told from where to receive the stream. However, if I put anything in front of inputstream1.readbytes referring to the BT connection I receive a compilation error.
B4X:
Dim BTdata As Serial
Dim InputStream1 As InputStream
...
InputStream1.ReadBytes(buffer, 0,10)
This compiles but throws at runtime the error
java.lang.RuntimeException: Object should first be initialized (InputStream).
I tried to initialze somehow but nothing worked. I conclude that I have to give up.

regards, positrom2
 
Upvote 0
Top