def testReceivingLongMessage(self): data = [1, 2, 3, 4, 5, 6, 7, 8] pgn = j1939.PGN( reserved_flag=True, pdu_specific=j1939.constants.DESTINATION_ADDRESS_GLOBAL) arbitration_id = j1939.ArbitrationID(pgn=pgn, source_address=0x01) m_out = j1939.PDU(arbitration_id=arbitration_id, data=data) otherbus = j1939.Bus(channel=can_interface) # The otherbus might miss messages if we send them before its receive # thread is running, so we wait here sleep(0.50) node = j1939.Node(otherbus, j1939.NodeName(0), [0x42, 0x01]) try: attempts = 0 while attempts < 3: m_in = otherbus.recv(timeout=2) if m_in is not None: break # send a long message self.bus.send(m_out) attempts += 1 self.assertIsNotNone(m_in, 'Should receive sent messages on J1939 Bus') self.assertIsInstance(m_in, j1939.PDU) self.assertListEqual(m_in.data, m_out.data) finally: otherbus.shutdown()
def testReceivingLongMessage(self): node = j1939.Node(self.bus, j1939.NodeName(0), [0x42, 0x01]) data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] pgn = j1939.PGN( reserved_flag=True, pdu_specific=j1939.constants.DESTINATION_ADDRESS_GLOBAL) arbitration_id = j1939.ArbitrationID(pgn=pgn, source_address=0x01) m_out = j1939.PDU(arbitration_id=arbitration_id, data=data) otherbus = j1939.Bus(channel=can_interface) attempts = 0 while attempts < 5: m_in = otherbus.recv(timeout=0.5) if m_in is not None: break # send a long message self.bus.send(m_out) attempts += 1 self.assertIsNotNone( m_in, 'Should receive messages on can bus when sending long message') self.assertIsInstance(m_in, j1939.PDU) self.assertListEqual(m_in.data, m_out.data) otherbus.shutdown()
def __init__(self): self.cnt = 0 ## close can0 os.system('sudo ifconfig can0 down') ## set bitrate of can0 os.system('sudo ip link set can0 type can bitrate 500000') ## open can0 os.system('sudo ifconfig can0 up') # os.system('sudo /sbin/ip link set can0 up type can bitrate 250000') ## show details can0 for debug. # os.system('sudo ip -details link show can0') if 0: ## set up CAN Bus of J1939 self.bus = j1939.Bus(channel='can0', bustype='socketcan_native') # set up Notifier self.notifier = can.Notifier(self.bus, [self.msg_handler]) else: # set up can interface. self.can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')# socketcan_native socketcan_ctypes ## set up Notifier self.notifier = can.Notifier(self.can0, [self.msg_handler]) self.can_parser = CANParser() self.lines = 0
def send_j1939(): bus = j1939.Bus(channel='vcan0') node1 = j1939.Node(bus, j1939.NodeName(0), [0x01]) node2 = j1939.Node(bus, j1939.NodeName(0), [0x42]) bus.j1939_notifier.listeners.append(node1) bus.j1939_notifier.listeners.append(node2) pgn = j1939.PGN(reserved_flag=True, pdu_specific=j1939.constants.DESTINATION_ADDRESS_GLOBAL) arbitration_id = j1939.ArbitrationID(pgn=pgn, source_address=0x01) msg = j1939.PDU(arbitration_id=arbitration_id, data=[0x10, 0x20, 0x30]) sleep(1) node1.start_address_claim() sleep(1) try: bus.send(msg) print("Message sent on {}".format(bus.channel_info)) except can.CanError: print("Message NOT sent") sleep(1) bus.flush_tx_buffer() bus.shutdown()
def __init__(self): self.accel_pkt_cnt = 0 self.gyro_pkt_cnt = 0 self.slope_pkt_cnt = 0 # set up Bus self.bus = j1939.Bus(channel='can0', bustype='socketcan_native') # set up Notifier self.notifier = can.Notifier(self.bus, [self.general_message])
def setUp(self): super(NetworkJ1939Test, self).setUp() self.bus = j1939.Bus(channel=can_interface) while self.bus.recv(timeout=0.1) is not None: pass
def testCreateBus(self): self.bus = j1939.Bus(channel=can_interface) self.bus.shutdown()
def setUp(self): super(NetworkJ1939Test, self).setUp() self.bus = j1939.Bus(channel=can_interface)
filters = [] if args.pgn is not None: print('Have to filter pgns: ', args.pgn) for pgn in args.pgn: if pgn.startswith('0x'): pgn = int(pgn[2:], base=16) filters.append({'pgn': int(pgn)}) if args.source is not None: for src in args.source: if src.startswith("0x"): src = int(src[2:], base=16) filters.append({"source": int(src)}) if args.filter is not None: filters = json.load(args.filter) #print("Loaded filters from file: ", filters) bus = j1939.Bus(channel=args.channel, bustype=args.interface, j1939_filters=filters) log_start_time = datetime.datetime.now() print('can.j1939 logger started on {}\n'.format(log_start_time)) try: for msg in bus: print(msg) except KeyboardInterrupt: bus.shutdown() print()
from can.interfaces.interface import * from can.protocols import j1939 filters = [] if args.pgn is not None: print('Have to filter pgns: ', args.pgn) for pgn in args.pgn: if pgn.startswith('0x'): pgn = int(pgn[2:], base=16) filters.append({'pgn': int(pgn)}) if args.source is not None: for src in args.source: if src.startswith("0x"): src = int(src[2:], base=16) filters.append({"source": int(src)}) if args.filter is not None: filters = json.load(args.filter) #print("Loaded filters from file: ", filters) bus = j1939.Bus(channel='can0', j1939_filters=filters) log_start_time = datetime.datetime.now() print('can.j1939 logger started on {}\n'.format(log_start_time)) try: for msg in bus: print(msg) except KeyboardInterrupt: bus.shutdown() print()