简体   繁体   English

用Python解释F1 2012 UDP数据包

[英]Interpreting F1 2012 UDP packets in Python

I am working on a project where in game telemetry data from F1 2012 is interpreted by Python and sent to an Arduino to display as a physical dashboard. 我正在一个项目中,Python将解释F1 2012中的游戏遥测数据,并将其发送到Arduino以显示为物理仪表板。

F1 2012 sends the data via UDP packets at address 127.0.0.1, port 20777. F1 2012通过UDP数据包在地址127.0.0.1,端口20777发送数据。

I have a simple python program that reads the incoming packets: 我有一个简单的python程序,可以读取传入的数据包:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 20777

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024)
    print ("Message:" data)

This outputs messages such as: 这将输出如下消息:

b'\xce\xb3T@\x00\x00\x00\x001K\xa7E\xa0\xf1\xf0\xbc\xa1\xc6\x13\xc4OY\x0b\xc3\xd5\xbf.D\xca\xe2\xc2;\xb0\x9109\xf0\x19\xbd\xbb\xdd8\xbb:q\x19M\xbf\x81t.933\x19\xbf\x1e,\x19\xbf\xc2\x82\x9d\xbc\x9c\x0fM?\xa2#H\xbc\xd6\x16x\xbe\x92hr\xba:\xa8\x88=X\xd7&?q$\xc7>\x82w#><\xe9\x1d>w\xc9\xb8\xa7Bh\x010\x00\x00\x00\x00kr\xb7\xaf\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x80?\x96z\x98\xb9!f\x849\x00\x00\x00\x00\x8d\x8b5C'

I know this is a rather long output but it contains 38 parameters about the car which refresh 60 times a second. 我知道这是一个相当长的输出,但是它包含有关汽车的38个参数,每秒刷新60次。 These are outlined in this website by someone working on a similar project. 这些是由从事类似项目的人员在本网站上概述的。

I have looked for similar questions on stackeoverflow with outputs from UDP similar to mine and I was informed that the data had to be unpacked with a suitable format. 我已经在stackeoverflow上寻找了类似的问题,其中UDP的输出与我的类似,我被告知必须使用合适的格式对数据进行解压缩。

Reading this website , it seem that the data in the packets are all float types in C so I modified my original code in Python. 阅读此网站 ,似乎数据包中的数据都是C语言中的浮点类型,因此我用Python修改了原始代码。

import socket
import struct

UDP_IP = "127.0.0.1"
UDP_PORT = 20777

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024)
    output = struct.unpack('f', data)
    print ("Message:", output)

Unpack is given the format 'f' to assign the format of the incoming data based on information from this page: docs.python.org/3.2/library/struct.html#format-characters 根据此页面上的信息,为拆包指定格式“ f”以分配传入数据的格式: docs.python.org/3.2/library/struct.html#format-characters

This now produces the error: 现在会产生错误:

struct.error: unpack requires a bytes object of length 4

I believe now that I am at a dead end and I do not know how to proceed. 我相信现在我已经走到了尽头,我也不知道该怎么办。

Please help. 请帮忙。

As the error message says: struct.error: unpack requires a bytes object of length 4 , you need to pass 4 bytes at a time. 如错误消息所述: struct.error: unpack requires a bytes object of length 4 ,您需要一次传递4个字节。

Modify your code to send 4 bytes a time 修改您的代码以一次发送4个字节

while True:
    data, addr = sock.recvfrom(1024)
    # data is 1024 length bytearray, break into 4 bytes and get the output
    output = struct.unpack('f', data[0:4]) # 4 bytes at a time
    print ("Message:", output)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM