Python serial.serialutil() Examples

The following are 2 code examples of serial.serialutil(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module serial , or try the search function .
Example #1
Source File: upload.py    From pros-cli2 with Mozilla Public License 2.0 5 votes vote down vote up
def dump_cortex(port, file, verbose=False):
    if not os.path.isfile(file):
        click.echo('Failed to download... file does not exist')
        return False
    port = prosflasher.ports.create_serial(port)
    if not port:
        click.echo('Failed to download: port not found')
        return
    try:
        reset_cortex(port)
        sys_info = ask_sys_info(port)
        if sys_info is None:
            click.echo('Failed to get system info... Try again', err=True)
            click.get_current_context().abort()
            sys.exit(1)
        click.echo(repr(sys_info))
        stop_user_code(port)
        if sys_info.connection_type == ConnectionType.serial_vexnet2:
            # need to send to download channel
            if not send_to_download_channel(port):
                return False
        if not expose_bootloader(port):
            return False
        if not prosflasher.bootloader.prepare_bootloader(port):
            return False
        if not prosflasher.bootloader.erase_flash(port):
            return False

        with open(file, 'wb') as f:
            address = 0x08000000
            data = prosflasher.bootloader.read_memory(port, address, 256)
            while len(data) > 0:
                f.write(data)
                address += 0x100

    except serial.serialutil.SerialException as e:
        click.echo('Failed to download code! ' + str(e))
    finally:
        port.close()
    click.echo("Download complete!")
    pass 
Example #2
Source File: serial_connection.py    From thonny with MIT License 5 votes vote down vote up
def __init__(self, port, baudrate, skip_reader=False):
        import serial
        from serial.serialutil import SerialException

        super().__init__()

        try:
            self._serial = serial.Serial(port, baudrate=baudrate, timeout=None)
        except SerialException as error:
            err_str = str(error)
            if "FileNotFoundError" in err_str:
                err_str = "port not found"
            message = "Unable to connect to " + port + ": " + err_str

            # TODO: check if these error codes also apply to Linux and Mac
            if error.errno == 13 and platform.system() == "Linux":
                # TODO: check if user already has this group
                message += "\n\n" + dedent(
                    """\
                Try adding yourself to the 'dialout' group:
                > sudo usermod -a -G dialout <username>
                (NB! This needs to be followed by reboot or logging out and logging in again!)"""
                )

            elif "PermissionError" in message:
                message += "\n\n" + dedent(
                    """\
                If you have serial connection to the device from another program,
                then disconnect it there."""
                )

            elif error.errno == 16:
                message += "\n\n" + "Try restarting the device."

            raise ConnectionFailedException(message)

        if skip_reader:
            self._reading_thread = None
        else:
            self._reading_thread = threading.Thread(target=self._listen_serial, daemon=True)
            self._reading_thread.start()