def main():
    with system.System() as my_system:
        database_alias = 'custom_database'
        database_filepath = os.path.join(os.path.dirname(__file__),
                                         'databases\custom_database.dbc')
        default_baud_rate = 500000
        my_system.databases.add_alias(database_alias, database_filepath,
                                      default_baud_rate)

    database_name = 'custom_database'
    cluster_name = 'CAN_Cluster'
    output_frame = 'CANEventFrame1'
    interface = 'CAN1'

    with nixnet.FrameOutQueuedSession(interface, database_name, cluster_name,
                                      output_frame) as output_session:
        terminated_cable = six.moves.input(
            'Are you using a terminated cable (Y or N)? ')
        if terminated_cable.lower() == "y":
            output_session.intf.can_term = constants.CanTerm.OFF
        elif terminated_cable.lower() == "n":
            output_session.intf.can_term = constants.CanTerm.ON
        else:
            print("Unrecognised input ({}), assuming 'n'".format(
                terminated_cable))
            output_session.intf.can_term = constants.CanTerm.ON

        user_value = six.moves.input('Enter payload [int, int]: ')
        try:
            payload_list = [int(x.strip()) for x in user_value.split(",")]
        except ValueError:
            payload_list = [2, 4, 8, 16]
            print('Unrecognized input ({}). Setting data buffer to {}'.format(
                user_value, payload_list))

        id = types.CanIdentifier(0)
        payload = bytearray(payload_list)
        frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload)

        print("Writing CAN frames using {} alias:".format(database_name))

        i = 0
        while i < 3:
            for index, byte in enumerate(payload):
                payload[index] = byte + i

            frame.payload = payload
            output_session.frames.write([frame])
            print('Sent frame with ID: {} payload: {}'.format(
                frame.identifier, list(frame.payload)))
            i += 1

    with system.System() as my_system:
        del my_system.databases[database_name]
Пример #2
0
    def send(self, msg, timeout=None):
        """
        Send a message using NI-XNET.

        :param can.Message msg:
            Message to send

        :param float timeout:
            Max time to wait for the device to be ready in seconds, None if time is infinite

        :raises can.exceptions.CanOperationError:
            If writing to transmit buffer fails.
            It does not wait for message to be ACKed currently.
        """
        if timeout is None:
            timeout = constants.TIMEOUT_INFINITE

        if msg.is_remote_frame:
            type_message = constants.FrameType.CAN_REMOTE
        elif msg.is_error_frame:
            type_message = constants.FrameType.CAN_BUS_ERROR
        elif msg.is_fd:
            if msg.bitrate_switch:
                type_message = constants.FrameType.CANFDBRS_DATA
            else:
                type_message = constants.FrameType.CANFD_DATA
        else:
            type_message = constants.FrameType.CAN_DATA

        can_frame = types.CanFrame(
            types.CanIdentifier(msg.arbitration_id, msg.is_extended_id),
            type=type_message,
            payload=msg.data,
        )

        try:
            self.__session_send.frames.write([can_frame], timeout)
        except errors.XnetError as error:
            raise CanOperationError(f"{error.args[0]} ({error.error_type})",
                                    error.error_code) from None
Пример #3
0
def test_can_identifier_extended_overflow():
    with pytest.raises(errors.XnetError):
        int(types.CanIdentifier(0xFFFFFFFF, True))
Пример #4
0
def test_can_identifier_overflow():
    valid_extended_identifier = 0xFFFF
    int(types.CanIdentifier(valid_extended_identifier, extended=True))
    with pytest.raises(errors.XnetError):
        int(types.CanIdentifier(valid_extended_identifier))
Пример #5
0
def test_can_identifier_equality():
    assert types.CanIdentifier(130) == types.CanIdentifier(130)
    assert types.CanIdentifier(130, True) == types.CanIdentifier(130, True)
    assert not (types.CanIdentifier(130) == types.CanIdentifier(13))
    assert not (types.CanIdentifier(130) == types.CanIdentifier(130, True))
    assert not (types.CanIdentifier(130) == "<invalid>")

    assert not (types.CanIdentifier(130) != types.CanIdentifier(130))
    assert not (types.CanIdentifier(130, True) != types.CanIdentifier(
        130, True))
    assert types.CanIdentifier(130) != types.CanIdentifier(13)
    assert types.CanIdentifier(130) != types.CanIdentifier(130, True)
    assert types.CanIdentifier(130) != "<invalid>"
def main():
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    input_frame = 'CANEventFrame1'
    output_frame = 'CANEventFrame1'
    interface1 = 'CAN1'
    interface2 = 'CAN2'

    with nixnet.FrameInQueuedSession(interface1, database_name, cluster_name,
                                     input_frame) as input_session:
        with nixnet.FrameOutQueuedSession(interface2, database_name,
                                          cluster_name,
                                          output_frame) as output_session:
            terminated_cable = six.moves.input(
                'Are you using a terminated cable (Y or N)? ')
            if terminated_cable.lower() == "y":
                input_session.intf.can_term = constants.CanTerm.ON
                output_session.intf.can_term = constants.CanTerm.OFF
            elif terminated_cable.lower() == "n":
                input_session.intf.can_term = constants.CanTerm.ON
                output_session.intf.can_term = constants.CanTerm.ON
            else:
                print("Unrecognised input ({}), assuming 'n'".format(
                    terminated_cable))
                input_session.intf.can_term = constants.CanTerm.ON
                output_session.intf.can_term = constants.CanTerm.ON

            # Start the input session manually to make sure that the first
            # frame value sent before the initial read will be received.
            input_session.start()

            user_value = six.moves.input('Enter payload [int, int]: ')
            try:
                payload_list = [int(x.strip()) for x in user_value.split(",")]
            except ValueError:
                payload_list = [2, 4, 8, 16]
                print('Unrecognized input ({}). Setting data buffer to {}'.
                      format(user_value, payload_list))

            id = types.CanIdentifier(0)
            payload = bytearray(payload_list)
            frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload)

            i = 0
            while True:
                for index, byte in enumerate(payload):
                    payload[index] = byte + i

                frame.payload = payload
                output_session.frames.write([frame])
                print('Sent frame with ID {} payload: {}'.format(id, payload))

                # Wait 1 s and then read the received values.
                # They should be the same as the ones sent.
                time.sleep(1)

                count = 1
                frames = input_session.frames.read(count)
                for frame in frames:
                    print('Received frame: {}'.format(frame))
                    print('    payload={}'.format(
                        list(six.iterbytes(frame.payload))))

                i += 1
                if max(payload) + i > 0xFF:
                    i = 0

                inp = six.moves.input('Hit enter to continue (q to quit): ')
                if inp.lower() == 'q':
                    break

            print('Data acquisition stopped.')