[Python][Voice] Write NI DAQ Data to File 儲存NI DAQ的資料到檔案中

這個範例說明如何將NI DAQ卡擷取的資料寫入檔案中
This example introduce how to write data acquired from NI DAQ card to a file.

這裡有5個步驟:
There are five steps:
1. Install nidaqmx package, Python can use this package to read analog signals from NI DAQ card. 2. Use Simulated NI DAQ Device to simulate NI DAQ device. 3. Use matplotlib to plot analog signals from NI DAQ device.
4. Use File open, write and close method to save a file.
5. Use "with open() as file" to save a file. This is a recommendation for file read and write in Python.

#1. 安裝nidaqmx套件,使用Pythone讀取NI DAQ類比訊號。
#2. 使用Simulated NI DAQ Device模擬DAQ卡類比訊號。
#3. 使用matplotlib作互動模式顯示DAQ卡的類比資料。
#4. 使用file open, write, close寫入檔案。
#5. 使用with open() as file寫入檔案,自動呼叫close,(建議使用)。
import nidaqmx
import matplotlib.pyplot as plt
plt.ion() 
i = 0
#file = open('daq_data.txt','a')  #用於追加資料
with open('daq_data.txt','a'as file
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0:1")
        while i < 100:
            aiData = task.read()
            plt.scatter(i,aiData[0],c="r")
            plt.scatter(i,aiData[1],c="b")
            plt.pause(0.05)
            a=str(aiData)             #將Array轉成string
            file.write(a+"\n")        #\n 往下一行
            i=i+1
            print(aiData)

plt.ioff()
plt.show()   
#file.close()



We will share more tutorial videos with people who want to learn LabVIEW and Python welcome to subscribe this channel. 未來會陸續更新影片,讓想學習LabVIEW和Python的朋友可以看影片學習,歡迎訂閱此頻道。 其他網址/related websites: https://labview-tech.blogspot.com/ https://fishark.pixnet.net/blog email: jacklee3633@gmail.com Line: lvnet

留言

這個網誌中的熱門文章

使用VS code開發Python程式 - Matplotlib繪圖

使用Pycharm開發Python程式與用Matplotlib繪圖

[Python][tkinter] Build tkinter GUI 使用tkinter建立圖形視窗