我正在使用两个通过DB9母头到DB9母头电缆连接的CANtact工具包。我想从一个工具箱发送CAN消息,而必须在另一个工具箱中接收该消息。我使用了以下代码
#!/bin/env python
import socket
import struct
canformat = '<IB3x8s'
class CanBridge():
def __init__(self, interface_from, interface_to):
self.interface_from = interface_from
self.interface_to = interface_to
self.canSocket_to = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
self.canSocket_from = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
try:
self.canSocket_to.bind((self.interface_to,))
self.canSocket_from.bind((self.interface_from,))
except OSError:
print("Could not bind to interfaces")
#put the sockets in blocking mode.
self.canSocket_to.settimeout(None)
self.canSocket_from.settimeout(None)
def run(self):
while True:
raw_bytes = self.canSocket_from.recv(512)
try:
self.canSocket_to.send(raw_bytes)
except OSError: #Buffer overflow usually from lack of connection.
pass
rawID,DLC,candata = struct.unpack(canformat,raw_bytes)
canID = rawID & 0x1FFFFFFF
candata_string = " ".join(["{:02X}".format(b) for b in candata])
print("{:08X} {}".format(canID, candata_string))
if __name__ == '__main__':
bridge = CanBridge('slcan1','slcan0')
bridge.run()
我收到以下错误
AttributeError:“ CanBridge”对象没有属性“ run”
如何解决此错误?还是有更好的代码可用? 我从以下网站获得了此代码 https://github.com/Heavy-Vehicle-Networking-At-U-Tulsa/can-man-in-the-middle