我正在尝试读取串行端口。问题是脚本有效,但读取命令似乎不尊重参数(要读取 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))