B4J Question (SOLVED)UDP packet not decoding

alienhunter

Active Member
Licensed User
Longtime User
Hi
i am trying to decode a UDP packet but it does not work somehow
I tried byteconvertor and still nothing
the UDP comes from
https://www.perle.com/products/iolan-ds-terminal-server.shtml#features
and it sends Raw serial data over UDP.
anyone knows how to
thank you
AH


the code
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim UDPSocket1 As UDPSocket
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    UDPSocket1.Initialize("UDP",2000, 20000)
MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Log(msg)
    Log("PACKET LENGTH:" & Packet.Length)
    Log("PACKET OFFSET:" & Packet.Offset)
    Log("PACKET STRING:" & Packet.toString)

End Sub

this is the output in the log
Capture.JPG

Capture.JPG
 

Daestrum

Expert
Licensed User
Longtime User
Have you tried other encodings other than UTF-8, maybe it's just plain ascii.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Do you know what data it should be sending?

if so use ByteConverter > HexFromBytes to see the data in hex, then it should be easier to see how it has been encoded.
 
Upvote 0

alienhunter

Active Member
Licensed User
Longtime User
Do you know what data it should be sending?

if so use ByteConverter > HexFromBytes to see the data in hex, then it should be easier to see how it has been encoded.

ok i will try


as far i can understand it shoud be int or float , it should return a temperature value or a volt value
 
Upvote 0

alienhunter

Active Member
Licensed User
Longtime User
Do you know what data it should be sending?

if so use ByteConverter > HexFromBytes to see the data in hex, then it should be easier to see how it has been encoded.

This is the hex
PACKET HEX:0000410037076C3B00006E6FBFC5ADC6C1200000BFAFA5B00000FFFF00000000000041200000C12000000000FFFF00000000000041200000C12000000002010000020000
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
as far i can understand it shoud be int or float , it should return a temperature value or a volt value
do you have an precise descriptiom about the packet?

Using UDP-Packet decoding for a PS/4 Game i used RandomAccessfile to parse the Packets

Example code with no relation to your packet.

The Indexes used in the code are Values from the F1 game packets...

You need to use the right index for your packets.
See packetdescription to find out

If you know the right index and you know there must be a value (int, float, long) you can pase the packet into the right values.
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim raf As RandomAccessFile
    raf.Initialize3(Packet.Data, True) 'change the endianess if the version number is incorrect
    log(ShortToUnsigned(raf.ReadShort(0)))
    dim time as Long = raf.ReadLong(4) ' example to read a LONG value from index 4
   log(raf.ReadFloat(12))
   log(raf.ReadInt(16))

end sub
Sub ShortToUnsigned(s As Short) As Int
   Return Bit.And(0xFFFF, s)
End Sub
 
Last edited:
Upvote 0

alienhunter

Active Member
Licensed User
Longtime User
do you have an precise descriptiom about the packet?

Using UDP-Packet decoding for a PS/4 Game i used RandomAccessfile to parse the Packets

Example code with no relation to your packet.

The Indexes used in the code are Values from the F1 game packets...

You need to use the right index for your packets.
See packetdescription to find out

If you know the right index and you know there must be a value (int, float, long) you can pase the packet into the right values.
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim raf As RandomAccessFile
    raf.Initialize3(Packet.Data, True) 'change the endianess if the version number is incorrect
    log(ShortToUnsigned(raf.ReadShort(0)))
    dim time as Long = raf.ReadLong(4) ' example to read a LONG value from index 4
   log(raf.ReadFloat(12))
   log(raf.ReadInt(16))

end sub
Sub ShortToUnsigned(s As Short) As Int
   Return Bit.And(0xFFFF, s)
End Sub

Thanks
i tried to make sense on every index to what i see in the controller looking for -3.7627
Capture.JPG

and it does not

i found something that may help
Capture.JPG

see code below


Dim raf As RandomAccessFile
raf.Initialize3(Packet.Data, True) 'change the endianess if the version number is incorrect

For i = 0 To Packet.Length-1
Try
Dim xx As String=raf.ReadDouble(i)
If xx.StartsWith("3") Then
Log(raf.ReadFloat(i))
Dim xd As Float=raf.ReadFloat(i)
Log(xd)
End If

Catch
End Try
Next
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
see code below
Please use [code]code here...[/code] tags when posting code.
There are many mistakes in your code.

Reading the data based on the structure you posted:
B4X:
Dim length As Short = raf.ReadShort(raf.CurrentPosition)
Dim enabled As Byte = raf.ReadByte(raf.CurrentPosition)
Dim CurRawValue As Short = raf.ReadShort(raf.CurrentPosition)
...
4 bytes is either Int or Float. Never Double.
 
Upvote 0

alienhunter

Active Member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.
There are many mistakes in your code.

Reading the data based on the structure you posted:
B4X:
Dim length As Short = raf.ReadShort(raf.CurrentPosition)
Dim enabled As Byte = raf.ReadByte(raf.CurrentPosition)
Dim CurRawValue As Short = raf.ReadShort(raf.CurrentPosition)
...
4 bytes is either Int or Float. Never Double.


thank you very much for your time and code
yes i know the code is not correct :oops: i was giving this a try i did not know further how to
only one thing
i do not have the raf.ReadByte member raf.readbytes using RandomAccessFile 1.15 version


But it worked with CurRawValue
this is a udp stream used in mtconnect from a machine

B4j = VALUE :10486
Mtconnect = "ACTUAL">10486.0000000000</PathFeedrate>

thanks a lot made my day :)
Alfred
 
Upvote 0
Top