0

I have a radar and I want to read the UDP packets that are sent by the radar via ethernet port. So I created this script :

import socket
UDP_PORT = 2700
interface=""

sock = socket.socket(socket.AF_INET, # Ethernet
                     socket.SOCK_DGRAM) # UDP

sock.bind((interface, UDP_PORT))

while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print "received message:", data

But when I runt it, I receive strange messages like this:

enter image description here

In the user manual of the radar, it says that the packet consists of the following fields:

  • PacketType:(unsigned byte)
  • TargetCount: (unsigned byte)
  • PacketID: (unsigned int32)
  • Reserved: (unsigned byte[32])
  • Targets: (RadarTarget[TargetCount]: An array of the detected targets.

How can I read this field from the UDP packet please???

Thank you in advance

atlan
  • 41
  • 6
  • You see the text representation of your binary data there. You will have to spilt up the fields and convert them to the given data type. `struct.unpack()` might be helpful: https://docs.python.org/2/library/struct.html – Klaus D. Jul 17 '17 at 11:22
  • Can you give me an example how can I use this function please?? Thank you very much – atlan Jul 17 '17 at 11:41
  • `socket.AF_INET` doesn't mean "ethernet", it rather means IPv4. – u354356007 Jul 17 '17 at 15:51
  • There's a plenty of examples of using `struct` on the internet. https://pymotw.com/3/struct/index.html - this one looks good. – u354356007 Jul 17 '17 at 15:52
  • I modified the code, by adding this lines: (type, count,id, reserved1, reserved2,reserved3,reserved4, speed, angle, distance, signal,targetid)=struct.unpack('!2B I 4B 4f I',data) print (type, count,id, reserved1, reserved2,reserved3,reserved4, speed, angle, distance, signal,targetid) – atlan Jul 18 '17 at 09:12
  • but I got this error: (type, count,id, reserved1, reserved2,reserved3,reserved4, speed, angle, distance, signal,targetid)=struct.unpack('!2B I 4B 4f I',data) struct.error: unpack requires a string argument of length 30 Thank you for helping me – atlan Jul 18 '17 at 09:13

0 Answers0