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 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 set_packet_types(self, value): '''value bit 1 - slope, bit 2 - angular rate, bit 3 - accelerometer''' arbitration_id = j1939.ArbitrationID(priority=6, pgn=65366, source_address=128) m = j1939.PDU(arbitration_id=arbitration_id, data=[128, value]) self.bus.send(m)
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 testSendingJ1939Message(self): # this version puts the message through the network arbitration_id = j1939.ArbitrationID(pgn=65259) m = j1939.PDU(arbitration_id=arbitration_id, data=bytearray(b'abc123')) logging.debug("writing message: {}".format(m)) self.bus.send(m) logging.debug("message written")
def set_lpf_filter(self, gyro_hz, accel_hz): '''value bit 1 - slope, bit 2 - angular rate, bit 3 - accelerometer''' arbitration_id = j1939.ArbitrationID(priority=6, pgn=65367, source_address=128) m = j1939.PDU(arbitration_id=arbitration_id, data=[128, gyro_hz, accel_hz]) self.bus.send(m)
def mod_bank0(self): # algorithm reset, save config, hardware bits, software bits, status, packet rate, packet type, digital filter] # original array # pdus_array = [ 80, 81, 82, 83, 84, 85, 86, 87] pdus_array = [100, 101, 102, 103, 104, 105, 106, 107] arbitration_id = j1939.ArbitrationID(priority=6, pgn=65520) m = j1939.PDU(arbitration_id=arbitration_id, data=pdus_array) self.bus.send(m)
def set_orientation(self, value): arbitration_id = j1939.ArbitrationID(priority=6, pgn=65368) value_msb = 0xFF00 & value value_msb = value_msb >> 8 value_lsb = 0x00FF & value m = j1939.PDU(arbitration_id=arbitration_id, data=[128, value_msb, value_lsb]) print(m) self.bus.send(m)
def get_config(self, pgn): pdu_format = 0xFF00 & pgn pdu_format = pdu_format >> 8 pdu_specific = 0x00FF & pgn print(pdu_format) print(pdu_specific) arbitration_id = j1939.ArbitrationID(priority=6, pgn=60159) m = j1939.PDU(arbitration_id=arbitration_id, data=[24, pdu_format, pdu_specific]) self.bus.send(m)
def testTooLongAMessage(self): data = bytes([random.randrange(0, 2**8 - 1) for b in range(1786)]) with self.assertRaises(AssertionError): j1939.PDU(arbitration_id=self.arbitration_id, data=data)
def testMessageWithList(self): data = [1, 4, 123, 35] m = j1939.PDU(arbitration_id=self.arbitration_id, data=data) self.assert_correct_message_data(m, data)
def testMessageWithByteArray(self): data_as_bytearray = bytearray(b'abc123') m = j1939.PDU(arbitration_id=self.arbitration_id, data=data_as_bytearray) self.assert_correct_message_data(m, data_as_bytearray)
def testMessageWithBytes(self): data_as_bytes = b'abc123' m = j1939.PDU(arbitration_id=self.arbitration_id, data=data_as_bytes) self.assert_correct_message_data(m, data_as_bytes)
def testCreateMessage(self): self.m = j1939.PDU(arbitration_id=self.arbitration_id)
def generate_long_messages(arbitration_id): for l in list(range(10)) + [100, 1000, 1784, 1785]: data = bytearray([random.randrange(0, 2**8 - 1) for b in range(l)]) m = j1939.PDU(arbitration_id=arbitration_id, data=data) yield m, data
def set_odr_mod(self, odr_setting): #arbitration_id = j1939.ArbitrationID(priority=6, pgn=65365, source_address=128) arbitration_id = j1939.ArbitrationID(priority=6, pgn=65385) m = j1939.PDU(arbitration_id=arbitration_id, data=[128, odr_setting]) self.bus.send(m)
def save_settings(self): arbitration_id = j1939.ArbitrationID(priority=6, pgn=65361) m = j1939.PDU(arbitration_id=arbitration_id, data=[0, 128]) print(m) self.bus.send(m)