def setUp(self): self.dev = LoopbackDev() self.disp = Dispatcher(self.dev) self.recv_queue = Queue() self.disp.add_receiver(self.recv_queue) # set up an isotp interface that sends and receives the same arb id self.sender = IsotpInterface(self.disp, 0, 1) self.receiver = IsotpInterface(self.disp, 1, 0) self.disp.start()
def initCAN_UDSInterface(candevice, tx_id, rx_id, extended_id=False, timeout=2): ''' CAN Hardware für Steuergerät tx_id aktivieren''' dev = socketcan.SocketCanDev(candevice) disp = Dispatcher(dev, single_process=False) uds_i = UDSInterface(disp, tx_id, rx_id, extended_id=extended_id, functional_timeout=timeout) disp.start() return disp, uds_i
def read_file(filename, req_arb_id, resp_arb_id): lp = LogPlayer(filename, realtime=False) disp = Dispatcher(lp) uds_req = UDSInterface(disp, 0, req_arb_id) uds_resp = UDSInterface(disp, 0, resp_arb_id) disp.start() session = [] while True: try: req = uds_req.decode_request() if req is None: break session.append(req) resp = None # wait until we get a response # this will return None if there is a response pending while resp is None: resp = uds_resp.decode_response() session.append(resp) except NegativeResponseException as e: session.append(e) disp.stop() return session
class IsotpTest(unittest.TestCase): def setUp(self): self.dev = LoopbackDev() self.disp = Dispatcher(self.dev) self.recv_queue = Queue() self.disp.add_receiver(self.recv_queue) # set up an isotp interface that sends and receives the same arb id self.sender = IsotpInterface(self.disp, 0, 1) self.receiver = IsotpInterface(self.disp, 1, 0) self.disp.start() def tearDown(self): self.disp.stop() def test_single_frame_loopback(self): """ Test ISOTP transmission and reception of a single frame message """ payload = [0xDE, 0xAD, 0xBE, 0xEF] self.sender.send(payload) resp = self.receiver.recv() self.assertEqual(payload, resp) def test_multi_frame_loopback(self): """ Test ISOTP transmission and reception of a multi-frame message """ payload = [0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF] * 50 # we need to run this in a thread so that the flow control frame is # sent and received, as IsotpInterfaces block tx_thread = threading.Thread(target=self.sender.send, args=(payload, )) tx_thread.start() # we'll receive data in this thread resp = self.receiver.recv() # wait for the transmitting thread to finish, then verify the result tx_thread.join() self.assertEqual(payload, resp) def test_tx_too_long(self): """ Ensure transmission of data that is too long raises exception """ with self.assertRaises(ValueError): self.sender.send([0xFF] * 4096) def test_padding(self): """ Test padding of frames sent using ISOTP """ payload = [0xDE, 0xAD, 0xBE, 0xEF] self.sender.padding_value = 0x55 self.sender.send(payload) resp = self.recv_queue.get() self.assertEqual(resp.data, [0x04, 0xDE, 0xAD, 0xBE, 0xEF, 0x55, 0x55, 0x55])
def read_file(self, filename, tx_id, rx_id): lp = LogPlayer(filename, realtime=False) disp = Dispatcher(lp) uds_req = UDSInterface(disp, 0, tx_id) uds_resp = UDSInterface(disp, 0, rx_id) disp.start() session = [] while True: try: req = uds_req.decode_request() if req is None: break if self.debug: print(req) session.append(req) resp = None # wait until we get a response # this will return None if there is a response pending tries = 0 while resp is None and tries < 5: resp = uds_resp.decode_response() tries = tries + 1 if self.debug: print(resp) session.append(resp) except NegativeResponseException as e: session.append(e) disp.stop() return session
class IsotpTest(unittest.TestCase): def setUp(self): self.dev = LoopbackDev() self.disp = Dispatcher(self.dev) self.recv_queue = Queue() self.disp.add_receiver(self.recv_queue) # set up an isotp interface that sends and receives the same arb id self.sender = IsotpInterface(self.disp, 0, 1) self.receiver = IsotpInterface(self.disp, 1, 0) self.disp.start() def tearDown(self): self.disp.stop() def test_single_frame_loopback(self): """ Test ISOTP transmission and reception of a single frame message """ payload = [0xDE, 0xAD, 0xBE, 0xEF] self.sender.send(payload) resp = self.receiver.recv() self.assertEqual(payload, resp) def test_multi_frame_loopback(self): """ Test ISOTP transmission and reception of a multi-frame message """ payload = [0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF]*50 # we need to run this in a thread so that the flow control frame is # sent and received, as IsotpInterfaces block tx_thread = threading.Thread(target=self.sender.send, args=(payload, )) tx_thread.start() # we'll receive data in this thread resp = self.receiver.recv() # wait for the transmitting thread to finish, then verify the result tx_thread.join() self.assertEqual(payload, resp) def test_multi_frame_loopback_with_bs(self): """ Test ISOTP transmission and reception of a multi-frame message with a defined blocksize """ payload = [0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF]*50 # we need to run this in a thread so that the flow control frame is # sent and received, as IsotpInterfaces block tx_thread = threading.Thread(target=self.sender.send, args=(payload, )) tx_thread.start() # we'll receive data in this thread resp = self.receiver.recv(bs=10, st_min=0) # wait for the transmitting thread to finish, then verify the result tx_thread.join() self.assertEqual(payload, resp) def test_tx_too_long(self): """ Ensure transmission of data that is too long raises exception """ with self.assertRaises(ValueError): self.sender.send([0xFF]*4096) def test_padding(self): """ Test padding of frames sent using ISOTP """ payload = [0xDE, 0xAD, 0xBE, 0xEF] self.sender.padding_value = 0x55 self.sender.send(payload) resp = self.recv_queue.get() self.assertEqual(resp.data, [0x04, 0xDE, 0xAD, 0xBE, 0xEF, 0x55, 0x55, 0x55])
import sys from pyvit.proto.obdii import ObdInterface from pyvit.hw.cantact import CantactDev from pyvit.dispatch import Dispatcher if len(sys.argv) != 2 and len(sys.argv) != 4: print("usage: %s CANtact_device [tx_arb_id] [rx_arb_id]" % sys.argv[0]) sys.exit(1) # set up a CANtact device dev = CantactDev(sys.argv[1]) dev.set_bitrate(500000) # create our dispatcher and OBD interface disp = Dispatcher(dev) # if we were given tx and rx ids, set them, otherwise use defaults if len(sys.argv) > 4: tx_arb_id = int(sys.argv[4], 0) rx_arb_id = int(sys.argv[5], 0) else: # functional address tx_arb_id = 0x7DF rx_arb_id = 0x7E8 obd = ObdInterface(disp, tx_arb_id, rx_arb_id) # setting debug to true will print all frames sent and received obd.debug = False disp.start()
from pyvit.proto.obdii import ObdInterface from pyvit.hw.cantact import CantactDev from pyvit.dispatch import Dispatcher if len(sys.argv) != 2 and len(sys.argv) != 4: print("usage: %s CANtact_device [tx_arb_id] [rx_arb_id]" % sys.argv[0]) sys.exit(1) # set up a CANtact device dev = CantactDev(sys.argv[1]) dev.set_bitrate(500000) # create our dispatcher and OBD interface disp = Dispatcher(dev) # if we were given tx and rx ids, set them, otherwise use defaults if len(sys.argv) > 4: tx_arb_id = int(sys.argv[4], 0) rx_arb_id = int(sys.argv[5], 0) else: # functional address tx_arb_id = 0x7DF # physical response ID of ECM ECU, responses from other ECUs will be ignored rx_arb_id = 0x7E8 obd = ObdInterface(disp, tx_arb_id, rx_arb_id) # setting debug to true will print all frames sent and received obd.debug = False
def setUp(self): dev = LoopbackDev() self.disp = Dispatcher(dev)
class DispatchTest(unittest.TestCase): def setUp(self): dev = LoopbackDev() self.disp = Dispatcher(dev) def test_dispatcher_single(self): rx = Queue() self.disp.add_receiver(rx) self.disp.start() f = can.Frame(0x123) self.disp.send(f) f2 = rx.get() self.disp.stop() self.assertEqual(f, f2) def test_dispatcher_multiple(self): rx = Queue() self.disp.add_receiver(rx) tx1 = can.Frame(1, data=[1, 2, 3, 4, 5]) tx2 = can.Frame(2, data=[0xFF, 0xFF, 0xFF]) tx3 = can.Frame(3, data=[0xDE, 0xAD, 0xBE, 0xEF]) self.disp.start() self.disp.send(tx1) self.disp.send(tx2) self.disp.send(tx3) rx1 = rx.get() rx2 = rx.get() rx3 = rx.get() self.disp.stop() self.assertEqual(rx1, tx1) self.assertEqual(rx2, tx2) self.assertEqual(rx3, tx3)