【问题标题】:How to return SerialException error message, Object Serial?如何返回 SerialException 错误消息,对象序列?
【发布时间】:2019-11-19 09:18:36
【问题描述】:

如何返回 SerialException 输出的错误信息?在这里,我尝试在已经打开的端口中打开端口。我想得到发生的错误。如果 SerialException 将返回 访问被拒绝

无法打开端口 'COM38': PermissionError(13, 'Access is denied.', 无,5)

import serial
from serial import SerialException

def open:
  try:
    serial.Serial('COM38', 9600)

    return True
  except SerialException:
    #How to return error message that serial exception occurs?
    return Raise

print(open())

【问题讨论】:

    标签: python serialization serial-port pyserial


    【解决方案1】:

    希望帮助。

    import serial
    from serial import SerialException
    
    def open:
      try:
        serial.Serial('COM38', 9600)
    
        return True
      except PermissionError as e:
          message = str(e)
          raise SerialException(message)
      except SerialException as ex:
          print(ex)
    
    print(open())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-02
      • 2018-02-18
      • 2012-03-20
      • 2017-02-27
      • 2012-09-25
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      相关资源
      最近更新 更多