-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_manager.py
85 lines (69 loc) · 3.13 KB
/
serial_manager.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
import serial.tools.list_ports
class SerialManager:
def __init__(self):
self.serial_connection = None
self.esp32_connected = False # Track whether an ESP32 connection has been established
def find_esp32_port(self):
esp32_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'Silicon Labs' in p.manufacturer
]
if not esp32_ports:
raise IOError("No ESP32 found")
if len(esp32_ports) > 1:
print("Multiple ESP32s found - using the first one")
self.esp32_connected = True # Set to True when ESP32 is found
return esp32_ports[0]
def open_connection(self, port):
self.serial_connection = serial.Serial(port, 115200) # 115200 is the default baud rate for ESP32
self.esp32_connected = True # Set to True when connection is opened
return self.serial_connection # Return the serial connection object
def write_to_serial(self, data):
if not self.esp32_connected: # Check if ESP32 is connected
print("No ESP32 connection. Cannot write data.")
return
if self.serial_connection is None or not self.serial_connection.is_open:
print("Serial connection is not open. Cannot write data.")
return
try:
if isinstance(data, str):
data_bytes = data.encode() # Convert string to bytes if necessary
else:
data_bytes = data # Data is already in bytes format
self.serial_connection.write(data_bytes) # Write data to serial port
except serial.SerialException as e:
print("Error writing to serial port:", e)
# Handle the error condition (e.g., retry, log, or exit gracefully)
'''
class SerialManager:
def __init__(self):
self.serial_connection = None
def find_esp32_port(self):
esp32_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'Silicon Labs' in p.manufacturer
]
if not esp32_ports:
raise IOError("No ESP32 found")
if len(esp32_ports) > 1:
print("Multiple ESP32s found - using the first one")
return esp32_ports[0]
def open_connection(self, port):
self.serial_connection = serial.Serial(port, 115200) # 115200 is the default baud rate for ESP32
return self.serial_connection # Return the serial connection object
def write_to_serial(self, data):
if self.serial_connection is None or not self.serial_connection.is_open:
print("Serial connection is not open. Cannot write data.")
return
try:
if isinstance(data, str):
data_bytes = data.encode() # Convert string to bytes if necessary
else:
data_bytes = data # Data is already in bytes format
self.serial_connection.write(data_bytes) # Write data to serial port
except serial.SerialException as e:
print("Error writing to serial port:", e)
# Handle the error condition (e.g., retry, log, or exit gracefully)
'''