我是靠谱客的博主 微笑发箍,最近开发中收集的这篇文章主要介绍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() 

 

 

最后

以上就是微笑发箍为你收集整理的Python实现的BeagleBone Black 串口助手(全部源码)的全部内容,希望文章能够帮你解决Python实现的BeagleBone Black 串口助手(全部源码)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部