public void releaseDevice() { if (cfgPara.hDevice != (IntPtr)(-1)) { NET2991A.NET2991_AI_StopTask(cfgPara.hDevice); NET2991A.NET2991_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); } }
public bool start() { Int32 dwChanCnt = 0; Int32 nIndex = 0; for (nIndex = 0; nIndex < NET2991A.NET2991A_MAX_CHANNELS; nIndex++) { if (cfgPara.AAIParam.CHParam[nIndex].bChannelEn == 1) { dwChanCnt++; } } // 实际总共需要读取的数据数 cfgPara.dwRealReadLen = 2 * dwChanCnt * cfgPara.nReadLength; // 初始化 cfgPara.nSampStatus = UserDef.CMD_UNCPT; cfgPara.dwReadDataSize = 0; cfgPara.bAIStatus = 0; // 读取数据的长度 cfgPara.dwFrameLen = UserDef.LMT_FRMCNT; // 启动采样 if (NET2991A.NET2991_AI_StartTask(cfgPara.hDevice) == 0) { Console.WriteLine("2911A启动任务失败!"); if (cfgPara.hDevice != (IntPtr)(-1)) { NET2991A.NET2991_AI_StopTask(cfgPara.hDevice); NET2991A.NET2991_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); } return(false); } needStop = false; Thread tDataRead = new Thread(ReadDataFun); tDataRead.Start(); return(true); }
public bool InitDevice(string strIP, string strDevPort, string strLocalPort, MsgUpdateDelegate std) { Int32 nIndex = 0; Int32 ipaddr = inet_addr(strIP); cfgPara.AAIParam.nDeviceIP = (UInt32)(System.Net.IPAddress.NetworkToHostOrder(ipaddr)); cfgPara.AAIParam.nDevicePort = Convert.ToUInt16(strDevPort); cfgPara.AAIParam.nLocalPort = Convert.ToUInt16(strLocalPort); cfgPara.AAIParam.CHParam[16].bChannelEn = 0; for (nIndex = 0; nIndex < 16; nIndex++) { cfgPara.AAIParam.CHParam[nIndex].bChannelEn = 1; cfgPara.AAIParam.CHParam[nIndex].nSampleRange = NET2991A.NET2991A_AI_SAMPRANGE_N5_P5V; cfgPara.AAIParam.CHParam[nIndex].nRefGround = NET2991A.NET2991A_AI_REFGND_RSE; } cfgPara.AAIParam.fSampleRate = UserDef.Frequency; //; cfgPara.AAIParam.nSampleMode = NET2991A.NET2991A_AI_SAMPMODE_CONTINUOUS; cfgPara.AAIParam.nSampsPerChan = 102400; cfgPara.AAIParam.nClockSource = NET2991A.NET2991A_AI_CLOCKSRC_LOCAL; cfgPara.AAIParam.nReserved0 = 0; cfgPara.AAIParam.nTriggerSource = NET2991A.NET2991A_AI_TRIGSRC_NONE; cfgPara.AAIParam.nTriggerDir = NET2991A.NET2991A_AI_TRIGDIR_FALLING; cfgPara.AAIParam.fTriggerLevel = 10; cfgPara.AAIParam.nDelaySamps = 0; cfgPara.AAIParam.nReTriggerCount = 1; cfgPara.AAIParam.bMasterEn = 0; cfgPara.AAIParam.nReserved1 = 0; cfgPara.AAIParam.nReserved2 = 0; cfgPara.nReadOffset = 0; cfgPara.nReadLength = 16 * UserDef.Frequency; cfgPara.hDevice = (IntPtr)(-1); UInt16 nDataTranDir = NET2991A.NET2991A_AI_TRANDIR_CLIENT; NET2991A.NET2991_DEV_SetSendSpeed(3); if (cfgPara.hDevice != (IntPtr)(-1)) { NET2991A.NET2991_AI_StopTask(cfgPara.hDevice); NET2991A.NET2991_DEV_Release(cfgPara.hDevice); } // 创建设备 cfgPara.hDevice = NET2991A.NET2991_DEV_Create(cfgPara.AAIParam.nDeviceIP, cfgPara.AAIParam.nDevicePort, cfgPara.AAIParam.nLocalPort, nDataTranDir, 0.2, 0.2, 2); bool isSuccess = true; mstd = std; if (cfgPara.hDevice == (IntPtr)(-1)) { mstd("2991A创建设备失败!"); isSuccess = false; } // 判断是否连接 else if (NET2991A.NET2991_DEV_IsLink(cfgPara.hDevice) == 0) { mstd("2991A设备没有连接!"); isSuccess = false; } // 初始化设备 else if (NET2991A.NET2991_AI_InitTask(cfgPara.hDevice, ref cfgPara.AAIParam, (IntPtr)(-1)) == 0) { mstd("2991A初始化设备失败!"); isSuccess = false; } // 不保存文件 else if (NET2991A.NET2991_AI_IsSaveFile(cfgPara.hDevice, 0) == 0) { mstd("2911A设置文件保存失败!"); isSuccess = false; } // 清除缓存 else if (NET2991A.NET2991_AI_ClearBuffer(cfgPara.hDevice) == 0) { mstd("2991A清除缓存失败!"); } if (!isSuccess && cfgPara.hDevice != (IntPtr)(-1)) { NET2991A.NET2991_AI_StopTask(cfgPara.hDevice); NET2991A.NET2991_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); return(false); } return(true); }
static void ReadDataFun() { UInt16[] nAIArray = new UInt16[2048]; UInt32 dwReadChan = 0; //UInt16[] threaholdMarker = new UInt16[16]; // mark is need to save UInt64 bufferCount = 0; UInt32 indexPerChannel = 0; UInt32 dwReadSampsPerChan = 0; UInt32 dwSampsPerChanRead = 0; UInt32 dwAvailSampsPerChan = 0; double fTimeOut = -1.0; Int32 nIndex = 0; Single fVoltData = 0.0F; Int16 tmp = 0; switch (cfgPara.AAIParam.CHParam[nIndex].nSampleRange) { case NET2991A.NET2991A_AI_SAMPRANGE_N10_P10V: fPerLsb = 20000.0F / 65536; break; case NET2991A.NET2991A_AI_SAMPRANGE_N5_P5V: fPerLsb = 10000.0F / 65536; break; case NET2991A.NET2991A_AI_SAMPRANGE_N2D5_P2D5V: fPerLsb = 5000.0F / 65536; break; case NET2991A.NET2991A_AI_SAMPRANGE_N1D25_P1D25V: fPerLsb = 2500.0F / 65536; break; default: break; } // 根据用户设置的读取长度设置每通道数据的读取长度 UInt32 nChanSize = (uint)(cfgPara.nReadLength * 2); // 读数据 while (true) { try { if (needStop) { break; } dwReadSampsPerChan = 1024; //long t1 = System.DateTime.Now.Ticks; // 连续采集模式下数据排列顺序为16路DI第一个采样点、CH0第一个采样点、CH1第一个采样点、CH2第一采样点.....CH15第一个采样点、16路DI第二个采样点... if (NET2991A.NET2991_AI_ReadBinary(cfgPara.hDevice, ref dwReadChan, nAIArray, dwReadSampsPerChan, ref dwSampsPerChanRead, ref dwAvailSampsPerChan, fTimeOut) == 0) { if (needStop) { break; } continue; } /*long t2 = System.DateTime.Now.Ticks; * Console.WriteLine("wwwwwwwwww " + (t2 - start) / 10000000); * start = t2;*/ if (needStop) { break; } //16通道,采集够1秒数据在处理 //先将数据放进缓存,这里确认每次读回来的数据远小于1秒数据,所以不考虑溢出 for (UInt16 n = 0; n < 1000; n++) { if (((n + 1) << 4) > dwSampsPerChanRead) { break; } int bn = n << 4; for (int i = 0; i < 16; i++) { buffer[currentFrameCount % 2, i, indexPerChannel] = nAIArray[bn + i]; } indexPerChannel++; } bufferCount += dwSampsPerChanRead; if (bufferCount < (ulong)UserDef.Frequency * 16 + 1 - dwSampsPerChanRead) // 500kps*16 { //如果还不够一帧数据 continue; } else { bufferCount = 0; indexPerChannel = 0; currentFrameCount++; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (cfgPara.hDevice != (IntPtr)(-1)) { Thread.Sleep(500); NET2991A.NET2991_AI_StopTask(cfgPara.hDevice); NET2991A.NET2991_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); } }