-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluetoothSerial.py
159 lines (128 loc) · 4.5 KB
/
BluetoothSerial.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
'''
Serial Terminal for those HC-05 HC-06 Bluetooth modules
Not nice but I have not found one I like so far.
Todo: add option for \n\r
'''
# here is the list of the prdefined commands
lCmds = ['at',
'at+pswd',
'at+name',
'at+role',
'at+cmode',
'at+version',
'at+reset',
'at+addr',
'at+orgl',
'at+rname',
'at+uart',
'at+bind',
'at+RMAAD',
'AT+ADCN',
'at+bind=',
''
]
version = '0.5'
import tkinter as tk
import tkinter.scrolledtext as tkscrolledtext
import serial.tools.list_ports
import io
# callback functions
def getComProts():
ports = list(serial.tools.list_ports.comports())
#print(ports)
if ports: return [x.device for x in ports]
else: return['No COM Port']
def refrehComProts():
ports = getComProts()
# Reset selectedCOMPort and delete all old options
selectedCOMPort.set('')
ComPortListDropDown['menu'].delete(0, 'end')
# Insert list of new options (tk._setit hooks them up to selectedCOMPort)
for choice in ports:
ComPortListDropDown['menu'].add_command(label=choice, command=tk._setit(selectedCOMPort, choice))
selectedCOMPort.set(ports[0])
def clearOutPut():
textbox.delete('1.0',tk.END)
def runScript(text = 'AT', row=None):
entries = entries_by_row[row] if row is not None else ['AT']
cmd = str(entries[0].get())
textbox.insert('1.0', 'CMD: ' + cmd +'\n')
with serial.Serial(selectedCOMPort.get(), selectedBaud.get(), timeout=1) as ser:
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
sio.write(cmd +"\n\r")
sio.flush() # it is buffering. required to get the data out *now*
#line = sio.readline()
seq =[]
count = 0
while True:
for c in sio.read():
seq.append(c)
joined_seq = ''.join(str(v) for v in seq) #Make a string from array
print (ord(c))
if c == '\n':
textbox.insert('1.0', "RESP " + str(count) + ': ' + joined_seq)
seq = []
count += 1
break
if 'c' not in dir(): textbox.insert('1.0', 'No Response!!! not in AT mode?!?!\n')
# Layout
top = tk.Tk()
layoutCnf = {'column': 0,
'row': 0,
#'ipadx': 5,
'padx': 5,
#'ipady': 5,
'pady': 5,
}
# Comport Bottons and settings
selectedCOMPort = tk.StringVar(top)
lComPorts = getComProts()
selectedCOMPort.set(lComPorts[0])
ComPortListDropDown = tk.OptionMenu(top, selectedCOMPort, *lComPorts)
selectedBaud = tk.StringVar(top)
lBaud = ['9600', '19200', '38400', '115200', '230400', '250000']
selectedBaud.set(lBaud[2])
BaudListDropDown = tk.OptionMenu(top, selectedBaud, *lBaud)
reFreshButton = tk.Button(text='Refresh', command=refrehComProts)
clearOutPutButton = tk.Button(text='Clear', command=clearOutPut)
## Layout
reFreshButton.grid(cnf=layoutCnf)
layoutCnf['column'] = 1
ComPortListDropDown.grid(cnf=layoutCnf)
layoutCnf['column'] += 1
BaudListDropDown.grid(cnf=layoutCnf)
layoutCnf['column'] += 1
clearOutPutButton.grid(cnf=layoutCnf)
layoutCnf['row'] +=1
layoutCnf['column'] = 0
# CMD buttons and entries
rowOffset = 0
row = 1
column = 0
entries_by_row = []
for command in range(len(lCmds)):
entries_by_row.append([])
button = tk.Button(top, text='Send', command=lambda row=row: runScript('nondefault text', row-1))
button.grid(layoutCnf, row=rowOffset+row, column=column, sticky='w')
column +=1
entry = tk.Entry(top)
entry.insert(0,lCmds[command])#text %s'%command)
entry.grid(layoutCnf, row=rowOffset+row, column=column, sticky='w', columnspan=3)
column +=1
entries_by_row[-1].append(entry)
row +=1
column = 0
layoutCnf['row'] = rowOffset + row + 1
layoutCnf['row'] = 1
layoutCnf['column'] = 2
# scrolled text box used to display the serial data
frame = tk.Frame(top, bg='cyan')
frame.grid(cnf=layoutCnf, columnspan=3, rowspan=len(lCmds))
textbox = tkscrolledtext.ScrolledText(master=frame, wrap='word', width=80, height=30) #width=characters, height=lines
textbox.grid(cnf=layoutCnf)#, columnspan=3, rowspan=len(lCmds))
#layoutCnf['row'] +=1
#layoutCnf['column'] = 0
#pack(side='bottom', fill='y', expand=True, padx=0, pady=0)
textbox.config(font="bold")
top.mainloop()