B4J Question UDP Message - binary

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to work out how to send a UDP message which contains binary 5 bytes and get the reply.

The device I am sending the message to is on the local network.

I have been told:

Use UDP to send the binary 5 bytes 0x007FFFFFFF to destination port 12345. The 7FFFFFFF is a mask for what you want returned.

I am currently trying to get it working in B4J for testing, but eventually I am planning to do this in B4A/B4i as well.

I have tired the following, but I am guessing I am doing it wrong as it doesn't return anything.

Anyone know how to do this, and where I have gone wrong ?

B4X:
Sub Process_Globals
    
    Dim UDP_Listen As UDPSocket        'jnetwork lib
    Dim Packet1 As UDPPacket        'jnetwork lib
    
    Dim data() As Byte
    
End Sub

Sub AppStart (Args() As String)
    
    UDP_Listen.Initialize("UDP_Listen",12345,9999999)
    
    RunMe
    
    StartMessageLoop
    
End Sub

Sub RunMe
    
    Dim msg As String = "0x007FFFFFFF"
    
    data = msg.GetBytes("UTF8")
    Packet1.Initialize(data, "192.168.0.33", 12345)
    UDP_Listen.Send(Packet1)
        
End Sub

Sub UDP_Listen_PacketArrived (Packet As UDPPacket)
    
    Dim msg1 As String
    msg1 = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")

    Log(msg1)
    
End Sub
 

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
Sub RunMe
    
    Dim msg As String = "007FFFFFFF"
    'Use ByteConverter library
    Dim bc As ByteConverter
    
    data = bc.HexToBytes(msg)
    Packet1.Initialize(data, "192.168.0.33", 12345)
    UDP_Listen.Send(Packet1)
        
End Sub

Sub UDP_Listen_PacketArrived (Packet As UDPPacket)
    
    Dim msg1 As String
    Dim bc As ByteConverter
    Dim temp(Packet.Length) As Byte
    bc.ArrayCopy(Packet.Data, Packet.Offset, temp, 0, Packet.Length)
    msg1 = bc.HexFromBytes(temp)

    Log(msg1)
    
End Sub
Note:
B4X:
    data = bc.HexToBytes(msg)
Produces and byte array content of
data(0) = 00
data(1) = 7F
data(2) = FF
data(3) = FF
data(4) = FF
If the destination is expecting
data(0) = FF
data(1) = FF
data(2) = FF
data(3) = F7
data(4) = 00
You can do this:
B4X:
Sub RunMe
    
    Dim msg As String = StrReverse("007FFFFFFF")
    'Use ByteConverter library
    Dim bc As ByteConverter
    
    data = bc.HexToBytes(msg)
    Packet1.Initialize(data, "192.168.0.33", 12345)
    UDP_Listen.Send(Packet1)
        
End Sub

Sub UDP_Listen_PacketArrived (Packet As UDPPacket)
    
    Dim msg1 As String
    Dim bc As ByteConverter
    Dim temp(Packet.Length) As Byte
    bc.ArrayCopy(Packet.Data, Packet.Offset, temp, 0, Packet.Length)
    msg1 = bc.HexFromBytes(temp)

    'No clue how data is sent back
    Log(msg1)
    Log(StrReverse(msg1)
    
End Sub

'https://www.b4x.com/android/forum/threads/reverse-text-flip-text.12200/post-137270
Sub StrReverse(Text As String) As String
    Dim tempstr As StringBuilder,temp As Int
    tempstr.Initialize
    For temp = Text.Length-1 To 0 Step -1
        tempstr.Append( Text.CharAt(temp) )
    Next
    Return tempstr.ToString
End Sub

Final note: Untested code. May contain typo(s) / logical errors
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
I'm curious to which device you're sending the information. Usually you start by putting a command in a command buffer of a certain length. You are talking about sending a 5 byte mask of 007FFFFFFF and a response of only 4 bytes. I think you're confusing command and mask. A mask is often used to turn certain bits on or off as part of a commando.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I managed to get it working.

I sent it with:
B4X:
Sub RunMe
    
    Dim msg As String = "007FFFFFFF"
    'Use ByteConverter library
    Dim bc As ByteConverter
    
    data = bc.HexToBytes(msg)
    Packet1.Initialize(data, "192.168.0.33", 12345)
    UDP_Listen.Send(Packet1)
        
End Sub

I had to use the following code in PacketArrived Sub and it allowed me to get the reply.

B4X:
Sub UDP_Listen_PacketArrived (Packet As UDPPacket)
    
    Dim msg1 As String
    msg1 = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")

    Log(msg1)
    
End Sub

For some reason it must have been the way I was sending it.
 
Upvote 0
Top