2017-07-15 35 views
0

下面的程序編譯但不打印數據到文件。我也試過(1),但沒有得到正確的輸出(沒有數據)。我仍然在學習python嵌入式和文件編程。任何人都可以看一看,並指出我的方向是正確的嗎?下面如何將arduino加速度計實時數據打印到文件

代碼:

import logging 
    import serial 
    import serial.threaded 
    import threading 
    #import time 
    #from datetime import * 
    #import datetime 
    import time as t 
    from datetime import datetime 

    import sys 




    ser = serial.Serial('COM3',baudrate=9600, timeout=1) 

    def getvalues(): 
     arduionoData=ser.readline().decode('ascii') #('UTF-8')# 
     return arduionoData 

    def realtime(): 
     """Generate time string""" 
     dt0 = datetime.now() 
     dt1 = dt0.replace(minute=1*(int)(dt0.minute),second= 
       (int)(dt0.second),microsecond=0) 
     return dt1.time().strftime('%H:%M:%S') 




     extraction_file = open("C:/Users/gurbir/Desktop/Arduino /accelerometerXonly_jul09a/extraction.txt",  "w") 

     #while(1): 


     extraction_file.write(getvalues()) 
     #extraction_file.write(realtime()) 
     t.sleep(3) #try to collect data for 3 seconds 
     extraction_file.close() 
     sys.exit()       

回答

0

嘗試寫csv文件

import csv 
while(1): 
     with open(r'log.csv', 'a') as f: 
      writer = csv.writer(f) 
      writer.writerow((getvalues())) 
+0

它的工作,但是,有沒有在3秒後退出while循環出優雅的方式? – user3313975