Advertisement
Guest User

Log COM port data to file for Arduino

a guest
Feb 28th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. ###################################################
  2. #    Write log from COM port to file for Arduino  #
  3. #    Requires Python 2.7 with PySerial installed  #
  4. #    Install PySerial: pip install pyserial       #
  5. ###################################################
  6.  
  7. import serial
  8.  
  9. COM_PORT = 'COM7'
  10. COM_BAUD = 9600
  11. LOG_FILE = 'data_log.txt'
  12.  
  13.  
  14. def create_port(name, baud, timeout = 1):
  15.     return serial.Serial(
  16.       port=name,
  17.       baudrate=baud,
  18.       bytesize=8,
  19.       parity=serial.PARITY_NONE,
  20.       stopbits=1,
  21.       rtscts=False,
  22.       dsrdtr=False,
  23.       timeout=timeout
  24.       )
  25.  
  26.  
  27. if __name__ == '__main__':
  28.     com = create_port(COM_PORT, COM_BAUD)
  29.     print "Logging started"
  30.     with open(LOG_FILE, "a") as log:    
  31.         while True:
  32.             try:
  33.                 line = com.readline().strip()
  34.                 if len(line):
  35.                     print(line)
  36.                     log.write(line + "\r\n")
  37.                     log.flush()
  38.             except:
  39.                 break  
  40.     print("Logging stopped\r\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement