예제 #1
0
    async def _ws_connect(self):
        import websockets

        try:
            try:
                self._ws = await websockets.connect(self._url, ping_interval=None)
            except websockets.exceptions.InvalidMessage:
                # try once more
                self._ws = await websockets.connect(self._url, ping_interval=None)
        except OSError as e:
            # print("\nCould not connect:", e, file=sys.stderr)
            raise ConnectionFailedException(str(e))
        debug("GOT WS", self._ws)

        # read password prompt and send password
        read_chars = ""
        while read_chars != "Password: "******"prelude", read_chars)
            ch = await self._ws.recv()
            debug("GOT", ch)
            read_chars += ch

        debug("sending password")
        await self._ws.send(self._password + "\n")
        debug("sent password")
예제 #2
0
    def __init__(self, port, baudrate, dtr=None, rts=None, skip_reader=False):

        import serial
        from serial.serialutil import SerialException

        super().__init__()

        try:
            self._serial = serial.Serial(port=None,
                                         baudrate=baudrate,
                                         timeout=None,
                                         exclusive=True)
            # Tweaking dtr and rts was proposed by
            # https://github.com/thonny/thonny/pull/1187
            # but in some cases it messes up communication.
            # At the same time, in some cases it is required
            # https://github.com/thonny/thonny/issues/1462
            if dtr is not None:
                logger.debug("Setting DTR to %s", dtr)
                self._serial.dtr = dtr
            if rts is not None:
                logger.debug("Setting RTS to %s", rts)
                self._serial.rts = rts

            self._serial.port = port
            logger.debug("Opening serial port %s", port)
            self._serial.open()
        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 or "Could not exclusively lock" in message:
                message += "\n\n" + dedent("""\
                If you have serial connection to the device from another program, then disconnect it there first."""
                                           )

            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()
예제 #3
0
 def _resolve_executable(self, executable):
     result = self._which(executable)
     if result:
         return result
     else:
         msg = "Executable '%s' not found. Please check your configuration!" % executable
         if not executable.startswith("/"):
             msg += " You may need to provide its absolute path."
         raise ConnectionFailedException(msg)
예제 #4
0
    def __init__(self, port, baudrate, skip_reader=False):

        import serial
        from serial.serialutil import SerialException

        super().__init__()

        # https://forum.micropython.org/viewtopic.php?f=15&t=4896&p=28132
        # https://forum.micropython.org/viewtopic.php?f=15&t=3698
        self._write_block_size = 30
        self._write_block_delay = 0.01

        try:
            self._serial = serial.Serial(port, baudrate=baudrate, timeout=None)
            # https://github.com/thonny/thonny/pull/1187
            self._serial.dtr = 0
            self._serial.rts = 0
        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()