【问题标题】:Pyserial reading does not respect the number of bitesPyserial阅读不尊重咬的数量
【发布时间】:2018-02-05 15:24:10
【问题描述】:

我正在尝试读取串行端口。 问题是脚本有效,但读取命令似乎不尊重参数(要读取 2 个字节)。

脚本的典型输出(返回函数“readPosValue”中的ans变量)是:

print(currTime,readPosValue(serPort))
(1517909247.176, '0b11000010110111001110011')

它们显然超过 16 位。 使用的脚本:

import time
import struct
import binascii
import serial

ser = serial.Serial(
port='COM2',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS

)

def inficonAcquisition(serPort):  

        try:

        while True:
            position = readPosValue(serPort)

            currTime = time.time()

            print(currTime,position)


    except KeyboardInterrupt:
        serPort.close()

        exit()

def readPosValue(serPort):       
    ans = ''
    while serPort.inWaiting() > 0:
        ans += serPort.read(2)    
    return bin(int(binascii.hexlify('ans'), 16))

【问题讨论】:

    标签: python-2.7 hex bit pyserial binascii


    【解决方案1】:

    问题出在 inWaiting() 函数中。 这种读数不需要它。

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2021-12-21
      • 2019-07-12
      相关资源
      最近更新 更多