Python实现的BeagleBone Black 串口助手(全部源码)

继续昨天的工作,又开发了一个串口调试助手。

偷懒了,没有用面向对象,把之前的代码修改了一下,直接放在BBB 如图

 

源码如下:

__author__ = 'fuchunyang111@163.com'  
	
from tkinter import *  
import serial  
from tkinter import ttk
import time
import threading 
import Adafruit_BBIO.UART as UART

def Choice(event):  
	context = boxValue.get()  
	list = ['COM1']  
   

	
def Submit(data):
	if ser.isOpen() == True:
		n = ser.write(data)
		print(data)

	else:
		show00.delete(0.0,END)
		show00['background'] = "red"
		show00.insert(0.0,'Open serial first!')
	

def serial_read():
	while 1:
		if ser.isOpen() == True:
			data = ser.read(1)
			text2.insert(0.0,data)
			#print("11")
		#time.sleep(0.00006)
		time.sleep(0.006)
	
def open():
	if ser.isOpen() != True:
		ser.open()	
	print("uart1 opening")
	if ser.isOpen() == True: 
		lab2['background'] = "green"
		button2['text'] = 'Close serial'
		button2['command'] = close
		show00.delete(0.0,END)
		show00['background'] = "white"
		show00.insert(0.0,'Serial opened!')
		print("uart1 opened")
	else:
		lab2['background'] = "red"
		show00.delete(0.0,END)
		show00['background'] = "red"
		show00.insert(0.0,'Serial error!') 
def close():  
	ser.close()  
	if ser.isOpen() == False:
		lab2['background'] = "black"
		button2['text'] = 'Open serial'
		button2['command'] = open
		show00.delete(0.0,END)
		show00['background'] = "white"
		show00.insert(0.0,'Serial closed!')
	
def txd(): 
	data = text1.get(0.0,END)
	data = data[::-1]
	Submit(data.encode())
	
def clear1():
	text1.delete(0.0,END)
def clear2():
	text2.delete(0.0,END)
 # ##	--------------------------------------------- 

 
root = Tk()  
root.title("BBB_Serial Test")  
root.geometry("580x400")
w = Canvas(root,width = 580,height = 400,background = "#4169E1")
w.pack()
w.create_line(25,45,550,45,fill = 'black',width = 3) # top直线


#串口设置相关变量  
ser = serial.Serial(port = "/dev/ttyO1", baudrate=115200)

#串口号  
lab1 = Label(root,background = "#4169E1",text = 'Ser num:')  
lab1.place(x = 10,y = 10) 
#串口号选择下拉菜单  
boxValue = StringVar()  
boxChoice = ttk.Combobox(root,textvariable = boxValue,state = 'readonly',width = 6)  
boxChoice['value'] = ('COM1')  
boxChoice.current(0)  
boxChoice.bind('<<ComboboxSelected>>',Choice)  
boxChoice.place(x = 80,y = 10) 
#串口开启按钿 
button2 = Button(root,text = 'Open serial',command = open)  
button2.place(x = 150,y = 5)
#串口状态指示
lab2 = Label(root,text = '    ',background = "black")
lab2.place(x  = 260,y = 10)
#消息提示框
lab13 = Label(root,background = "#4169E1",text = 'Message:',)
lab13.place(x = 290,y = 10 )
#消息提示值框
show00 = Text(root,width = 26,height = 1,wrap = WORD)
show00.place(x = 360,y = 10 )
show00.delete(0.0,END)
show00.insert(0.0,'Thank you for using!')

#标签
lab5 = Label(root,background = "#4169E1",text = 'Txd:')
lab5.place(x = 10,y = 30*2+10 )
lab6 = Label(root,background = "#4169E1",text = 'Rxd:')
lab6.place(x = 10,y = 30*3+10 )

#值框
text1 = (Text(root,width = 45,height = 1,wrap = WORD))
text1.place(x = 70,y = 30*2+10 )
text2 = (Text(root,width = 45,height = 20,wrap = WORD))
text2.place(x = 70,y = 30*3+10 )

#发送,清空按钮
buttonTxd1 = Button(root,text = 'Send',command = txd,height = 1,width = 5)  
buttonTxd1.place(x = 410,y = 30*2+5)
buttonTxd2 = Button(root,text = 'clear',command = clear1,height = 1,width = 5)  
buttonTxd2.place(x = 490,y = 30*2+5)
buttonRxd = Button(root,text = 'clear',command = clear2,height = 1,width = 5)  
buttonRxd.place(x = 410,y = 30*3+15)

t = threading.Thread(target=serial_read) # set a thread 
t.setDaemon(True) # defend this thread 
t.start() # thread start

root.resizable(False,False) 
root.mainloop() 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值