【问题标题】:Python2 & 3: embedded board, read USB dataPython2 & 3:嵌入式板卡,读取USB数据
【发布时间】:2015-05-15 21:05:22
【问题描述】:

我有一块嵌入式板 (ATMEL micro),可将数据发送到 USB 端口。

现在,如果我尝试用简单的“打印”读取这些数据,结果是:

第一组数据(红色)在它们“稳定”之前不必去(即 5 个元素的列表:绿色数据)!

怎么办?

这是我的第一个简单示例:

data = []
while len(data) != 5:
    ser = serial.Serial(strPort, 115200)
    line=ser.readline()
    ...???...

...然后呢?有什么想法吗?

【问题讨论】:

    标签: python serial-port embedded usb


    【解决方案1】:

    只需拆分输入行。

    data = []
    ser = serial.Serial(strPort, 115200)
    while len(data) != 5:
        data = ser.readline().decode('ascii').split('\t')
    

    【讨论】:

    • Python 2 工作正常,但 Python 3 我有: data=ser.readline().split('\t') TypeError: Type str does not support the buffer API 如何解决这个问题?
    • 我使用了这个代码:data[] ser=serial.Serial(strPort, 9600, timeout=1) raw_data=ser.read(9999) if len(raw_data) > 0: packet="{0}".format(raw_data.decode('ISO8859.15')) data=raw_data.split("#"),它工作正常。有没有“更好的pythonic方式”?
    【解决方案2】:

    您能否将阅读内容过滤为:

    line=ser.readline()
    if line[0,5] == "b' -"
        #Process the data
    

    您要阅读的所有行是否都以相同的“b” - 字符串开头?

    【讨论】:

    • 是的,但是我有这个错误: if line[0,5] == "b' -": TypeError: string indices must be integers, not元组
    • P.S.:对不起,我不明白怎么写!
    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多