我正在使用PySerial從串行端口讀取傳入的字節到緩衝區。每次我打電話給我的閱讀功能這個異常被拋出。PySerial拋出'NoneType'異常在讀取期間()
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "gatewayTester.py", line 480, in mcuRead
byteIn = mcuPort.read()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 269, in read
win32.ResetEvent(self._overlapped_read.hEvent)
AttributeError: 'NoneType' object has no attribute 'hEvent'
我的應用程序似乎工作正常,儘管拋出了這個異常。
但是,我想就這個問題的來源做一些澄清。
這是我的串行讀取功能。
def mcuRead(self, *args, **kwargs):
global mcuPort
global readVal
global serialWriting
global testRunning
print("READING MCU")
sys.stdout.flush()
inBuffer = ""
while testRunning:
try:
byteIn = ""
if not mcuPort.is_open:
mcuPort.open()
#Read incoming byte
if not serialWriting:
byteIn = mcuPort.read()
inBuffer += byteIn
#Skip newlines to parse incoming commands
if(byteIn == "\n"):
inBuffer = ""
#Check if buffer contains a valid command
for rxCmd in mcuRxCmds:
if inBuffer == rxCmd['Command']:
#store incoming command globally
readVal = rxCmd['Command']
#Clear buffer
inBuffer = ""
print("MCU IN "+str(readVal))
except(OSError, serial.SerialException):
print(OSError)
sys.stdout.flush()
串口被全局創建如下。讀取功能在單獨的線程上同時運行。
mcuPort = serial.Serial(port, 115200, timeout=0)