# Time stamp CAN messages from the serial stream. import sys from datetime import datetime import io def add_timestamp_to_lines(): # Ensure stdin is read with a fallback encoding with io.TextIOWrapper(sys.stdin.buffer, encoding='ISO8859-1', errors='replace') as stdin: for line in stdin: if 'CANMSG' in line or 'CMQ' in line: timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S.%f] ") sys.stdout.write(timestamp + line) #else: #sys.stdout.write(line) if __name__ == "__main__": add_timestamp_to_lines()