【问题标题】:How to pass parsed arguments to PySerial Api如何将解析的参数传递给 PySerial Api
【发布时间】:2021-11-12 11:54:39
【问题描述】:

我收集所有数据以通过用户输入参数化串行连接。 但是 Pyserial API 不会直接将值“EIGHTBITS”作为参数,但您必须调用 serial.EIGHTBITS。 有办法克服吗?

ap = argparse.ArgumentParser()
ap.add_argument("-p", "--Port", required=True,
                          help="specify Port Name")
ap.add_argument("-b", "--Baud", type=int,required=True,
                          help="Specify Baud rate")
ap.add_argument("-t", "--timeout", type=float,required=True, 
                          help="Connexion Timeout")
ap.add_argument("-d", "--ByteSize",default="EIGHTBITS",choices=['FIVEBITS','SIXBITS','SEVENBITS','EIGHTBITS'],
                          help="Specify the Data bits number")
ap.add_argument("-s", "--StopBits", default="STOPBITS_ONE",choices=['STOPBITS_ONE','STOPBITS_ONE_POINT_FIVE','STOPBITS_TWO'],
                          help="StopBits Value")
ap.add_argument("-pa", "--parity", default="PARITY_NONE",choices=['PARITY_NONE','PARITY_EVEN','PARITY_ODD','PARITY_MARK','PARITY_SPACE'],
                          help="Set up the Parity Bit")
ap.add_argument("-f", "--FlowControl", type=bool, default=False,
                          help="StopBits Value")
args = vars(ap.parse_args())

Port = args["Port"]
BaudRate = args["Baud"]
Timeout = args["timeout"]
ByteSize = args["ByteSize"]
Stop = args["StopBits"]
Parity = args["parity"]
FlowC=args["FlowControl"]

ser=serial.Serial(Port,BaudRate,bytesize=ByteSize,parity=Parity,stopbits=Stop,timeout=Timeout,xonxoff=FlowChart)

【问题讨论】:

    标签: python argparse pyserial


    【解决方案1】:

    使用 getattr 从串行模块中按名称(字符串)检索 serial.EIGHTBITS 等的值,例如。 “八位”。

    Parity = getattr(serial, args["parity"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-05
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2018-12-20
      • 2011-02-27
      相关资源
      最近更新 更多