美文网首页
读取串口

读取串口

作者: qy_ffa1 | 来源:发表于2022-04-20 23:23 被阅读0次

    安装   pyserial  及 serial

    程式如下:

    import serial# 导入串口包

    import time# 导入时间包

    ser = serial.Serial("COM3",9600,timeout=5)# 开启com3口,波特率9600,超时5

    ser.flushInput()# 清空缓冲区

    def main():

    while True:

    count = ser.inWaiting()# 获取串口缓冲区数据

            if count !=0:

    recv = ser.read(ser.in_waiting).decode("gbk")# 读出串口数据,数据采用gbk编码

                print(recv)# 打印一下子

            time.sleep(0.1)# 延时0.1秒,免得CPU出问题

    if __name__ =='__main__':

    main()

    相关文章

      网友评论

          本文标题:读取串口

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