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()
Exemple #2
0
    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()