py:serialport transmission

File : serial_trans.py (直接右键另存为下载)
Type : python
Brief : 使用python进行串口通信。


import serial
import os
import sys
import numpy as np
import time

port             = 'COM6'
baud             = 115200

if __name__ == "__main__":
    ser = serial.Serial(port, baud)
    ser.parity = serial.PARITY_NONE
    ser.stopbits = 1
    ser.bytesize = 8
    if ser.isOpen():
        print("INFO: Open serialport success.")
    else:
        print("INFO: Open serialport failed.")
        os.system("pause")
        sys.exit()

    try:
        while True:
            cnt = ser.inWaiting()
            if cnt > 0:
                data = ser.read(cnt)
                print(data)

    except KeyboardInterrupt:
        if ser != None:
            ser.close()

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [ [email protected] ]