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
评论列表
文章目录