0、检测可用串口
import serial.tools.list_ports
def SearchPorts():
port_list = list(serial.tools.list_ports.comports())
if len(port_list) == 0:
print('无可用串口')
else:
for i in range(len(port_list)): print(port_list[i])
1、打开串口
com = serial.Serial('COM3', 115200, timeout = 0.5)
#设置串口号COM3,波特率115200,连接超时0.05s
print com
print com.name #设备名称
print com.port #设备名
print com.baudrate #波特率
print com.bytesize #字节大小
print com.parity #校验位N-无校验,E-偶校验,O-奇校验
print com.stopbits #停止位
print com.timeout #读超时设置
print com.writeTimeout #写超时
print com.xonxoff #软件流控
print com.rtscts #硬件流控
print com.dsrdtr #硬件流控
print com.interCharTimeout #字符间隔超时
//输出串口信息
2、串口配置
#bytesize:FIVE BITS、SIXBITS、SEVENBITS、EIGHTBITS
#parity: PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE
#stopbits: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
com.baudrate = 9600 #设置波特率
com.bytesize = 8 #字节大小
com.bytesize = serial.EiGHTBITS #8位数据位
#一般配置无校验位
com.parity = serial.PARITY_NONE #无校验
#ser.parity = serial.PARITY_ODD #奇校验
#ser.parity = serial.PARITY_EVEN #偶校验
com.stopbits = 1 #停止位
com.timeout = 0.5 #读超时设置
com.writeTimeout = 0.5 #写超时
#com.xonxoff #软件流控
#com.rtscts #硬件流控
#com.dsrdtr #硬件流控
#com.interCharTimeout #字符间隔超时
3、向串口写数据
success_bytes = com.write('This is data for test')
print success_bytes #返回成功写入的字节数
4、从串口读数据
data = com.read(10)
data = com.readline()
data = com.readlines()
data = com.read_all()
print data
5、串口通讯
import serial
import serial.tools.list_ports
import time
#查找可用串口
def SearchPorts():
port_list = list(serial.tools.list_ports.comports())
if len(port_list) == 0:
print('无可用串口')
else:
for i in range(len(port_list)): print(port_list[i])
#打开串口
def OpenPort(FreeSerial):
try:
com = serial.Serial(FreeSerial,9600,timeout=0.05)
if com.isOpen() == True:
print("串口已打开")
print("*---------------串口配置信息----------------*")
print("串口名:",com.name,"\n串口波特率:",com.baudrate,"\n串口校验位:",com.parity,"\n串口停止位:",com.stopbits,"\n串口读写超时:",com.timeout)
print("*------------------------------------------*\n")
else:
print("串口异常")
except:
print("串口异常")
return com
#写操作
def Write(com,data):
SuccessfulByte = com.write(str(data).encode("utf-8"))
if(SuccessfulByte!=0):
print("*---------------写操作反馈------------------*")
print("成功写入:",SuccessfulByte,"Byte")
print("*------------------------------------------*\n")
#读操作
def Read(com):
data = com.read_all()
SuccessfulByte = len(data)
if(SuccessfulByte!=0):
print("*---------------读操作反馈------------------*")
print("成功接收", SuccessfulByte, "Byte")
print("*------------------------------------------*\n")
return data
转载自ahilll的CSDN博客
https://blog.csdn.net/ahilll/article/details/81531587
import tkinter as tk
import Work
#窗口配置
window = tk.Tk()
window.title('Serial Communication')
window.geometry('700x600')
Port_list = Work.SearchPorts()
com = ''
Port = []
for i in Port_list:
Port.append(str(i))
if(len(Port)==0):
Port.append('COM0 - NO FREE PORT')
Port.append('COM0 - NO FREE PORT')
elif(len(Port)==1):
Port.append('COM0 - NO FREE PORT')
var1 = tk.StringVar()
lable_1 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11), width=10, text='BAUDRATE')
lable_1.place(x=25,y=10)
var2 = tk.StringVar()
lable_2 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11), width=10, text='PARITY')
lable_2.place(x=25,y=80)
var3 = tk.StringVar()
lable_3 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11), width=10, text='STOPBITS')
lable_3.place(x=25,y=180)
var4 = tk.StringVar()
lable_4 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11) ,width=10, text='BYTE')
lable_4.place(x=25,y=260)
var5 = tk.StringVar()
lable_5 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11), width=10, text='PORT')
lable_5.place(x=25,y=340)
var6 = tk.StringVar()
lable_6 = tk.Label(window, bg='white', fg='red', font=('Arial', 10) ,width=20, text='empty')
lable_6.place(x=5,y=570)
lable_7 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11), width=10, text='ReadData')
lable_7.place(x=150,y=10)
lable_8 = tk.Label(window, bg='white', fg='brown', font=('Arial', 11), width=10, text='WriteData')
lable_8.place(x=150,y=465)
# 串口配置选项
r1 = tk.Radiobutton(window, text='9600', variable=var1, value='9600')
r1.place(x=25,y=30)
r2 = tk.Radiobutton(window, text='115200', variable=var1, value='115200')
r2.place(x=25,y=50)
r3 = tk.Radiobutton(window, text='N (无校验)', variable=var2, value='N')
r3.place(x=25,y=110)
r4 = tk.Radiobutton(window, text='O (奇校验)', variable=var2, value='O')
r4.place(x=25,y=130)
r5 = tk.Radiobutton(window, text='E (偶校验)', variable=var2, value='E')
r5.place(x=25,y=150)
r6 = tk.Radiobutton(window, text='N(无停止位)', variable=var3, value='N')
r6.place(x=25,y=210)
r7 = tk.Radiobutton(window, text='1(1位停止位)', variable=var3, value='1')
r7.place(x=25,y=230)
r8 = tk.Radiobutton(window, text='8 Byte', variable=var4, value='8')
r8.place(x=25,y=290)
r9 = tk.Radiobutton(window, text='16 Byte', variable=var4, value='16')
r9.place(x=25,y=310)
r10 = tk.Radiobutton(window, text=str(Port[0])[:5], variable=var5, value=str(Port[0])[:5])
r10.place(x=25,y=370)
r11 = tk.Radiobutton(window, text=str(Port[1])[:5], variable=var5, value=str(Port[1])[:5])
r11.place(x=25,y=390)
on_hit = False
#回调:开启串口
def Open():
global on_hit
global com
if on_hit == False:
on_hit = True
print(var5.get())
com = Work.OpenPort(var5.get(),var1.get(),'0.5',var2.get(),var3.get(),var4.get())
var6.set('串口已打开')
lable_6.config(text = var6.get())
else:
on_hit = False
Work.Close(com)
var6.set('串口已关闭')
lable_6.config(text = var6.get())
#回调:写数据
def Write():
Work.Write(com,entry_1.get())
#回调:读数据
def Read():
var = Work.Read(com)
text_1.insert('end', var)
button_1 = tk.Button(window, text='打开/关闭串口', font=('Arial', 11), width=10, height=1, command=Open)
button_1.place(x=25,y=430)
button_2 = tk.Button(window, text='发送数据', font=('Arial', 11), width=10, height=1, command=Write)
button_2.place(x=25,y=480)
button_3 = tk.Button(window, text='读取数据', font=('Arial', 11), width=10, height=1, command=Read)
button_3.place(x=25,y=530)
entry_1 = tk.Entry(window, show=None, state='normal',width=70)
entry_1.place(x=150,y=500)
text_1 = tk.Text(window,state='normal',height=30,width=70)
text_1.place(x=150,y=50)
window.mainloop()