|
@@ -0,0 +1,31 @@
|
|
|
|
+def get_data():
|
|
|
|
+ import serial
|
|
|
|
+ import time
|
|
|
|
+
|
|
|
|
+ # Configure serial port (adjust port name as needed)
|
|
|
|
+ ser = serial.Serial('COM10', 9600)
|
|
|
|
+
|
|
|
|
+ # Wait for the serial connection to establish
|
|
|
|
+ ser.timeout = 2
|
|
|
|
+
|
|
|
|
+ #ser.write(b'testcom.py\n') # Send command to start the script
|
|
|
|
+ while True:
|
|
|
|
+
|
|
|
|
+ # Read data from serial port
|
|
|
|
+ data = ser.readline().decode().strip()
|
|
|
|
+
|
|
|
|
+ # Check if data is not empty
|
|
|
|
+ #print(data)
|
|
|
|
+ if data:
|
|
|
|
+ try:
|
|
|
|
+ # Attempt to convert data to float
|
|
|
|
+ value = float(data)
|
|
|
|
+ timestamp = time.time()
|
|
|
|
+ return timestamp, value
|
|
|
|
+ except ValueError:
|
|
|
|
+ print("Invalid data format:", data)
|
|
|
|
+ else:
|
|
|
|
+ print("Empty data received")
|
|
|
|
+
|
|
|
|
+ # Add a small delay to prevent rapid looping
|
|
|
|
+ time.sleep(0.001)
|