
from argparse import ArgumentParser
import sys
import serial

def run(device, baud):
    with serial.Serial(device, baud, timeout=0.1) as ser:
        while True:
            line = ser.readline()
            if line:
                sys.stdout.write(line)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('device',
                        help='serial device, typically /dev/tty.usbserial-*')
    parser.add_argument('--baud', dest='baud', type=int, default=74880)
    args = parser.parse_args()
    run(args.device, args.baud)


from argparse import ArgumentParser
import sys
import serial
from datetime import datetime

def run(device, baud, prefix=None):
    with serial.Serial(device, baud, timeout=0.1) as ser:
        while True:
            line = ser.readline()
            if not line:
                continue
            if prefix:
                line = prefix() + line
            sys.stdout.write(line)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('device',
                        help='serial device, typically /dev/tty.usbserial-*')
    parser.add_argument('--baud', dest='baud', type=int, default=74880)
    parser.add_argument('-t', '--timestamp', dest='timestamp', action='store_true',
                        help="Add timestamp to start of each line")
    args = parser.parse_args()
    prefix = None
    if args.timestamp:
        prefix = lambda: datetime.now().strftime("[%H:%M:%S.%f] ")
    run(args.device, args.baud, prefix)

Add option to timestamp each line from serial
Add option to timestamp each line from serial

  
  from argparse import ArgumentParser
  import sys
  import serial
+ from datetime import datetime
  
- def run(device, baud):
+ def run(device, baud, prefix=None):
      with serial.Serial(device, baud, timeout=0.1) as ser:
          while True:
              line = ser.readline()
-             if line:
+             if not line:
+                 continue
+             if prefix:
+                 line = prefix() + line
-                 sys.stdout.write(line)
+             sys.stdout.write(line)
  
  if __name__ == '__main__':
      parser = ArgumentParser()
      parser.add_argument('device',
                          help='serial device, typically /dev/tty.usbserial-*')
      parser.add_argument('--baud', dest='baud', type=int, default=74880)
+     parser.add_argument('-t', '--timestamp', dest='timestamp', action='store_true',
+                         help="Add timestamp to start of each line")
      args = parser.parse_args()
+     prefix = None
+     if args.timestamp:
+         prefix = lambda: datetime.now().strftime("[%H:%M:%S.%f] ")
-     run(args.device, args.baud)
+     run(args.device, args.baud, prefix)
  
Add option to timestamp each line from serial
## Code Before:

from argparse import ArgumentParser
import sys
import serial

def run(device, baud):
    with serial.Serial(device, baud, timeout=0.1) as ser:
        while True:
            line = ser.readline()
            if line:
                sys.stdout.write(line)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('device',
                        help='serial device, typically /dev/tty.usbserial-*')
    parser.add_argument('--baud', dest='baud', type=int, default=74880)
    args = parser.parse_args()
    run(args.device, args.baud)

## Instruction:
Add option to timestamp each line from serial
## Code After:

from argparse import ArgumentParser
import sys
import serial
from datetime import datetime

def run(device, baud, prefix=None):
    with serial.Serial(device, baud, timeout=0.1) as ser:
        while True:
            line = ser.readline()
            if not line:
                continue
            if prefix:
                line = prefix() + line
            sys.stdout.write(line)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('device',
                        help='serial device, typically /dev/tty.usbserial-*')
    parser.add_argument('--baud', dest='baud', type=int, default=74880)
    parser.add_argument('-t', '--timestamp', dest='timestamp', action='store_true',
                        help="Add timestamp to start of each line")
    args = parser.parse_args()
    prefix = None
    if args.timestamp:
        prefix = lambda: datetime.now().strftime("[%H:%M:%S.%f] ")
    run(args.device, args.baud, prefix)
