Android Question how i can send a "struct" with the UDP.Send(Packet)

espi

Member
Licensed User
Longtime User
Hi

i try to use udp socket and want send data from a "struct" as a Header
so i created a Type. For me would be better, i can use a List for flexible data to send
and load the data to the list ( so i have for example DataTest2 in this type )
also for me would be ok, if this data be a byte array ( like the DataTest1 )

My Base Question is, how i can send this "struct" with the UDP.Send(Packet)
Cause Packet must be Byte() but i get an error for convert the "struct" to byte().



B4X:
' global Variable
   Type Datagram ( Startbyte As Byte, _
           CMD As Byte, _
           DataTest1 As Byte(1024), _
           DataTest2 As List, _
           Endbyte As Byte)

my try was this:

B4X:
     Dim SendData1 As Datagram
     SendData1.Initialize
     Dim MyData As List
     MyData.Initialize
     MyData.AddAll(Array As Byte(0xFF, 0xFB, 0xFC))
   
     SendData1.Startbyte = 0x9C
     SendData1.CMD  = 0xDD
     SendData1.DataTest1 = "LINE A".GetBytes("UTF8")
     SendData1.DataTest2 = MyData
     SendData1.Endbyte  = 0x33
   
     Dim Packet As UDPPacket
     Packet.Initialize(SendData, PanelIP, PanelPort)
     UDPSocket1.Send(Packet)

Thanks for any help.
Regards

espi
 

espi

Member
Licensed User
Longtime User
You can use B4XSerializator to convert a custom type or more complex collections to bytes.

thank you Erel,
i had a try with this now and did some other things try too, but i get not the right data, they are over 156 bytes long
can it be, that i do wrong with the custom type?
any help/hint/info is welcome.

It is a simple "UDP" client to send the custom type, with header there are 7 Byte and data can be 1 do 1024 byte (total 64*32*3 = 6144 ) and can be split
in 6 packets example a 1024 data, this is for manage than a RGB LED Panel ( 64*32 )

later, the data can be a "file" too, example a picture, to show it on the RGB LED Panel.
I use the tmp2.net header for this.
https://gist.github.com/jblang/89e24e2655be6c463c56


B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Private ser As B4XSerializator
   Dim UDPSocket1 As UDPSocket
   Dim raf As RandomAccessFile
   Dim PanelIP As String = "192.168.4.1"
   Dim PanelPort As Int  = 65506

   Type tmp2net (   StartB As Int, _
           CmdB As Int, _
           HszB As Int, _
           LszB As Int, _
           PkId As Int, _
           PkCnt As Int, _
           RGBData(3) As Byte , _
           Endb As Int )
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Private ToggleButton1 As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     UDPSocket1.Initialize("UDP",60300,8000)
   End If

   'Do not forget To load the layout File created with the visual designer. For example:
   Activity.LoadLayout("me1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub ToggleButton1_CheckedChange(Checked As Boolean)

   Dim RGBList As List
   RGBList.Initialize
   If Checked Then
   RGBList.AddAll(Array As Int(0xFF, 0xFF, 0xFF))
   Else
   RGBList.AddAll(Array As Int(0x01, 0x01, 0x01))
   End If
  
   ' this was a test 
   'Dim RGB(3) As Byte
   'If Checked Then
   '   RGB(0) = 0xFF
   '   RGB(1) = 0XFF
   '   RGB(2) = 0XFF
   'Else
   '   RGB(0) = 0x01
   '   RGB(1) = 0X01
   '   RGB(2) = 0X01
   'End If
  
   Dim Datagram As tmp2net
   Datagram.Initialize
   Datagram.StartB = 0x9D
   Datagram.CmdB    = 0xDA
   Datagram.HszB   = 0x00
   Datagram.LszB   = 0x03
   Datagram.PkId   = 0x01
   Datagram.PkCnt   = 0x01
   ' Datagram.RGBData = RGB  ' RGB as Array of Int
   Datagram.RGBData  = ser.ConvertObjectToBytes(RGBList)
   Datagram.Endb    = 0x36

   'Test    
   'Dim buf() As Byte
   'raf.Initialize3(buf,False)
   'raf.WriteBytes(ser.ConvertObjectToBytes(Datagram),0,10,0)

   Dim Packet As UDPPacket
   Packet.Initialize(ser.ConvertObjectToBytes(Datagram),PanelIP, PanelPort)
   'Test
   'Packet.Initialize(buf,PanelIP, PanelPort)
   UDPSocket1.Send(Packet)
 
End Sub

On µController Debug Log over UART
first data's delivery on µController like this on toggle the botton:

udp_perf:: #DEBUG: Packet is 156 long
udp_perf:: Got: 78 9c 93 60 14 67 60 60 48 32 49 d4 2b 4d 29 30

udp_perf:: #DEBUG: Packet is 156 long
udp_perf:: Got: 78 9c 93 60 14 67 60 60 48 32 49 d4 2b 4d 29 30

thank you
Regards

espi
 
Last edited:
Upvote 0
Top