/// <summary> /// 创建DeviceApplication新实例 /// </summary> /// <param name="deviceIndex">设备索引</param> public VirtualOscilloscope(int deviceIndex) { DeviceIndex = deviceIndex; //打开设备 if (HardwareControl.dsoOpenDevice((ushort)deviceIndex) == 0) { throw new Exception("设备打开失败!"); } IsOpen = true; //获取标定数据 calData = Marshal.AllocHGlobal(2 * 32); if (HardwareControl.dsoGetCalLevel((ushort)deviceIndex, calData, 32) == 0) { throw new Exception("读取校正数据失败!"); } //设置参数 CH1VoltageDIV = EVoltageDIV.DIV_1V; CH2VoltageDIV = EVoltageDIV.DIV_1V; TimeDIV = ETimeDIV.DIV_1MSaS; SampleTime = 100; }
/// <summary> /// 读取设备数据 /// </summary> /// <param name="sampleCount">采样数据</param> /// <param name="channel1Array">通道1数据</param> /// <param name="channel2Array">通道2数据</param> /// <param name="triggerPointIndex">触发点位置</param> public void ReadDeviceData(int sampleCount, out short[] channel1Array, out short[] channel2Array, out uint triggerPointIndex) { //加线程锁 lock (deviceLock) { //提取数据 channel1Array = new short[sampleCount]; channel2Array = new short[sampleCount]; triggerPointIndex = 0; //创建内存空间 IntPtr channel1Data = IntPtr.Zero; IntPtr channel2Data = IntPtr.Zero; try { channel1Data = Marshal.AllocHGlobal(sizeof(short) * sampleCount); channel2Data = Marshal.AllocHGlobal(sizeof(short) * sampleCount); for (int i = 0; i < sampleCount / maxSampleCount; i++) { //获取硬件采样数据 if (HardwareControl.dsoReadHardData((ushort)DeviceIndex, IntPtr.Add(channel1Data, i * maxSampleCount * sizeof(short)), IntPtr.Add(channel2Data, i * maxSampleCount * sizeof(short)), (uint)maxSampleCount, calData, (int)CH1VoltageDIV, (int)CH2VoltageDIV, (short)TriggerSweep, (short)TriggerSource, TriggerLevel, (short)TriggerSlope, (int)TimeDIV, HorizontalTriggerPosition, (uint)maxSampleCount, ref triggerPointIndex, (short)InsertMode) != -1) { } } if ((sampleCount % maxSampleCount) != 0) { int surplusCount = sampleCount % maxSampleCount; //获取硬件采样数据 if (HardwareControl.dsoReadHardData((ushort)DeviceIndex, IntPtr.Add(channel1Data, (sampleCount - surplusCount) * sizeof(short)), IntPtr.Add(channel2Data, (sampleCount - surplusCount) * sizeof(short)), (uint)surplusCount, calData, (int)CH1VoltageDIV, (int)CH2VoltageDIV, (short)TriggerSweep, (short)TriggerSource, TriggerLevel, (short)TriggerSlope, (int)TimeDIV, HorizontalTriggerPosition, (uint)surplusCount, ref triggerPointIndex, (short)InsertMode) != -1) { } } //数据拷贝 unsafe { short *ch1 = (short *)channel1Data.ToPointer(); short *ch2 = (short *)channel2Data.ToPointer(); for (int i = 0; i < sampleCount; i++) { channel1Array[i] = ch1[i]; channel2Array[i] = ch2[i]; } } } catch (Exception) { } finally { if (channel1Data != IntPtr.Zero) { Marshal.FreeHGlobal(channel1Data); channel1Data = IntPtr.Zero; } if (channel2Data != IntPtr.Zero) { Marshal.FreeHGlobal(channel2Data); channel2Data = IntPtr.Zero; } } } }