NiDA 1.0.3
dotnet add package NiDA --version 1.0.3
NuGet\Install-Package NiDA -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NiDA" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NiDA" Version="1.0.3" />
<PackageReference Include="NiDA" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NiDA --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NiDA, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NiDA@1.0.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NiDA&version=1.0.3
#tool nuget:?package=NiDA&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NiDA 使用说明文档
目录
项目结构
NiDataAcquisition/
├── NiDA/ # 核心库项目
│ ├── Config/ # 配置相关
│ │ ├── ConfigManager.cs # 配置管理器
│ │ └── DeviceConfig.cs # 设备配置类
│ ├── Core/ # 核心功能
│ │ ├── DeviceManager.cs # 设备管理器(单例)
│ │ ├── INiDevice.cs # 设备接口
│ │ └── NiDevice.cs # 设备实现
│ ├── Events/ # 事件定义
│ ├── Exceptions/ # 异常定义和日志
│ ├── Utils/ # 工具类
│ └── NiDA.csproj # 库项目文件
├── NiDataAcquisitionTestApp/ # Windows Forms 测试应用
│ ├── Controls/ # 用户控件
│ ├── MainForm.cs # 主窗体
│ └── Program.cs # 程序入口
├── libs/ # NI 驱动 DLL
└── NiDataAcquisition.sln # 解决方案文件
概述
NiDA(National Instruments Data Acquisition)是一个基于 .NET 8.0 的 NI-DAQmx 驱动封装库,专注于简化和规范 NI 数据采集设备的使用流程。
该库提供了设备连接、数据采集、模拟输出、数据平滑、数据映射等核心功能,适用于工业自动化、实验室测量、传感器数据采集等应用场景。
功能特性
- 设备管理:自动发现可用 NI 设备,获取设备信息
- 设备管理器:单例设备管理器,提供全局设备访问和事件转发
- 接口抽象:INiDevice 接口定义设备标准操作,便于扩展和测试
- 模拟输入:支持单次采样和连续采集模式
- 模拟输出:支持单点输出和波形输出
- 数据缓存:线程安全的环形数据缓存区
- 数据处理:提供平均值、标准差、最大值、最小值、峰峰值等统计计算
- 数据归一化/标准化:支持数据归一化到 [0,1] 范围和零均值标准化
- 事件驱动:提供数据采集、状态变化、错误发生等事件通知
- 配置管理:支持配置的序列化保存和加载
- 日志记录:自动记录错误日志到文件
系统要求
- 操作系统:Windows 10/11
- .NET 版本:.NET 8.0 或更高版本
- 驱动要求:NI-DAQmx 驱动已安装
- 硬件:支持 NI-DAQmx 的 NI 设备(如 USB-6001/6002/6003 等)
快速开始
1. 基本使用流程
using NiDA.Config;
using NiDA.Core;
// 步骤1:创建设备配置
DeviceConfig config = new DeviceConfig
{
DeviceName = "Dev1",
InputChannel = "ai0",
OutputChannel = "ao0",
SampleRate = 1000,
SampleCount = 100
};
// 步骤2:使用配置初始化设备
NiDevice device = new NiDevice(config);
// 步骤3:初始化设备
if (device.Initialize())
{
// 步骤4:开始数据采集
device.StartAcquisition();
// 步骤5:处理数据(通过事件或缓存)
// ...
// 步骤6:停止采集
device.StopAcquisition();
}
// 步骤7:释放资源
device.Dispose();
2. 使用事件处理数据
// 订阅数据采集事件
device.DataCollected += (sender, e) =>
{
double[] rawData = e.RawData; // 原始电压数据
double[] mappedData = e.MappedData; // 映射后的物理量数据
DateTime timestamp = e.Timestamp; // 采集时间戳
};
// 订阅状态变化事件
device.StatusChanged += (sender, e) =>
{
Console.WriteLine($"状态变化: {e.OldStatus} -> {e.NewStatus}");
Console.WriteLine($"消息: {e.Message}");
};
// 订阅错误事件
device.ErrorOccurred += (sender, e) =>
{
Console.WriteLine($"错误: {e.Message}");
Console.WriteLine($"错误码: {e.ErrorCode}");
Console.WriteLine($"建议: {e.RecoverySuggestion}");
};
3. 使用 DeviceManager(推荐)
using NiDA.Config;
using NiDA.Core;
using NiDA.Utils;
// 步骤1:获取单例管理器
DeviceManager manager = DeviceManager.Instance;
// 步骤2:发现可用设备
List<string> devices = DeviceDiscovery.GetAvailableDevices();
string deviceName = devices.FirstOrDefault() ?? "Dev1";
// 步骤3:创建设备配置
DeviceConfig config = new DeviceConfig
{
DeviceName = deviceName,
InputChannel = "ai0",
SampleRate = 1000,
SampleCount = 100
};
// 步骤4:通过管理器创建设备
if (manager.CreateDevice(config))
{
// 步骤5:订阅管理器事件
manager.DeviceDataCollected += (sender, e) =>
{
// 处理采集到的数据
DataStatistics stats = DataUtils.GetStatistics(e.RawData);
Console.WriteLine($"采集数据 - 平均: {stats.Mean:F3}V");
};
// 步骤6:开始采集
manager.StartAcquisition();
// 采集一段时间...
// 步骤7:停止采集
manager.StopAcquisition();
}
// 步骤8:释放资源(或让程序结束时自动清理)
manager.DisposeDevice();
API 参考
DeviceManager 类
单例设备管理器,提供全局设备访问和事件转发功能。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
Instance |
DeviceManager |
获取单例实例 |
HasDevice |
bool |
是否已创建设备 |
方法
| 方法 | 说明 |
|---|---|
GetDevice() |
获取当前设备实例 |
CreateDevice(config) |
创建并初始化设备 |
InitializeDevice() |
初始化设备 |
StartAcquisition() |
开始数据采集 |
StopAcquisition() |
停止数据采集 |
DisposeDevice() |
释放设备资源 |
事件
| 事件 | 说明 |
|---|---|
DeviceStatusChanged |
设备状态变化时触发 |
DeviceDataCollected |
数据采集完成时触发 |
DeviceErrorOccurred |
设备错误发生时触发 |
DeviceChanged |
设备变更时触发 |
使用示例
using NiDA.Config;
using NiDA.Core;
// 获取单例管理器
DeviceManager manager = DeviceManager.Instance;
// 创建设备
DeviceConfig config = new DeviceConfig { DeviceName = "Dev1" };
if (manager.CreateDevice(config))
{
// 订阅事件
manager.DeviceDataCollected += (sender, e) => {
Console.WriteLine($"采集到 {e.RawData.Length} 个数据点");
};
// 开始采集
manager.StartAcquisition();
}
INiDevice 接口
NI 设备操作标准接口,定义设备的核心操作。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
Status |
DeviceStatus |
当前设备状态 |
Config |
DeviceConfig |
当前设备配置 |
DataCache |
ConcurrentQueue<double> |
数据缓存队列 |
CacheCount |
int |
缓存中的数据数量 |
方法
| 方法 | 说明 |
|---|---|
Initialize() |
初始化设备,建立连接 |
StartAcquisition() |
开始连续数据采集 |
StopAcquisition() |
停止数据采集 |
ReadSingleSample() |
同步采集一次数据 |
ReadSingleSampleAsync() |
异步采集一次数据 |
WriteAnalogOutput(value, useMappedValue) |
写入单点模拟输出 |
WriteAnalogOutputAsync(value, useMappedValue) |
异步写入单点模拟输出 |
WriteAnalogOutputWaveform(values, useMappedValue) |
写入模拟输出波形 |
WriteAnalogOutputWaveformAsync(values, useMappedValue) |
异步写入模拟输出波形 |
GetCachedData() |
获取所有缓存数据 |
ClearCache() |
清空数据缓存 |
UpdateConfig(config) |
更新设备配置 |
Dispose() |
释放设备资源 |
事件
| 事件 | 说明 |
|---|---|
DataCollected |
数据采集完成时触发 |
StatusChanged |
设备状态变化时触发 |
ErrorOccurred |
发生错误时触发 |
NiDevice 类
设备操作核心类,提供所有设备交互功能。
构造函数
| 构造函数 | 说明 |
|---|---|
NiDevice() |
使用默认配置创建设备实例 |
NiDevice(DeviceConfig config) |
使用指定配置创建设备实例 |
属性
| 属性 | 类型 | 说明 |
|---|---|---|
Status |
DeviceStatus |
当前设备状态 |
Config |
DeviceConfig |
当前设备配置 |
DataCache |
ConcurrentQueue<double> |
数据缓存队列 |
CacheCount |
int |
缓存中的数据数量 |
方法
| 方法 | 说明 |
|---|---|
Initialize() |
初始化设备,建立连接 |
StartAcquisition() |
开始连续数据采集 |
StopAcquisition() |
停止数据采集 |
ReadSingleSample() |
同步采集一次数据 |
ReadSingleSampleAsync() |
异步采集一次数据 |
WriteAnalogOutput(value, useMappedValue) |
写入单点模拟输出 |
WriteAnalogOutputAsync(value, useMappedValue) |
异步写入单点模拟输出 |
WriteAnalogOutputWaveform(values, useMappedValue) |
写入模拟输出波形 |
WriteAnalogOutputWaveformAsync(values, useMappedValue) |
异步写入模拟输出波形 |
GetCachedData() |
获取所有缓存数据 |
ClearCache() |
清空数据缓存 |
UpdateConfig(config) |
更新设备配置 |
Dispose() |
释放设备资源 |
事件
| 事件 | 说明 |
|---|---|
DataCollected |
数据采集完成时触发 |
StatusChanged |
设备状态变化时触发 |
ErrorOccurred |
发生错误时触发 |
配置指南
DeviceConfig 配置项
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
DeviceName |
string | "Dev1" | 设备名称 |
InputChannel |
string | "ai0" | 模拟输入通道 |
OutputChannel |
string | "ao0" | 模拟输出通道 |
SampleRate |
double | 1000 | 采样率(Hz) |
SampleCount |
int | 100 | 每次采集的样本数 |
InputMinVoltage |
double | -10.0 | 输入电压范围最小值(V) |
InputMaxVoltage |
double | 10.0 | 输入电压范围最大值(V) |
OutputMinVoltage |
double | -10.0 | 输出电压范围最小值(V) |
OutputMaxVoltage |
double | 10.0 | 输出电压范围最大值(V) |
TerminalConfiguration |
string | "Rse" | 终端配置(Rse/Diff/Nrse/PseudoDiff) |
CacheSize |
int | 10000 | 数据缓存大小 |
EnableMapping |
bool | false | 是否启用数据映射 |
MappingSlope |
double | 1.0 | 映射斜率 |
MappingIntercept |
double | 0.0 | 映射截距 |
MappingUnit |
string | "" | 映射单位(如 "°C") |
EnableSmoothing |
bool | false | 是否启用数据平滑 |
SmoothingAlgorithm |
enum | MovingAverage | 平滑算法类型 |
SmoothingWindowSize |
int | 5 | 平滑窗口大小 |
SmoothingSigma |
double | 1.0 | 高斯滤波标准差 |
配置保存和加载
using NiDA.Config;
// 保存配置到文件
DeviceConfig config = new DeviceConfig { DeviceName = "Dev1" };
ConfigManager.SaveConfig(config, "my_config.json");
// 从文件加载配置
DeviceConfig loadedConfig = ConfigManager.LoadConfig("my_config.json");
// 检查配置文件是否存在
bool exists = ConfigManager.ConfigExists("my_config.json");
// 验证配置文件有效性
bool isValid = ConfigManager.ValidateConfigFile("my_config.json");
// 重置为默认配置
DeviceConfig defaultConfig = ConfigManager.ResetToDefault();
// 克隆配置
DeviceConfig clonedConfig = ConfigManager.CloneConfig(config);
预设配置
// 获取默认配置
DeviceConfig defaultConfig = DeviceConfig.CreateDefault();
// 创建高精度配置(高采样率场景)
DeviceConfig highPrecisionConfig = DeviceConfig.CreateHighPrecision();
// SampleRate = 10000, SampleCount = 1000, CacheSize = 100000
// 创建低功耗配置(低采样率场景)
DeviceConfig lowPowerConfig = DeviceConfig.CreateLowPower();
// SampleRate = 100, SampleCount = 10, CacheSize = 1000
事件处理
DataCollectedEventArgs
采集到新数据时触发的事件参数。
public class DataCollectedEventArgs : EventArgs
{
public double[] RawData { get; } // 原始电压数据
public double[] MappedData { get; } // 映射后的数据(如果有)
public DateTime Timestamp { get; } // 采集时间戳
public bool HasMappedData { get; } // 是否有映射数据
}
StatusChangedEventArgs
设备状态变化时触发的事件参数。
public class StatusChangedEventArgs : EventArgs
{
public DeviceStatus NewStatus { get; } // 新状态
public DeviceStatus OldStatus { get; } // 旧状态
public string Message { get; } // 状态消息
}
ErrorOccurredEventArgs
发生错误时触发的事件参数。
public class ErrorOccurredEventArgs : EventArgs
{
public string Message { get; } // 错误消息
public ErrorType ErrorType { get; } // 错误类型
public int ErrorCode { get; } // 错误码
public string RecoverySuggestion { get; } // 恢复建议
}
设备状态枚举
public enum DeviceStatus
{
Uninitialized, // 未初始化
Initialized, // 已初始化
Connected, // 已连接
Acquiring, // 正在采集
Stopped, // 已停止
Error // 错误状态
}
错误处理
异常类型
| 异常类型 | 说明 |
|---|---|
AcquisitionException |
数据采集相关异常 |
ConfigurationException |
配置相关异常 |
ConnectionException |
设备连接相关异常 |
DeviceException |
设备操作基类异常 |
MappingException |
数据映射相关异常 |
异常处理示例
try
{
DeviceConfig config = ConfigManager.LoadConfig();
NiDevice device = new NiDevice(config);
if (!device.Initialize())
{
Console.WriteLine("设备初始化失败");
return;
}
device.StartAcquisition();
}
catch (AcquisitionException ex)
{
Console.WriteLine($"采集异常: {ex.Message}");
ErrorLogger.LogException(ex);
}
catch (ConnectionException ex)
{
Console.WriteLine($"连接异常: {ex.Message}");
ErrorLogger.LogException(ex);
}
catch (ConfigurationException ex)
{
Console.WriteLine($"配置异常: {ex.Message}");
ErrorLogger.LogException(ex);
}
catch (Exception ex)
{
Console.WriteLine($"未知异常: {ex.Message}");
ErrorLogger.LogException(ex);
}
日志文件
错误日志自动记录到 Logs/NiDataAcquisition_YYYYMMDD.log 文件。
// 手动记录错误
ErrorLogger.LogError(ErrorLogger.ErrorType.Connection, "连接失败", ex);
// 手动记录信息
ErrorLogger.LogInfo("设备初始化成功");
// 获取当前日志文件路径
string logPath = ErrorLogger.GetLogFilePath();
数据平滑
支持的平滑算法
| 算法 | 枚举值 | 说明 |
|---|---|---|
| 移动平均 | MovingAverage |
计算窗口内数据的平均值 |
| 中值滤波 | Median |
取窗口内数据的中值 |
| 高斯滤波 | Gaussian |
高斯权重加权平均 |
使用数据平滑
// 方法1:通过配置启用平滑
DeviceConfig config = new DeviceConfig
{
EnableSmoothing = true,
SmoothingAlgorithm = SmoothingAlgorithm.MovingAverage,
SmoothingWindowSize = 5
};
NiDevice device = new NiDevice(config);
// 方法2:直接设置平滑器
device.DataSmoother = new MovingAverageSmoother { WindowSize = 5 };
// 或
device.DataSmoother = new MedianSmoother { WindowSize = 5 };
// 或
device.DataSmoother = new GaussianSmoother { WindowSize = 5, Sigma = 1.0 };
ISmoother 接口
public interface ISmoother
{
int WindowSize { get; set; } // 平滑窗口大小
double Smooth(double[] data); // 平滑单个值(重载版本)
double[] SmoothArray(double[] data); // 平滑数组
}
数据映射
数据映射功能将原始电压值转换为物理量值。
映射公式
物理量 = 电压 × 斜率 + 截距
电压 = (物理量 - 截距) / 斜率
使用数据映射
// 方法1:通过配置启用映射
DeviceConfig config = new DeviceConfig
{
EnableMapping = true,
MappingSlope = 100.0, // 例如:电压到温度的斜率
MappingIntercept = -273.15, // 例如:转换为摄氏度
MappingUnit = "°C" // 单位
};
NiDevice device = new NiDevice(config);
// 方法2:直接设置映射器
device.DataMapper = new LinearDataMapper(
slope: 100.0,
intercept: -273.15,
unit: "°C",
isEnabled: true
);
// 手动映射
double voltage = 2.73; // 输入电压
double temperature = device.DataMapper.Map(voltage); // 转换为温度
// 反向映射(物理量转电压)
double outputVoltage = device.DataMapper.ReverseMap(25.0); // 25°C 转电压
IDataMapper 接口
public interface IDataMapper
{
bool IsEnabled { get; set; } // 是否启用映射
string Unit { get; set; } // 物理量单位
double Map(double rawValue); // 单值映射
double ReverseMap(double mappedValue); // 单值反向映射
double[] MapArray(double[] rawValues); // 批量映射
double[] ReverseMapArray(double[] mappedValues); // 批量反向映射
}
自定义映射器
// 实现 IDataMapper 接口
public class CustomDataMapper : IDataMapper
{
public bool IsEnabled { get; set; } = true;
public string Unit { get; set; } = "";
public double Map(double rawValue)
{
// 自定义映射逻辑
return Math.Pow(rawValue, 2) * 10;
}
public double ReverseMap(double mappedValue)
{
// 自定义反向映射逻辑
return Math.Sqrt(mappedValue / 10);
}
public double[] MapArray(double[] rawValues)
{
return rawValues.Select(Map).ToArray();
}
public double[] ReverseMapArray(double[] mappedValues)
{
return mappedValues.Select(ReverseMap).ToArray();
}
}
// 使用自定义映射器
device.DataMapper = new CustomDataMapper { IsEnabled = true, Unit = "custom" };
数据处理工具
DataUtils 类
提供常用的数据处理和统计计算功能。
统计计算方法
| 方法 | 说明 |
|---|---|
CalculateMean(data) |
计算数据平均值 |
CalculateStdDev(data) |
计算数据标准差 |
CalculateMax(data) |
计算数据最大值 |
CalculateMin(data) |
计算数据最小值 |
CalculatePeakToPeak(data) |
计算峰峰值 |
GetStatistics(data) |
获取完整统计信息 |
数据处理方法
| 方法 | 说明 |
|---|---|
Normalize(data, min, max) |
数据归一化到 [0, 1] 范围 |
Standardize(data, mean, stdDev) |
数据标准化(零均值) |
MovingAverage(data, windowSize) |
移动平均滤波 |
DataStatistics 类
| 属性 | 类型 | 说明 |
|---|---|---|
Count |
int |
数据点数量 |
Mean |
double |
平均值 |
StdDev |
double |
标准差 |
Min |
double |
最小值 |
Max |
double |
最大值 |
PeakToPeak |
double |
峰峰值 |
使用示例
using NiDA.Utils;
double[] data = { 1.0, 2.0, 3.0, 4.0, 5.0 };
// 计算统计信息
double mean = DataUtils.CalculateMean(data);
double stdDev = DataUtils.CalculateStdDev(data);
// 获取完整统计
DataStatistics stats = DataUtils.GetStatistics(data);
Console.WriteLine($"平均: {stats.Mean}, 标准差: {stats.StdDev}");
// 归一化数据
double[] normalized = DataUtils.Normalize(data);
// 标准化数据
double[] standardized = DataUtils.Standardize(data);
设备发现工具
DeviceDiscovery 类
提供 NI 设备发现和信息获取功能。
方法
| 方法 | 说明 |
|---|---|
GetAvailableDevices() |
获取可用设备列表 |
DeviceExists(deviceName) |
检查设备是否存在 |
GetDeviceInfo(deviceName) |
获取设备详细信息 |
DeviceInfo 类
| 属性 | 类型 | 说明 |
|---|---|---|
Name |
string |
设备名称 |
ProductType |
string |
产品类型 |
SerialNumber |
string |
序列号 |
AnalogInputChannels |
int |
模拟输入通道数 |
AnalogOutputChannels |
int |
模拟输出通道数 |
DigitalInputChannels |
int |
数字输入通道数 |
DigitalOutputChannels |
int |
数字输出通道数 |
使用示例
using NiDA.Utils;
// 获取可用设备列表
List<string> devices = DeviceDiscovery.GetAvailableDevices();
foreach (string deviceName in devices)
{
Console.WriteLine($"设备: {deviceName}");
}
// 获取设备信息
if (DeviceDiscovery.DeviceExists("Dev1"))
{
DeviceInfo info = DeviceDiscovery.GetDeviceInfo("Dev1");
Console.WriteLine($"产品: {info.ProductType}");
Console.WriteLine($"AI 通道: {info.AnalogInputChannels}");
Console.WriteLine($"AO 通道: {info.AnalogOutputChannels}");
}
最佳实践
1. 资源管理
// 使用 using 语句确保资源正确释放
using (NiDevice device = new NiDevice(config))
{
device.Initialize();
device.StartAcquisition();
// 处理数据...
}
// 设备资源在这里自动释放
2. 线程安全
// DataCache 是线程安全的 ConcurrentQueue
device.DataCollected += (sender, e) =>
{
// 可以安全地从多个线程访问
double[] data = device.GetCachedData();
};
// 使用缓存数据时建议复制
device.DataCollected += (sender, e) =>
{
double[] dataCopy = device.GetCachedData().ToArray();
// 处理副本...
};
3. 配置验证
// 保存配置前验证
if (config.IsValid())
{
ConfigManager.SaveConfig(config);
}
else
{
Console.WriteLine("配置无效,请检查参数");
}
4. 异步操作
// 使用异步方法避免阻塞 UI 线程
private async Task StartAcquisitionAsync()
{
await Task.Run(() =>
{
device.StartAcquisition();
});
// 或使用异步单次采集
double[] data = await device.ReadSingleSampleAsync();
}
5. 错误恢复
device.ErrorOccurred += (sender, e) =>
{
if (e.RecoverySuggestion != null)
{
Console.WriteLine($"建议: {e.RecoverySuggestion}");
}
// 根据错误类型执行恢复
switch (e.ErrorType)
{
case ErrorType.Connection:
// 尝试重新连接
break;
case ErrorType.Acquisition:
// 尝试重启采集
break;
}
};
完整示例
完整数据采集示例
using NiDA.Config;
using NiDA.Core;
using NiDA.Events;
using System.Collections.Concurrent;
class DataAcquisitionExample
{
private static ConcurrentQueue<double> dataBuffer = new();
public static void Run()
{
// 1. 创建并配置设备
DeviceConfig config = new DeviceConfig
{
DeviceName = "Dev1",
InputChannel = "ai0",
OutputChannel = "ao0",
SampleRate = 1000,
SampleCount = 100,
EnableMapping = true,
MappingSlope = 100.0,
MappingIntercept = 0,
MappingUnit = "mV",
EnableSmoothing = true,
SmoothingAlgorithm = SmoothingAlgorithm.MovingAverage,
SmoothingWindowSize = 5
};
// 2. 创建设备实例
using (NiDevice device = new NiDevice(config))
{
// 3. 订阅事件
device.DataCollected += OnDataCollected;
device.StatusChanged += OnStatusChanged;
device.ErrorOccurred += OnErrorOccurred;
// 4. 初始化设备
if (!device.Initialize())
{
Console.WriteLine("设备初始化失败");
return;
}
// 5. 开始采集
Console.WriteLine("开始数据采集...");
device.StartAcquisition();
// 6. 等待采集数据
Thread.Sleep(5000);
// 7. 停止采集
device.StopAcquisition();
// 8. 获取并处理缓存数据
double[] cachedData = device.GetCachedData();
Console.WriteLine($"缓存数据点数: {cachedData.Length}");
}
// 9. 设备资源自动释放
}
private static void OnDataCollected(object sender, DataCollectedEventArgs e)
{
foreach (double value in e.RawData)
{
dataBuffer.Enqueue(value);
}
if (e.HasMappedData)
{
double avgMapped = e.MappedData.Average();
Console.WriteLine($"平均映射值: {avgMapped:F2} {e.MappedData.Length}");
}
}
private static void OnStatusChanged(object sender, StatusChangedEventArgs e)
{
Console.WriteLine($"状态: {e.OldStatus} -> {e.NewStatus}");
}
private static void OnErrorOccurred(object sender, ErrorOccurredEventArgs e)
{
Console.WriteLine($"错误 [{e.ErrorCode}]: {e.Message}");
if (e.RecoverySuggestion != null)
{
Console.WriteLine($"建议: {e.RecoverySuggestion}");
}
}
}
模拟输出示例
public static void AnalogOutputExample()
{
DeviceConfig config = new DeviceConfig
{
DeviceName = "Dev1",
OutputChannel = "ao0",
OutputMinVoltage = -10.0,
OutputMaxVoltage = 10.0
};
using (NiDevice device = new NiDevice(config))
{
device.Initialize();
// 单点输出(直接电压值)
device.WriteAnalogOutput(2.5); // 输出 2.5V
// 单点输出(使用映射值)
device.WriteAnalogOutput(25.0, useMappedValue: true); // 假设映射系数为 0.1,输出 2.5V
// 波形输出
double[] waveform = Enumerable.Range(0, 100)
.Select(i => 5.0 * Math.Sin(2 * Math.PI * i / 100))
.ToArray();
device.WriteAnalogOutputWaveform(waveform);
device.Dispose();
}
}
构建和运行
构建项目
- 确保已安装 .NET 8.0 SDK
- 在解决方案根目录执行:
dotnet build NiDataAcquisition.sln
运行测试应用
cd NiDataAcquisitionTestApp
dotnet run
依赖项
- Newtonsoft.Json: 13.0.4
- NationalInstruments.Common: NI 基础库
- NationalInstruments.DAQmx: NI 数据采集驱动
版本信息
- 版本: 1.0.3
- 框架: .NET 8.0
- 依赖: NationalInstruments.DAQmx, Newtonsoft.Json
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Newtonsoft.Json (>= 13.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.