/// <summary> /// 读取数据 /// </summary> /// <param name="channelData1">通道1数据</param> /// <param name="channelData2">通道2数据</param> public void ReadData(out double[] channelData1, out double[] channelData2) { lock (lockObject) { channelData1 = new double[0]; channelData2 = new double[0]; //获取数据 int res = MyDLLimport.AiReadBulkData((int)sampleCount * 2, 1, 2000, g_pBuffer); if (res == 0) { //等待事件 var g_CurrentEventID = MyDLLimport.EventCheck(2000); if (g_CurrentEventID == 1365 || g_CurrentEventID == -1) { return; } channelData1 = new double[sampleCount - invalidDataCount]; channelData2 = new double[sampleCount - invalidDataCount]; unsafe { byte *pData = (byte *)g_pBuffer; for (uint i = invalidDataCount; i < sampleCount; i++) { channelData1[i - invalidDataCount] = (*(pData + i * 2) - currentCHAZero) * currentCHAScale; channelData2[i - invalidDataCount] = (*(pData + i * 2 + 1) - currentCHBZero) * currentCHBScale; } } } } }
/// <summary> /// 读取数据 /// </summary> /// <param name="channelData1">通道1数据</param> /// <param name="channelData2">通道2数据</param> public void ReadDataBlock(out double[] channelData1, out double[] channelData2) { lock (lockObject) { int retryCount = 0; channelData1 = new double[0]; channelData2 = new double[0]; //开始AD采集 MyDLLimport.USBCtrlTransSimple((Int32)0x33); //等待采集完成 while (MyDLLimport.USBCtrlTransSimple((Int32)0x50) != 33) { Thread.Sleep(10); retryCount++; if (retryCount > 1000) { throw new TimeoutException("采集数据超时"); } } //获取数据 int res = MyDLLimport.AiReadBulkData((int)sampleCount * 2, 1, 2000, g_pBuffer); if (res == 0) { //等待事件 var g_CurrentEventID = MyDLLimport.EventCheck(2000); if (g_CurrentEventID == 1365 || g_CurrentEventID == -1) { return; } channelData1 = new double[sampleCount - invalidDataCount]; channelData2 = new double[sampleCount - invalidDataCount]; unsafe { byte *pData = (byte *)g_pBuffer; for (uint i = invalidDataCount; i < sampleCount; i++) { channelData1[i - invalidDataCount] = (*(pData + i * 2) - currentCHAZero) * currentCHAScale; channelData2[i - invalidDataCount] = (*(pData + i * 2 + 1) - currentCHBZero) * currentCHBScale; } } } } }
/// <summary> /// 开始连续采集 /// </summary> public void StartSerialSample(int serialSampleTime) { new Thread(() => { lock (lockObject) { _shouldStop = false; SampleTime = serialSampleTime; IsSerialSampling = true; while (!_shouldStop) { //开始AD采集 MyDLLimport.USBCtrlTransSimple((Int32)0x33); int eventTimeout = 2000; double[] channelData1 = new double[sampleCount]; double[] channelData2 = new double[sampleCount]; uint eventNumber = CaculateEvtNum((int)sampleCount * 2); //单次包的数据长度 uint packetDataLength = sampleCount / eventNumber; //获取数据 int res = MyDLLimport.AiReadBulkData((int)sampleCount * 2, eventNumber, eventTimeout, g_pBuffer); while (!_shouldStop) { //获取当前的事件 int currentEventID = MyDLLimport.EventCheck(eventTimeout); if ((currentEventID == 1365) || (currentEventID == -1)) { break; } if (packetDataLength < invalidDataCount) { throw new Exception("数据异常"); } double[] currentChannel1 = new double[packetDataLength]; double[] currentChannel2 = new double[packetDataLength]; unsafe { byte *pData = (byte *)g_pBuffer; if (currentEventID == 0) { for (uint i = invalidDataCount; i < packetDataLength; i++) { currentChannel1[i] = (*(pData + i * 2) - currentCHAZero) * currentCHAScale; currentChannel2[i] = (*(pData + i * 2 + 1) - currentCHBZero) * currentCHBScale; channelData1[i] = currentChannel1[i]; channelData2[i] = currentChannel2[i]; } } else { for (uint i = 0; i < packetDataLength; i++) { currentChannel1[i - 0] = (*(pData + (packetDataLength * currentEventID + i) * 2) - currentCHAZero) * currentCHAScale; currentChannel2[i - 0] = (*(pData + (packetDataLength * currentEventID + i) * 2 + 1) - currentCHBZero) * currentCHBScale; channelData1[packetDataLength * currentEventID + i] = currentChannel1[i]; channelData2[packetDataLength * currentEventID + i] = currentChannel2[i]; } } } OnScopeReadDataCompleted(channelData1, channelData2, currentChannel1, currentChannel2, (int)eventNumber, currentEventID + 1); if (currentEventID == (eventNumber - 1)) { break; } } } IsSerialSampling = false; } }).Start(); }