-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (53 loc) · 1.67 KB
/
main.py
File metadata and controls
68 lines (53 loc) · 1.67 KB
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
import serial
import matplotlib.pyplot as plt
import numpy as np
import time
import structlog
logger = structlog.get_logger()
def on_integrate_button_click(event):
logger.info("Button click")
def main():
# Configure the serial connection
SERIAL_PORT = '/dev/cu.usbserial-0001' # Change this to your serial port
BAUD_RATE = 921600
# Open the serial connection
ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
# set integration time
ser.write(b'\xB1') # Set integration time to 10us
# Prepare data storage
NUM_POINTS = 3647
DATA_SIZE = 7296
spectral_data: list[int] = [0 for _ in range(NUM_POINTS)]
# Set up the plot
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(spectral_data)
ax.set_ylim(0, 6000)
ax.set_xlim(0, NUM_POINTS)
ax.set_title('Intensity Data')
ax.set_xlabel('Point')
ax.set_ylabel('Intensity')
try:
while True:
spectral_data.clear()
# Send the request byte
ser.write(b'\xA1')
time.sleep(0.05)
# Read the intensity data
data: bytes = ser.read(DATA_SIZE)
logger.info(f"Got {len(data)} bytes of data")
for i in range(1, 3648):
spectral_data.append(data[2 * i + 1] * 256 + data[2 * i])
# Update the plot
line.set_ydata(spectral_data)
fig.canvas.draw()
fig.canvas.flush_events()
except KeyboardInterrupt:
print("Program terminated by user.")
except Exception as e:
logger.exception(e)
finally:
# Close the serial connection
ser.close()
if __name__ == "__main__":
main()