P positrom2 Active Member Licensed User Longtime User Jun 25, 2012 #1 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
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
Erel B4X founder Staff member Licensed User Longtime User Jun 25, 2012 #2 You can use ByteConverter.ShortsFromBytes: Basic4android - ByteConverter Upvote 0
P positrom2 Active Member Licensed User Longtime User Jun 25, 2012 #3 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: Code: Copy 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: Jun 25, 2012 Upvote 0
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: Code: Copy 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
P pbmi Member Licensed User Longtime User Jun 25, 2012 #4 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
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
P positrom2 Active Member Licensed User Longtime User Jun 26, 2012 #5 In B4A, array declarations are 1-based Click to expand... 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
In B4A, array declarations are 1-based Click to expand... 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
Erel B4X founder Staff member Licensed User Longtime User Jun 27, 2012 #6 Dim Bytes(0) as Byte Click to expand... This line declares an empty array. Upvote 0
P positrom2 Active Member Licensed User Longtime User Jun 27, 2012 #7 This line declares an empty array. Click to expand... 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
This line declares an empty array. Click to expand... 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
Erel B4X founder Staff member Licensed User Longtime User Jun 27, 2012 #8 An array is actually an object. This means that when you assign another array to it (or declare it again) then it will reference the new array. I don't really understand how your data is formatted. Is it a stream of bytes? Upvote 0
An array is actually an object. This means that when you assign another array to it (or declare it again) then it will reference the new array. I don't really understand how your data is formatted. Is it a stream of bytes?
P positrom2 Active Member Licensed User Longtime User Jun 27, 2012 #9 Is it a stream of bytes? Click to expand... 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
Is it a stream of bytes? Click to expand... 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
Erel B4X founder Staff member Licensed User Longtime User Jun 27, 2012 #10 I suggested you to use ByteConverter.ShortsFromBytes. Have you tried it? Short is a 2 byte number. Upvote 0
P positrom2 Active Member Licensed User Longtime User Jun 27, 2012 #11 ByteConverter.ShortsFromBytes Click to expand... 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
ByteConverter.ShortsFromBytes Click to expand... 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
Erel B4X founder Staff member Licensed User Longtime User Jun 27, 2012 #12 ByteConverter allows you to set the byte order. You should not use a text reader. Either read the data with InputStream.ReadBytes or use AsyncStreams. Upvote 0
ByteConverter allows you to set the byte order. You should not use a text reader. Either read the data with InputStream.ReadBytes or use AsyncStreams.
P positrom2 Active Member Licensed User Longtime User Jun 27, 2012 #13 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: Jun 27, 2012 Upvote 0
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
Erel B4X founder Staff member Licensed User Longtime User Jun 28, 2012 #14 Where is the data coming from? Upvote 0
P positrom2 Active Member Licensed User Longtime User Jun 28, 2012 #15 Where is the data coming from? Click to expand... 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
Where is the data coming from? Click to expand... 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
Erel B4X founder Staff member Licensed User Longtime User Jun 28, 2012 #16 I recommend you to go over the Network tutorial and AsyncStreams tutorial. Upvote 0
P positrom2 Active Member Licensed User Longtime User Jun 28, 2012 #17 I recommend you to go over the Network tutorial and AsyncStreams tutorial. Click to expand... 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. Code: Copy 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). Click to expand... I tried to initialze somehow but nothing worked. I conclude that I have to give up. regards, positrom2 Upvote 0
I recommend you to go over the Network tutorial and AsyncStreams tutorial. Click to expand... 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. Code: Copy 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). Click to expand... I tried to initialze somehow but nothing worked. I conclude that I have to give up. regards, positrom2