-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat_cmd_interface.py
268 lines (225 loc) · 9.58 KB
/
at_cmd_interface.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import subprocess
import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox
import serial
import serial.tools.list_ports
import threading
import time
class SerialCommunicator:
"""
Handles serial communication with a device.
Attributes:
port (str): The serial port to connect to.
baud_rate (int): The baud rate for serial communication (default: 115200).
timeout (int): Timeout value for serial read operations (default: 1).
ser (serial.Serial): The serial object.
Author: Starke Wang
"""
def __init__(self, port, baud_rate=115200, timeout=1):
"""
Initializes SerialCommunicator with the specified parameters.
"""
self.port = port
self.baud_rate = baud_rate
self.timeout = timeout
self.ser = None
def connect(self):
"""
Establishes a serial connection to the specified port.
Returns:
bool: True if connection is successful, False otherwise.
"""
try:
self.ser = serial.Serial(self.port, self.baud_rate, timeout=self.timeout)
return True
except serial.SerialException as e:
print(f"Error connecting to serial port: {e}")
return False
def disconnect(self):
"""
Closes the serial connection.
"""
if self.ser:
self.ser.close()
self.ser = None
def send_command(self, command):
"""
Sends an AT command to the device.
Args:
command (str): The AT command to send.
Returns:
str: The response from the device.
"""
if self.ser:
self.ser.write(command.encode())
time.sleep(0.1) # Adjust sleep time as needed
response = self.ser.read_all().decode()
return response
else:
return "Not connected to serial port."
class ATCommandInterface:
"""
Provides a graphical user interface for sending AT commands to a device.
Attributes:
baud_rate (int): The baud rate for serial communication (default: 115200).
serial_communicator (SerialCommunicator): The serial communicator object.
Author: Starke Wang
"""
def __init__(self, baud_rate=115200):
"""
Initializes ATCommandInterface with the specified baud rate.
"""
self.baud_rate = baud_rate
self.serial_communicator = None
self.create_gui()
def create_gui(self):
"""
Creates the graphical user interface elements.
"""
self.root = tk.Tk()
self.root.title("AT Command Interface")
# Configure styles
style = ttk.Style()
style.configure("TButton", padding=6, relief="flat", background="#ccc")
style.configure("TLabel", font=("Helvetica", 12))
style.configure("TCombobox", padding=5)
style.configure("TEntry", padding=5)
# --- COM Port Selection ---
port_frame = ttk.Frame(self.root)
port_frame.pack(pady=10)
port_label = ttk.Label(port_frame, text="COM Port:")
port_label.pack(side=tk.LEFT, padx=5)
self.com_port_var = tk.StringVar()
self.com_port_combo = ttk.Combobox(port_frame, textvariable=self.com_port_var, state="readonly", width=15)
self.com_port_combo.pack(side=tk.LEFT, padx=5)
self.update_com_ports()
connect_button = ttk.Button(port_frame, text="Connect", command=self.connect_to_port)
connect_button.pack(side=tk.LEFT, padx=5)
disconnect_button = ttk.Button(port_frame, text="Disconnect", command=self.disconnect_from_port)
disconnect_button.pack(side=tk.LEFT, padx=5)
# --- Device Info ---
device_info_label = ttk.Label(self.root, text="Device Info:")
device_info_label.pack(pady=5)
self.device_info_var = tk.StringVar(value="No device detected")
device_info_display = ttk.Label(self.root, textvariable=self.device_info_var, font=("Helvetica", 12, "bold"))
device_info_display.pack(pady=5)
# --- Command Input ---
command_frame = ttk.Frame(self.root)
command_frame.pack(pady=10)
command_label = ttk.Label(command_frame, text="AT Command:")
command_label.pack(side=tk.LEFT, padx=5)
self.command_entry = ttk.Entry(command_frame, width=50)
self.command_entry.pack(side=tk.LEFT, padx=5)
send_button = ttk.Button(command_frame, text="Send", command=self.send_at_command)
send_button.pack(side=tk.LEFT, padx=5)
# --- Output Display ---
output_frame = ttk.Frame(self.root)
output_frame.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
self.output_text = scrolledtext.ScrolledText(output_frame, wrap=tk.WORD, width=70, height=20)
self.output_text.pack(fill=tk.BOTH, expand=True)
clear_button = ttk.Button(self.root, text="Clear Output", command=self.clear_output)
clear_button.pack(pady=5)
# Center the window
self.root.update_idletasks()
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
window_width = self.root.winfo_width()
window_height = self.root.winfo_height()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
self.root.geometry(f"+{x}+{y}")
# Closing Event
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
def update_com_ports(self):
"""
Updates the list of available COM ports in the GUI.
"""
ports = [port.device for port in serial.tools.list_ports.comports()]
self.com_port_combo['values'] = ports
if ports:
self.com_port_combo.current(0) # Select the first port by default
def connect_to_port(self):
"""
Connects to the selected COM port.
"""
selected_port = self.com_port_var.get()
self.serial_communicator = SerialCommunicator(selected_port, baud_rate=self.baud_rate)
if self.serial_communicator.connect():
self.output_text.insert(tk.END, f"Connected to {selected_port}\n")
self.get_device_info()
else:
messagebox.showerror("Error", f"Failed to connect to {selected_port}")
def disconnect_from_port(self):
"""
Disconnects from the current COM port.
"""
if self.serial_communicator:
self.serial_communicator.disconnect()
self.output_text.insert(tk.END, "Disconnected from serial port\n")
self.device_info_var.set("No device detected")
def get_device_info(self):
"""
Retrieves device information (DSN and Battery Level) using ADB in a separate thread.
"""
def get_info_thread():
try:
# Check if a device is connected
adb_check = subprocess.run(["adb", "devices"], capture_output=True, text=True)
if "device" in adb_check.stdout:
# Use ADB command to get device serial number
serial_process = subprocess.run(
["adb", "get-serialno"],
capture_output=True, text=True
)
serial_number = serial_process.stdout.strip()
# Use ADB command to get battery level
battery_process = subprocess.run(
["adb", "shell", "cat /sys/class/power_supply/battery/capacity"],
capture_output=True, text=True
)
battery_level = battery_process.stdout.strip()
if serial_number and battery_level:
self.device_info_var.set(f"Serial Number: {serial_number}, Battery: {battery_level}%")
else:
self.device_info_var.set("Unable to retrieve device information.")
else:
self.device_info_var.set("No ADB device connected.")
except Exception as e:
self.device_info_var.set(f"ADB error: {e}")
threading.Thread(target=get_info_thread).start()
def send_at_command(self):
"""
Sends the AT command entered by the user.
"""
command = self.command_entry.get().strip().upper()
if not command.startswith("AT"):
self.output_text.insert(tk.END, "Invalid AT Command format.\n")
return
if not command.endswith('\r'):
command += '\r'
if self.serial_communicator:
response = self.serial_communicator.send_command(command)
self.output_text.insert(tk.END, f"Command: {command.strip()}\nResponse: {response}\n")
self.output_text.see(tk.END)
else:
self.output_text.insert(tk.END, "Not connected to serial port.\n")
def clear_output(self):
"""
Clears the output text area.
"""
self.output_text.delete(1.0, tk.END)
def on_closing(self):
"""
Handles the window closing event.
"""
if self.serial_communicator:
self.serial_communicator.disconnect()
self.root.destroy()
def run(self):
"""
Starts the Tkinter main loop.
"""
self.root.mainloop()
if __name__ == "__main__":
app = ATCommandInterface(baud_rate=115200)
app.run()