-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_from_arduino.py
36 lines (27 loc) · 1.14 KB
/
read_from_arduino.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
import serial
import time
import importlib
# Read values from arduino serial port
def read_from_arduino(serial_port, baud_rate=9600):
# Open the serial port
ser = serial.Serial(serial_port, baud_rate)
try:
while True:
# Read a line from the serial port
line = ser.readline().decode('utf-8').strip()
# Split the line by commas to get individual data items
data = line.split(',')
# Convert each item to an integer
int_data = [int(item) for item in data]
# Check the ranges and calculate the final value
final_value = flex_to_num(int_data)
# Print the result
print(f"Received data: {int_data} -> Calculated value: {final_value}")
# Wait for 1 second before the next iteration
time.sleep(1)
return final_value
except KeyboardInterrupt:
# Gracefully close the serial port if interrupted
print("Exiting program.")
finally:
ser.close()