Skip to content

Commit

Permalink
Add script to write the serial print into a log file.
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavosr8 committed Feb 6, 2025
1 parent 4a1fe86 commit bb812c2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
37 changes: 37 additions & 0 deletions scripts/log-serial/log-serial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3

import serial
import sys
from datetime import datetime


if len(sys.argv) < 3:
print("Usage: ./log-serial.py <file_path> <serial_port>")
sys.exit(1)

file_path = sys.argv[1]
serial_port = sys.argv[2]

serport = serial.Serial(baudrate=19200)
serport.setPort(serial_port)
serport.open()

# Don't reset the MMC but drive the P2.10 pin to HIGH, so it can be reset
# externally and execute the application code, not the ROM bootloader
serport.setRTS(0) # set RTS line to 3.3V
serport.setDTR(0) # set DTR line to 3.3V


with open(file_path, "a") as log_file:
while True:

try:
message = serport.readline().decode("utf-8", errors="replace").strip()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f %z")
log_entry = f"[{timestamp}] {message}\n"
log_file.write(log_entry)
print(log_entry, flush=True)
log_file.flush() # Always flush when a new log is written

except serial.SerialException:
print("Serial port disconnected. Shutting down...")
10 changes: 10 additions & 0 deletions scripts/log-serial/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Serial Logger Service

[Service]
ExecStart=/usr/local/bin/log-serial.py /var/log/openmmc-serial.log %i
StandardOutput=journal
Restart=always

[Install]
WantedBy=multi-user.target

0 comments on commit bb812c2

Please sign in to comment.