Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中通过UDP发送CAN帧_Python_Linux_Sockets_Udp_Can Bus - Fatal编程技术网

Python中通过UDP发送CAN帧

Python中通过UDP发送CAN帧,python,linux,sockets,udp,can-bus,Python,Linux,Sockets,Udp,Can Bus,我在两台Linux机器之间建立了UDP套接字连接,可以轻松地发送例如b“Hello,World!”。但现在我需要发送下面的罐装框架 from can import Message send_msg = Message(data=[1, 2, 3, 4, 5]) 因此,如果我打印send_msg,它会显示: Timestamp: 0.000000 ID: 00000000 X DLC: 5 01 02 03 04 05 我想把这个

我在两台Linux机器之间建立了UDP套接字连接,可以轻松地发送例如
b“Hello,World!”
。但现在我需要发送下面的罐装框架

from can import Message
send_msg = Message(data=[1, 2, 3, 4, 5])
因此,如果我打印
send_msg
,它会显示:

Timestamp:        0.000000    ID: 00000000    X                DLC:  5    01 02 03 04 05
我想把这个打印在接收端。我使用的发送端和接收端代码如下:

发送:

import socket

UDP_IP = "10.140.189.249"
UDP_PORT = 5005
from can import Message
send_msg = Message(data=[1, 2, 3, 4, 5])

print(send_msg)
MESSAGE = send_msg

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
在这里我知道
MESSAGE=send\u msg
是错误的

接收

import socket

UDP_IP = "0.0.0.0"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

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

请告知

由于UDP连接和CAN连接的物理层有很大不同,您无法通过UDP发送CAN帧。当然,可以发送CAN机架的有效载荷,并在接收端组装CAN消息:即在发送端:

sock.sendto(b“12345“, (UDP_IP, UDP_PORT))
在接受方:

msg = Message(data=bytearray(recv_msg))
最有可能的情况是,您不仅希望传输CAN机架的数据,还希望传输ID和其他字段

另一种可能是在发送端pickle消息对象,在接收端使用
pickle.dumps
pickle.loads


不能在UDP连接上模拟CAN总线的所有功能,如仲裁、错误帧等。

因此,对于pickle.dumps和pickle.loads,是否可以发送CAN ID?是的,这就是我的意思。只需通过UDP连接发送阵列。在接收器端,获取数据并在那里创建
can.Message
。当您需要超过CAN有效载荷时,您可以在接收器上创建
CAN.Message
pickle完整对象(不仅仅是有效载荷),通过UDP发送pickle数据,并在接收器上取消pickle,以便获得
CAN.Message
您可以执行
send_msg.data
来获得有效载荷。由于这已经是一个
字节数组
,因此没有必要对其进行pickle。您可以直接发送。只需将其转换为列表,例如,
列表(接收的数据)
我认为这更适合新问题没有细节的“不起作用”并不是一个有用答案的基础。一般来说,任何需要在另一端可用的东西都需要单独或作为数据包传输。