继续昨天的工作,又开发了一个串口调试助手。
偷懒了,没有用面向对象,把之前的代码修改了一下,直接放在BBB 如图
源码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117__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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复