-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to write the serial print into a log file.
- Loading branch information
1 parent
4a1fe86
commit bb812c2
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |