如何使用python套接字将数据从驾驶模拟器PC发送到笔记本电脑?
目标
驾驶模拟器 PC 生成实时数据(1/60 秒),我可以通过socket
获取这些数据。我通过以太网线将笔记本电脑连接到这台电脑。现在,我想将数据从 PC 发送到笔记本电脑,或者通过 socket
直接在笔记本电脑中获取数据。以下是设置:
routeTable.txt 指定如何从模拟器获取数据
以下文本文件位于 PC 上的 DrivingSimulator/bin.x64
中。我用星号 *
来突出显示 IP 地址和端口
rem This is a test for vehicle position
rem name type size
var
FrameNum i 1
VDS_Chassis_CG_Position d 3
end
rem Here we format each variable for output, we can change the size of the output here if we want
rem DevID type size is Expr/Name Name or Expression
device
1 i 1 n "FrameNum"
2 d 3 n "VDS_Chassis_CG_Position"
end
rem These are the input devices
rem id CellName offset(byte) size(Byte)
input
end
rem ID **IP address** **In port** **Out port** Packed? List of devices
route
1 **127.0.0.1** **9000** **9001** 1 2
end
UDP.py 包含通过 socket
在 python 中获取 PC 中数据的代码
以下 python 脚本在 PC 上运行时,成功打印出python控制台中的数据:
import socket
import struct
UDP_IP = "127.0.0.1"
UDP_PORT = 9000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
fields = struct.unpack_from('=ddd', data)
print(fields[0],fields[1],fields[2])
如何在连接的笔记本电脑上获取相同的数据?
我尝试过:
笔记本电脑通过以太网电缆连接到电脑。我尝试在笔记本电脑上运行 UDP.py 脚本,它没有在终端上打印出任何内容,但也没有抛出任何错误。然后我想我应该使用笔记本电脑的IP。因此,我尝试通过更改 UDP.py
中的 UDP_IP
和 routeTable.txt
中的 IP 地址
笔记本电脑的 IP,即 192.168.x.yy
。相同的行为:没有打印输出,但也没有错误。
问:
如何获取笔记本电脑上的数据?
Goal
A driving simulator PC generates real-time data (1/60th of a second) that I can get via socket
. I connected a laptop to this PC via ethernet cable. Now, I want to send the data from PC to the laptop OR get the data directly in laptop via socket
. Following is the setup:
routeTable.txt specifies how to get data from the simulator
Following text file is located at DrivingSimulator/bin.x64
on PC. I have put the stars *
to highlight the IP address and ports
rem This is a test for vehicle position
rem name type size
var
FrameNum i 1
VDS_Chassis_CG_Position d 3
end
rem Here we format each variable for output, we can change the size of the output here if we want
rem DevID type size is Expr/Name Name or Expression
device
1 i 1 n "FrameNum"
2 d 3 n "VDS_Chassis_CG_Position"
end
rem These are the input devices
rem id CellName offset(byte) size(Byte)
input
end
rem ID **IP address** **In port** **Out port** Packed? List of devices
route
1 **127.0.0.1** **9000** **9001** 1 2
end
UDP.py contains the code to get data in PC in python via socket
The following python script when run on PC, prints out the data in python console successfully:
import socket
import struct
UDP_IP = "127.0.0.1"
UDP_PORT = 9000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
fields = struct.unpack_from('=ddd', data)
print(fields[0],fields[1],fields[2])
How do I get the same data on the attached laptop?
What I tried:
The laptop is connected to PC via an ethernet cable. I tried running the UDP.py
script on the laptop, it did not print out anything on the terminal, but also did not throw any error. Then I thought I should use the IP of the laptop. So, I tried that by changing the UDP_IP
in UDP.py
and the IP address
in routeTable.txt
with the laptop's IP i.e., 192.168.x.yy
. Same behavior: no print out but also no error.
Question:
How do I get the data on the laptop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论