使用in-waiting读取接收到的字符串长度为0时,需要在前面加延时,sleep(2)就可以读到
读出为零时
import serial
import re
import time
def com_factory():
com = serial.Serial('Com3',115200)
#time.sleep(2)
len= com.in_waiting
print ('读取到的字符长度:',len)
str = com.read(len)
str = str.hex()
print('读取到的结果',str)
if __name__ == '__main__':
while True:
com_factory()
image.png
加了延时就读取正常
import serial
import re
import time
def com_factory():
com = serial.Serial('Com3',115200)
time.sleep(1)
len= com.in_waiting
print ('读取到的字符长度:',len)
str = com.read(len)
str = str.hex()
print('读取到的结果',str)
if __name__ == '__main__':
while True:
com_factory()
sd.png
网友评论