B4R Question rRF24 data handling

Michael Sergio

Member
Licensed User
Longtime User
Hi. Please help me i don`t understand how this works.
I`m trying to send this data for example from one arduino to another via RF24L01 with rRF24 library.
SERVER:
B4X:
Dim b() As Byte = Array As Byte(14, 5500)
    raf.WriteBytes(b, 0, b.Length, 0)
    rf24.Write(buffer)
CLIENT:
B4X:
Sub rf24_NewData (Data() As Byte)
    raf2.Initialize(Data, True)
    Dim b() As Byte = raf2.ReadBytes2(2, raf2.CurrentPosition)
    Dim i() As Int=bc.IntsFromBytes(b)
    Log(i(0),i(1))
End Sub
I`m having "Out of bounds error. Array length = 1, Index = 1"
If i do "Log(b(0),b(1))" then it works but i have the result in bytes, so the second number since is that large, would be "124" or something like that ( don`t remember).

Basically i have 2 numbers ( a number can be larger than 9999) i want to send that data via RF24L01 and receive the numbers and asign them to Int.

I also tried via B4Rserializator( i dont know if this method is supported)
SEND:
B4X:
Dim obj() As Object=Array(22,8888)
    rf24.Write(ser.ConvertArrayToBytes(obj))
RECEIVE:
B4X:
Sub rf24_NewData (Data() As Byte)
Dim be(10) As Object
    Dim data() As Object = ser.ConvertBytesToArray(Data, be)
    If data.Length = 0 Then Return
    Dim nrOne As Object= data(0) 
    Dim nrTwo As Object = data(1)
Log(nrOne,NrTwo)
End Sub

I`m having "Out of bounds error. "

I would like to know something that works even if i`m sending string or other types (HEX etc).

Sorry but i`m not really good with bytes to X conversion and stuff.
Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This line is wrong:
Dim b() As Byte = Array As Byte(14, 5500)

A byte cannot be larger than 255.

Start with this code:
B4X:
'bc is a global ByteConverter
Dim b() As Byte = bc.UIntsToBytes(Array As UInt(14, 5500))
Log(b.Length) 'should be 4

Receive:
B4X:
Dim data() As Byte
   If data.Length >= 4 Then
       Dim numbers() As UInt = bc.UIntsFromBytes(bc.SubString2(data, 0, 4))
   Else
     Log("Missing data")
   End If
 
Upvote 0

Michael Sergio

Member
Licensed User
Longtime User
This line is wrong:
Dim b() As Byte = Array As Byte(14, 5500)

A byte cannot be larger than 255.
Oh i tought all integer gets converted to bytes automatically..
Your solution is good for me, of course, thank you.

B4X:
Dim b() As Byte = bc.UIntsToBytes(Array As UInt(13, 5123))
 rf24.Write(b)

B4X:
Sub rf24_NewData (Data() As Byte)
    If Data.Length >= 4 Then
        Dim numbers() As UInt = bc.UIntsFromBytes(bc.SubString2(Data, 0, 4))
        Log("McNr: ",numbers(0))
        Log("Counter: ",numbers(1))
    Else
        Log("Missing data")
    End If
End Sub

Result : McNr:13,Counter:5123
 
Upvote 0
Top