-
Notifications
You must be signed in to change notification settings - Fork 0
/
readimage.py
44 lines (34 loc) · 1.12 KB
/
readimage.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
import serial
import time
import datetime
# Open the serial port
ser = serial.Serial('COM4', 115200, timeout=1) # Adjust 'COM_PORT' as necessary
def save_image(data, filename=None):
if filename is None:
# Use current time to create a unique filename
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"image_{timestamp}.jpg"
with open(filename, 'wb') as f:
f.write(data)
def read_image():
image_data = bytearray()
start_time = time.time()
while True:
if ser.in_waiting:
byte = ser.read()
image_data += byte
# Reset the timer if data is being received
start_time = time.time()
elif time.time() - start_time > 2:
# No data received for 2 seconds, assume image is complete
break
return image_data
if __name__ == '__main__':
while True:
print("Waiting for image data...")
data = read_image()
if data:
print("Image received, saving...")
save_image(data)
print("Image saved.")
time.sleep(5) #