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.
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?
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
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
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
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).