Python script read serial port, output to text file -
i'm trying read output serial port , write text file. far connects , prints output, can't figure out how send output text file (preferrable csv file).
#! /usr/bin/env python3 import serial import csv import sys import io #excel stuff #from time import gmtime, strftime #resultfile=open('mydata.csv','wb') #end excel stuff def scan(): """scan available ports. return list of tuples (num, name)""" available = [] in range(256): try: s = serial.serial(i) available.append( (i, s.portstr)) s.close() # explicit close 'cause of delayed gc in java except serial.serialexception: pass return available if __name__=='__main__': print ("found ports:") n,s in scan(): print ("(%d) %s" % (n,s)) selection = input("enter port number:") try: ser = serial.serial(eval(selection), 9600, timeout=1) print("connected to: " + ser.portstr) except serial.serialexception: pass while true: # read line , convert b'xxx\r\n' xxx line = ser.readline().decode('utf-8')[:-1] if line: # if isn't blank line # f=open('testing.txt','w+') print(line) #print >> f.write('test.txt') f.close() #print(line) #with open('test.csv', 'w') csv_file: # writer = csv.dictwriter(csv_file, fieldnames=['header1'], lineterminator='\n') ser.close()
import sys sys.stdout = open('file', 'w') print 'test'
or redirect shell-output
$ python foo.py > file
duplicate of this: redirect stdout file in python?
Comments
Post a Comment