PYTHON+TKINTER+pySERIAL 串口上位机

# 导入所需库
from tkinter import *
import tkinter as tk
from tkinter.messagebox import *
import serial#导入串口通信库
from time import sleep
import serial.tools.list_ports



# 创建串口实例
ser = serial.Serial()
# 创建 tkinter 窗口或小部件的实例
win = Tk()
win.geometry("700x350")
a=0
def update_text():
   # 配置 Label 小部件中的文本
   #a=1
   global a
   a = int(a) + 1
   if (var.get()==''):#判断字符为空
       print('请选择要开启的串口')
       result = showerror('错误', '请选择要开启的串口')
       print(f'错误: {result}')
       a=0
   else :  
       if (a==1) :
           port_open_recv()#开启串口
           button.configure(text="串口已开启",font=('Helvetica 14 bold'),bg='green')
           print(var.get(),"串口已开启")
           print(a)
       if (a==2):
            port_close()#关闭串口
            button.configure(text="串口已关闭",font=('Helvetica 14 bold'),bg='red')
            print(var.get(),"串口已关闭")
            print(a)
            a=0

class var_send:#类似c语言的静态变量var_send.num
    num=0

def send_buff():
    send(entry_send_value.get())
    send('\n')
    print('var_send:',entry_send_value.get())

def send_buff_0(): 
   # send('0\n')
   # send(entry_value.get())
    send('0\n')
    print('var_send:',entry_value.get())
    
    
def port_open_recv():#对串口的参数进行配置
    
    #ser.port=ser_port
    #ser.port="com12"
    ser.timeout=0.01
    ser.port=var.get()
    ser.baudrate=115200
    ser.bytesize=8
    ser.stopbits=1
    ser.parity="N"#奇偶校验位
    ser.open()
    if(ser.isOpen()):
        print(ser.port,"串口打开成功!")
    else:
        print("串口打开失败!")
#isOpen()函数来查看串口的开闭状态

def serload(ser):
    while True:
        if ser.in_waiting:
            str1 = ser.readline().decode('GBK')  # 读一行,以/n结束。
            #char1 = print(ser.read(size=1).hex()) # 从串口读size个字节
            print(str1)          

def port_close():
    ser.close()
    if(ser.isOpen()):
        print("串口关闭失败!")
    else:
        print("串口关闭成功!")

def send(send_data):
    if(ser.isOpen()):
        ser.write(send_data.encode('utf-8'))#编码
        print("发送成功",send_data)
    else:
        print("发送失败!")

#创建option
var=StringVar() # 字符串变量
var.set("com2")
op1=OptionMenu(win,var,'com10','com2','com3','com4','com5','com6','com7','com8','com9','com10','com11','com12','com13','com14','com15')
op1.place(x=10, y=10)
#创建option
#send_var=StringVar() # 字符串变量
#op2=OptionMenu(win,send_var,'0\n','b\n','r\n','d\n','e\n')
#op2.place(x=10, y=50)

# 创建 Label 小部件
#label=Label(win, text="串口发送", font=('Helvetica 10 bold'))
#label.place(x=500, y=50)

# 创建一个按钮以更新 label 小部件的文本
button=Button(win, text="串口已关闭", command=update_text,font=('Helvetica 14 bold'),bg='red')
button.place(x=100, y=10)
#button.pack()
# 创建一个按钮以更新 label 小部件的文本
send_button=Button(win, text="串口发送", command=send_buff,font=('Helvetica 14 bold'),bg='white')
send_button.place(x=100, y=100)

send_button=Button(win, text="开机", command=send_buff_0,font=('Helvetica 14 bold'),bg='white')
send_button.place(x=100, y=230)

text11=Label(win,text=" baudrate=115200\n bytesize=8\n stopbits=1\n",justify=LEFT)
text11.place(x=250, y=5)
 
entry_send_value=Entry(win) 
entry_send_value.place(x=100, y=70)
 
entry_value=Entry(win) 
entry_value.place(x=100, y=200)

def receive_buff():
    if(ser.isOpen()):
        com_input=ser.readline(200)
        if com_input :
            print(com_input.decode('utf-8'))   
    win.after (100,receive_buff)               
                
 
win.after (100,receive_buff)               
   
win.mainloop()





在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoqi976633690

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值