A atiaust Active Member Licensed User Longtime User Apr 7, 2019 #1 Hi All, I have a B4R app that reads data from an RS232 device. I am sending the contents of the communications buffer in bytes to a B4J app using 'astream.write(buffer) in B4R. in B4R I use this code to get the data to display a reading (data is a double) Code: Copy Sub astream_newData (Buffer() as byte) Dim b(4) as byte Dim bc as ByteConverter bc.ArrayCopy2(Buffer, 2, b, 0, 4) Dim d() as double = bc.DoubleFromBytes(b) log("Reading = ",d(0)) End Sub This works in B4R but not in B4J. Code: Copy Dim bc as ByteConverter Dim s(4) As Byte bc.ArrayCopy(Buffer,6,s,0,4) Dim d() As Double = bc.DoublesFromBytes(s) Log("d = "&d(0)) In B4J I get the following error - note: when the array is sent from B4R to B4J 4 extra bytes is added. All data after "0F000000" is correct so I use an offset of 6 instead of 2 in the array copy. Spoiler: error a = 0F000000AA103A86A73C0000000048006000FB len = 19 Error occurred on line: 72 (Main) java.lang.ArrayIndexOutOfBoundsException: 0 at b4j.example.main._astream_newdata(main.java:142) Any one have any ideas? Thanks
Hi All, I have a B4R app that reads data from an RS232 device. I am sending the contents of the communications buffer in bytes to a B4J app using 'astream.write(buffer) in B4R. in B4R I use this code to get the data to display a reading (data is a double) Code: Copy Sub astream_newData (Buffer() as byte) Dim b(4) as byte Dim bc as ByteConverter bc.ArrayCopy2(Buffer, 2, b, 0, 4) Dim d() as double = bc.DoubleFromBytes(b) log("Reading = ",d(0)) End Sub This works in B4R but not in B4J. Code: Copy Dim bc as ByteConverter Dim s(4) As Byte bc.ArrayCopy(Buffer,6,s,0,4) Dim d() As Double = bc.DoublesFromBytes(s) Log("d = "&d(0)) In B4J I get the following error - note: when the array is sent from B4R to B4J 4 extra bytes is added. All data after "0F000000" is correct so I use an offset of 6 instead of 2 in the array copy. Spoiler: error a = 0F000000AA103A86A73C0000000048006000FB len = 19 Error occurred on line: 72 (Main) java.lang.ArrayIndexOutOfBoundsException: 0 at b4j.example.main._astream_newdata(main.java:142) Any one have any ideas? Thanks
Erel B4X founder Staff member Licensed User Longtime User Apr 7, 2019 #2 B4R double = B4J / B4A / B4i float Tip: Use B4RSerializator. Upvote 0
A atiaust Active Member Licensed User Longtime User Apr 7, 2019 #3 Thanks Erel, I tried using B4RSerializator with out much success. Can you point me in the right direction please. Thanks Upvote 0
Thanks Erel, I tried using B4RSerializator with out much success. Can you point me in the right direction please. Thanks
Erel B4X founder Staff member Licensed User Longtime User Apr 7, 2019 #4 [B4X] B4RSerializator - Send and receive objects instead of bytes Upvote 0
A atiaust Active Member Licensed User Longtime User Apr 7, 2019 #5 Thank Erel, If I use B4RSerializator to convert the buffer how do I write it out in B4R? Code: Copy Dim ser as B4RSerializator Dim be(15) As Object 'used as a storage buffer. Dim objects() As Object = ser.ConvertBytesToArray(Buffer, be) astream.Write = ???? Upvote 0
Thank Erel, If I use B4RSerializator to convert the buffer how do I write it out in B4R? Code: Copy Dim ser as B4RSerializator Dim be(15) As Object 'used as a storage buffer. Dim objects() As Object = ser.ConvertBytesToArray(Buffer, be) astream.Write = ????
Erel B4X founder Staff member Licensed User Longtime User Apr 7, 2019 #6 You need to use ser.ConvertArrayToBytes and send the bytes. Upvote 0
A atiaust Active Member Licensed User Longtime User Apr 7, 2019 #7 Hi Erel, I am able to get the value in B4J using the Float as you suggested earlier. I now have a float number, example '0.023456789' using byteconverter bc.FloatsFromBytes(s) How can I get it as a string value with 3 decimal places. formatnumber doesn't seem to work for a float? Thanks Upvote 0
Hi Erel, I am able to get the value in B4J using the Float as you suggested earlier. I now have a float number, example '0.023456789' using byteconverter bc.FloatsFromBytes(s) How can I get it as a string value with 3 decimal places. formatnumber doesn't seem to work for a float? Thanks
Erel B4X founder Staff member Licensed User Longtime User Apr 7, 2019 #8 You should start a new thread for a new question. Upvote 0