-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialread.py
34 lines (29 loc) · 975 Bytes
/
serialread.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
import time
import serial
import logging
from datetime import datetime
# Constants
SERIAL_PORT = '/dev/ttyUSB0' # typical FTDI interface name
LOG_FILE = '/path/to/logfile.log' # Replace with your desired log file path
# Set up logging
logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s %(message)s')
# Function to read data from serial port
def read_serial_data(ser):
if not ser.isOpen():
ser.open()
ser_bytes = ser.readline()
return ser_bytes.decode('utf-8', 'ignore').strip()
# Main loop
def main():
ser = serial.Serial(SERIAL_PORT)
while True:
try:
data = read_serial_data(ser)
if data:
logging.info(f"Data: {data}")
print(f"Data: {data}")
except Exception as e:
logging.error(f"Error: {e}")
time.sleep(1) # Delay to prevent rapid error logging in case of continuous failure
if __name__ == "__main__":
main()