Python之pyserial_BT2UART

使用pyserial实现串口调试简单功能:

import os
import sys
import serial
import binascii

#SerialPort='com4'
#Baudrate=9600
#ser = serial.Serial('com4', 9600)

SerialPort = sys.argv[1]
Baudrate = sys.argv[2]

def read():
    global ser
    while 1:
        data = ser.readline()
        print(data.decode('ascii'), end='')

def write():
    global ser
    while 1:
        data = input(">")
        print(data)
        data = data.encode()
        ser.write(data)
        ser.write(b'\r\n')

ser = serial.Serial(SerialPort, Baudrate)
if ser.is_open:
    print('SerialPort is opened!')
else:
    ser = serial.Serial(SerialPort, Baudrate)
#read()
write()
if ser.is_close():
    print('SerialPort is closed!')
else:
    ser.close()
time.sleep(1)
os.system("pause")

bat脚本内容如下:

@echo off
SET SerialPort="com4"
SET Baudrate=9600
SET PATH=%PATH%;C:\Users\Administrator\AppData\Local\Programs\Python\Python36
SET SourceFile=pyserial_BT2UART.py
cd %cd%
echo %cd% %SourceFile% %SerialPort% %Baudrate%
python.exe %SourceFile% %SerialPort% %Baudrate%

你可能感兴趣的:(Python)