美文网首页
每日学习5-python3.6 串口操作中遇到的问题

每日学习5-python3.6 串口操作中遇到的问题

作者: Symbian米汤 | 来源:发表于2018-01-30 12:58 被阅读0次

使用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

相关文章

网友评论

      本文标题:每日学习5-python3.6 串口操作中遇到的问题

      本文链接:https://www.haomeiwen.com/subject/xunqzxtx.html