B4R Question B4R to B4A using B4RSerializer - Invalid Input at B4A

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

whilist working on an experiment to send data, via Bluetooth, using the B4RSerializer from B4R to B4A, receiving an error on B4A "Invalid Data" in sub NewData.

Appreciate a hint what could be the cause? Sample project attached.

B4R 1.78 (BETA #2)
B4X:
' Sending data. DistanceLabel is a String, Distance a Double
astream.Write(ser.ConvertArrayToBytes(Array(DistanceLabel, Distance)))

B4A 6.5 - with B4RSerializer.bas
B4X:
' Class Starter
Private Sub astream_NewData (Buffer() As Byte)
    CallSub2(Main, "NewValue", ser.ConvertBytesToArray(Buffer))
End Sub

' Class Main
Public Sub NewValue(data() As Object)
    Log("NewValue:"&data.Length)
    If data.Length = 0 Then Return
    For Each o As Object In data
        Log(o)
    Next
 

Attachments

  • b4rhowtodistancehcsr04bt.zip
    9.1 KB · Views: 281

rwblinn

Well-Known Member
Licensed User
Longtime User
On B4A with:
B4X:
Private Sub astream_NewData (Buffer() As Byte)
  Dim bc As ByteConverter
  Log("astream_NewData:" & bc.HexFromBytes(Buffer))
...
resulting log output
B4X:
LogCat connected to: B4A-Bridge: WIKO LENNY2
Device found: HC-05
Trying to connect...
Connected
astream_NewData:7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007035F84427F7E020844697374616E63652028636D293A20000756CA82427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007E08B83427F7E02
astream_NewData:0844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A20000756CA82427F7E020844697374616E63652028636D293A200007894C85427F7E020844697374616E63652028636D293A200007338284427F7E020844697374616E63652028636D293A200007789D83427F7E020844
astream_NewData:697374616E63652028636D293A200007035F84427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007E08B83427F7E020844697374616E63652028636D293A2000078AC182427F7E0208446973
astream_NewData:74616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A2000078AC182427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007AC9483427F7E02084469737461
astream_NewData:6E63652028636D293A2000079E4472427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A200007AC9483427F7E020844697374616E63652028636D293A2000078AC182427F7E020844697374616E63652028636D293A200007AC9483427F
astream_NewData:7E
astream_NewData:020844697374616E6365
astream_NewData:2028636D293A20000767
astream_NewData:7984427F
astream_NewData:7E
astream_NewData:020844697374616E6365
astream_NewData:2028636D293A200007
astream_NewData:112481427F
astream_NewData:7E
astream_NewData:020844697374616E63
astream_NewData:652028636D293A200007
astream_NewData:CF6784427F
astream_NewData:7E
astream_NewData:020844697374616E63
astream_NewData:652028636D293A200007
astream_NewData:45A683427F

BTW: instead of b4rserializer tested with asyncstreamtext, which is working fine.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Messages can be split or merged. B4RSerializator doesn't collect the data. It expects full valid messages.

The solution is:
1. Add a reference to ByteConverter library.
2. Add this code to Process_Globals (Starter service):
B4X:
Private DataBuffer(10000) As Byte
Private DataBufferLength As Int
Private bc As ByteConverter
Private const MARK_BEGIN = 0x7E, MARK_END = 0x7F As Byte

3. Change the code that receives the data to:
B4X:
Private Sub astream_NewData (Buffer() As Byte)
   bc.ArrayCopy(Buffer, 0, DataBuffer, DataBufferLength, Buffer.Length)
   DataBufferLength = DataBufferLength + Buffer.Length
   If FindBeginMark Then
     CallSub2(Main, "NewValue", FindEndMark) 'we have a full message here.
   End If
End Sub

Private Sub FindBeginMark As Boolean
   For i = 0 To DataBufferLength - 1
     If DataBuffer(i) = MARK_BEGIN Then Exit
   Next
   If i > 0 Then
     'remove the beginning
     bc.ArrayCopy(DataBuffer, i, DataBuffer, 0, DataBufferLength - i)
     DataBufferLength = DataBufferLength - i
   End If
   Return DataBuffer(0) = MARK_BEGIN
End Sub

Private Sub FindEndMark As Object()
   For i = 0 To DataBufferLength - 1
     If DataBuffer(i) = MARK_END And (i = DataBufferLength - 1 Or  DataBuffer(i + 1) = MARK_BEGIN) Then
       Dim b(i + 1) As Byte
       bc.ArrayCopy(DataBuffer, 0, b, 0, i + 1)
       'remove the current message
       bc.ArrayCopy(DataBuffer, i + 1, DataBuffer, 0, DataBufferLength - i - 1)
       DataBufferLength = DataBufferLength - i - 1
       Return ser.ConvertBytesToArray(b)
     End If
   Next
   Return Array()
End Sub
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Confirmed working - test project with B4R and B4A Code attached.

Note: This test project is the basis for my new B4R / B4A Maker Project "wistance" (watch distance) , to support parking, in the garage, my oldtimer car, which has no parking distance control. Communication using bluetooth. B4A app to control an Arduino UNO with several sensors. Will share soon more ... still a way to go

Appreciate your excellent help.
 

Attachments

  • b4rhowtodistancehcsr04bt.zip
    9.2 KB · Views: 288
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…