python串口收发数据

2020-01-08 11:34:53 浏览数 (2)

一、利用虚拟串口工具,将com1和com3串口连接起来

二、运行程序

代码语言:javascript复制
import serial
from time import sleep



def recv(serial):
    while True:
        data = serial.read_all()
        if data == '':
            continue
        else:
            break
        sleep(0.02)
    return data



if __name__ == '__main__':
    serial = serial.Serial('COM3',115200, timeout=0.5)  #/dev/ttyUSB0
    if serial.isOpen() :
        print("open success")
    else :
        print("open failed")

    while True:


        str1 = input("请输入要发送到串口的话:")
        a=str1 "n"
        #print(len(a))
        serial.write((a).encode("gbk"))
        sleep(0.1)

        data =recv(serial)
        if data != b'' :
            print("receive : ",data.decode("gbk"))

三、打开串口调试工具,接受和发送数据

0 人点赞