Python的DAQ数据采集

1.DAQ的driver下载 https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019Pf1SAE&l=zh-CN

2.github 里的下载,读使用说明read.me

https://github.com/zsl10100808/nidaqmx-python/blob/master/README.rst

按照操作配置,使用python 相应命令实现数据连续采集

3.python程序

#!/usr/bin/env python3
# from pyqtgraph.Qt import QtCore, QtGui
import sys
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pprint
import nidaqmx
from nidaqmx.constants import AcquisitionType, TaskMode
from nidaqmx.errors import DaqError
import numpy as np
from scipy.io import savemat
from scipy import signal
from time import sleep
import datetime
pp = pprint.PrettyPrinter(indent=4)# print setup
system = nidaqmx.system.System.local()
try:
    dev = system.devices.device_names[1]#according to the device number
    print(dev)

except:
    print("check Nidaqmx driver or python version3.x or DO YOU connect the DAQ?")
    sys.exit()


# plot cure sim

# QtGui.QApplication.setGraphicsSystem('raster')
app = QtGui.QApplication([]) #GUI 
win = pg.GraphicsLayoutWidget(show=True, title="Basic plotting examples")
win.resize(1000, 600) #1000*600 
win.setWindowTitle('pyqtgraph example: Plotting')

# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)

p1 = win.addPlot(title="mmg updating plot")
# define a filter,filter from scipy signal 
b, a = signal.butter(8, [0.01, 0.2], 'bandpass')

filtered_data = {'ch1': [], 'ch2': [], 'ch3': [], 'ch4': []}
"""
setup require task and then get data,dictionary
"""


with nidaqmx.Task() as task:
    task.ai_channels.add_ai_voltage_chan(dev+"/ai0:3")

    task.timing.cfg_samp_clk_timing(
        rate=200, sample_mode=AcquisitionType.CONTINUOUS)
    # freq=1k,continuously get data
    # Python 2.X does not have nonlocal keyword.
    non_local_var = {'ch1': [], 'ch2': [], 'ch3': [], 'ch4': []}

    w_start = 0
    window = 30

    def callback(task_handle, every_n_samples_event_type,
                 number_of_samples, callback_data):
        # print('Every N Samples callback invoked.')

        samples = np.array(task.read(number_of_samples_per_channel=200))
        #tranfer to array
        # print(np.shape(samples))
        non_local_var['ch1'].extend(samples[0, :])
        non_local_var['ch2'].extend(samples[1, :])
        non_local_var['ch3'].extend(samples[2, :])
        non_local_var['ch4'].extend(samples[3, :])
        data_ = signal.filtfilt(b, a, samples)
        filtered_data['ch1'].extend(data_[0, :])
        filtered_data['ch2'].extend(data_[1, :])
        filtered_data['ch3'].extend(data_[2, :])
        filtered_data['ch4'].extend(data_[3, :])
        # print(data_filtered)
        return 0

    task.register_every_n_samples_acquired_into_buffer_event(
        200, callback)

    task.start()

    curve1 = p1.plot(non_local_var['ch1'], pen=(255, 0, 0), name="channel1")
    curve2 = p1.plot(non_local_var['ch2'], pen=(0, 255, 0), name="channel2")
    curve3 = p1.plot(non_local_var['ch3'], pen=(0, 0, 255), name="channel3")
    curve4 = p1.plot(non_local_var['ch4'], pen=(127, 127, 0), name="channel4")
    #ch1 use channel1 ,pen  color

    p1.setXRange(0, 500) # data range 
    start_time = pg.ptime.time()
    win.nextRow()
    p2 = win.addPlot()
    curve1_filtered = p2.plot(
        filtered_data['ch1'], pen=(255, 0, 0), name="channel1")
    curve2_filtered = p2.plot(
        filtered_data['ch2'], pen=(0, 255, 0), name="channel2")
    curve3_filtered = p2.plot(
        filtered_data['ch3'], pen=(0, 0, 255), name="channel3")
    curve4_filtered = p2.plot(
        filtered_data['ch4'], pen=(127, 127, 0), name="channel4")

    p2.setXRange(0, 500)

    def update():
        global curve1, curve2, curve3, curve4, window, p1, w_start, p2, curve1_filtered, curve2_filtered, curve3_filtered, curve4_filtered
        curve1.setData(non_local_var['ch1'][w_start:-1])
        curve2.setData(non_local_var['ch2'][w_start:-1])
        curve3.setData(non_local_var['ch3'][w_start:-1])
        curve4.setData(non_local_var['ch4'][w_start:-1])

        curve1_filtered.setData(filtered_data['ch1'][w_start:-1])
        curve2_filtered.setData(filtered_data['ch2'][w_start:-1])
        curve3_filtered.setData(filtered_data['ch3'][w_start:-1])
        curve4_filtered.setData(filtered_data['ch4'][w_start:-1])

        # acummlite the curve
        # curve1.setData(non_local_var['ch1'])
        # curve2.setData(non_local_var['ch2'])
        # curve3.setData(non_local_var['ch3'])
        # curve4.setData(non_local_var['ch4'])

        w_start = w_start+window
        # p1.enableAutoRange('xy',False)## stop auto scaling
        # print(pg.ptime.time()-start_time)
    timer = QtCore.QTimer()
    timer.timeout.connect(update)# connect
    sleep(0.5)  # delay x s
    print(pg.ptime.time()-start_time)
    timer.start(1000)  # every x ms go to func update
    input('Running task. Press Enter to stop and see number of '
          'accumulated samples.\n')#wait the input to stop 
    task.close()

print(len(non_local_var['ch1']))
today = datetime.date.today()
date = str(today)
try:
    dir_name = input('input a dir where you want to save the file:\n')
    import os
    os.chdir(dir_name)
except FileNotFoundError as e:
    print(e) 

filename = input('input a saving name:\n')
if input("save original data? y/n\n") == 'y':
    savemat('{0}_{1}_filtered.mat'.format(date, filename), non_local_var)
savemat('{0}_{1}.mat'.format(date, filename), filtered_data)

4.完成数据采集

通过输入按键的命令实现数据的task.close 

5.数据的实时显示

上面显示原始数据

下面显示滤波后的数据

注意:

1.下载好必须的python库

2.设置好update频率(太快无法显示)

3.通过定时器实现了update

4.可以通过thread线程优化

该结果用于window,同时适用于树莓派,ubuntu程序

  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
### 回答1: Laview DAQ数据采集是一种应用于工业自动化、科学研究、控制系统等领域的数据采集技术,能够采集和处理来自各种物理和电子信号的信息,并将其转化为数字信号,从而实现对系统、设备和生产线的监测和控制。 通过Laview DAQ数据采集实例,用户可以实现对电压、电流、温度、压力等各种模拟信号的采集和测量。此外,Laview DAQ还支持数字信号的采集,如开关信号、计数器信号等。 Laview DAQ数据采集实例具有多种应用场景,如在工业自动化领域,可以用于系统监测和数据采集;在科学研究领域,可以用于实验数据采集数据处理;在控制系统领域,可以用于工程监控和状态检测等。 总之,Laview DAQ数据采集实例是一种高效、可靠、精准的数据采集技术,为各种工业自动化、科学研究和控制系统的监测和控制提供了强有力的支持。 ### 回答2: Laview DAQ(Data Acquisition)数据采集实例是使用Laview进行数据采集的一个示例。Laview是一款基于G语言的数据处理和分析软件,具有强大的数据采集功能。在实例中,可以使用Laview DAQ模块连接外部设备(如传感器、测量仪器等),并采集实时数据。 以温度测量为例,可以使用Laview DAQ模块连接温度传感器,实时测量环境温度,将采集到的数据通过Laview进行处理和分析。在Laview中,可以设置采样频率、采样时间和采样通道等参数,以得到符合实验要求的数据。同时,Laview还提供了多种数据分析工具和可视化效果,例如实时曲线图、统计分析等功能,帮助用户更好地理解和利用采集到的数据。 除了温度测量,Laview DAQ模块还可以连接其他传感器,如压力传感器、流量计等,实现不同类型信号的采集和处理。采集到的数据可以用于科研实验、质量控制、环境监测等领域。Laview DAQ数据采集实例为用户提供了一个简单易用的数据采集解决方案,可以大大提高实验效率和精度,推动科技进步和工业应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值