Beispiel #1
0
    def __init__(self,
                 baudrate='autobaud',
                 timeout=.01,
                 automon=True,
                 reaction_poll_delay=.01):
        '''Discovers hardware port name.'''
        self.interface = pk.Interface('can', timeout)

        self.poll_delay = reaction_poll_delay
        self.baudrate = None
        self.pre_data = []
        self.data = []
        self.isopen = False
        self.bgmon = None
        self.registry = {}

        if baudrate == 'autobaud':
            self.autobaud(None)
        elif isinstance(baudrate, int):
            self.autobaud(baudrate)

        if automon:
            self.open()
            self.bgmonitor()
        else:
            self.data = self.pre_data
Beispiel #2
0
    def autobaud(self, baudrate: int) -> str:
        '''Autodetect the bus baudrate

        If the passed argument 'baudrate' is None, the baudrate will be autodetected,
        otherwise, the bus baudrate will be set to the passed value.

        When attempting to auto-detect baudrate, the system will time-out after 3.5 seconds.

        Args:
            baudrate: The baudrate of the bus in thousands. Set to 'None' to autodetect

        Returns:
            The discovered or set baudrate
        '''
        set_rate = None
        with pk.Interface('terminal', timeout=.001) as term:
            if not baudrate:
                term.cwrite('can-autobaud')

                start = time()
                elapsed = 0

                set_rate = term.cread()[0].strip('\n\r')
                while not set_rate and elapsed < 3.5:
                    set_rate = term.cread()[0].strip('\n\r')
                    elapsed = time() - start
            else:
                term.cwrite('set can-baudrate ' + str(baudrate))
                set_rate = str(baudrate)

        temp = re.search(r'\s(?P<baud>[\d]+)k', set_rate)
        self.baudrate = temp.groupdict()['baud'] if temp else None

        return self.baudrate
Beispiel #3
0
def test_multiple_release():
    ''' Ensure that is allowed to close a closed port '''
    term = pk.Interface('terminal')
    term.claim()

    term.release()
    term.release()
    term.release()
Beispiel #4
0
def test_port_claim_release():
    ''' Confirm that ports can be claimed and released by pyserial '''
    for i in range(0, 5):
        term = pk.Interface('terminal')
        can = pk.Interface('can')

        term.claim()
        can.claim()

        assert term.ser.is_open
        assert can.ser.is_open

        term.release()
        can.release()

        assert not term.ser
        assert not can.ser
Beispiel #5
0
def test_read_write():
    ''' Confirm that we can write to both ports '''

    term = pk.Interface('terminal')
    term.claim()

    can = pk.Interface('can')
    can.claim()

    term.cwrite('version')
    can.cwrite('std 123 8 1122334455667788 data')

    term_out = term.cread()
    can_out = can.cread()

    match = re.match(r"<.+v(\d\.){3}\d.+>", term_out[0])
    assert match
    assert '123 1122334455667788' in can_out[0]
Beispiel #6
0
def test_unclaimed_read_write():
    ''' Check that correct errors are thrown when a user tries a closed port '''
    from pytest import raises
    term = pk.Interface('terminal')

    with raises(ConnectionError):
        term.cwrite('version')

    with raises(ConnectionError):
        term.cread()
Beispiel #7
0
def test_context_manager():
    ''' Check that we can use the interface as a context manager '''

    out = ''
    with pk.Interface('terminal') as term:
        term.cwrite('version')
        out = term.cread()

    match = re.match(r"<.+v(\d\.){3}\d.+>", out[0])
    assert match
Beispiel #8
0
def test_multiple_argument_writes():
    ''' Check that buffer is cleared in such a way that multi-argument writes always succeed '''
    test_values = ['700', '710', '720', '730', '740', '750', '800']
    out_values = []
    with pk.Interface('terminal') as term:
        for value in test_values:
            term.cwrite('set can-baudrate {}'.format(value))
            out_values.append(term.cread()[0])

    for value in out_values:
        assert 'Error' not in value
Beispiel #9
0
    def __init__(self, timeout=.01, automon=True):
        '''Discovers hardware port name. '''
        self.interface = pk.Interface('terminal', timeout)

        self.pre_data = []
        self.data = []
        self.isopen = False

        self.info = {
            'version':
                {
                    'value': None,
                    'desc': 'Firmware version number.'
                },
            'build':
                {
                    'value': None,
                    'desc': 'Firmware build date.'
                },
            'configuration':
                {
                    'value': None,
                    'desc': 'Current user configuration.'
                },
            'ignition-sense':
                {
                    'value': None,
                    'desc': 'If ignition sensing is enabled or disabled.'
                },
            'startup-timer':
                {
                    'value': None,
                    'desc': 'Time, in seconds, until boot after ignition on.'
                },
            'shutdown-timer':
                {
                    'value': None,
                    'desc': 'Time, in seconds, until soft power off after ignition off.'
                },
            'hard-off-timer':
                {
                    'value': None,
                    'desc': 'Time, in seconds, until hard power off after igntion off.'
                },
            'auto-power-on':
                {
                    'value': None,
                    'desc': 'Force device to power on when first connected to AC power.'
                },
            'shutdown-voltage':
                {
                    'value': None,
                    'desc': 'Voltage when device will power off to avoid battery discharge.'
                },
            'hotplug':
                {
                    'value': None,
                    'desc': 'Set if the display port outputs are hotpluggable.'
                },
            'can-baudrate':
                {
                    'value': None,
                    'desc': 'Current CAN bus baudrate.'
                },
            'dio-power-switch':
                {
                    'value': None,
                    'desc': 'Have digital inputs act as a remote power switch when device is off.'
                },
            'boot-config':
                {
                    'value': None,
                    'desc': 'If the current configuration will be loaded at boot.'
                },
            'voltage':
                {
                    'value': None,
                    'desc': 'The last-read system input voltage'
                }
        }

        self.bgmon = None
        self.registry = {}

        if automon:
            self.open()
            self.bgmonitor()
        else:
            self.data = self.pre_data