serial_gamepad.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:BiblioPixel2 作者: ManiacalLabs 项目源码 文件源码
def _connect(self):
        try:
            if(self.dev == ""):
                SerialGamePad.findSerialDevices(self._hardwareID)

                if len(SerialGamePad.foundDevices) > 0:
                    self.dev = SerialGamePad.foundDevices[0]
                    log.info("Using COM Port: %s", self.dev)

            try:
                self._com = serial.Serial(self.dev, timeout=5)
            except serial.SerialException as e:
                ports = SerialGamePad.findSerialDevices(self._hardwareID)
                error = "Invalid port specified. No COM ports available."
                if len(ports) > 0:
                    error = "Invalid port specified. Try using one of: \n" + \
                        "\n".join(ports)
                log.info(error)
                raise SerialPadError(error)

            packet = SerialGamePad._generateHeader(CMDTYPE.INIT, 0)
            self._com.write(packet)

            resp = self._com.read(1)
            if len(resp) == 0:
                SerialGamePad._comError()

            return ord(resp)

        except serial.SerialException as e:
            error = "Unable to connect to the device. Please check that it is connected and the correct port is selected."
            log.exception(e)
            log.error(error)
            raise e
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号