public void releaseDevice() { if (cfgPara.hDevice != (IntPtr)(-1)) { NET2991B.NET2991B_AI_StopTask(cfgPara.hDevice); NET2991B.NET2991B_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); } }
public bool start() { Int32 dwChanCnt = 0; Int32 nIndex = 0; for (nIndex = 0; nIndex < NET2991B.NET2991B_MAX_CHANNELS; nIndex++) { if (cfgPara.BAIParam.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 (NET2991B.NET2991B_AI_StartTask(cfgPara.hDevice) == 0) { Console.WriteLine("启动任务失败!"); if (cfgPara.hDevice != (IntPtr)(-1)) { NET2991B.NET2991B_AI_StopTask(cfgPara.hDevice); NET2991B.NET2991B_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.BAIParam.nDeviceIP = (UInt32)(System.Net.IPAddress.NetworkToHostOrder(ipaddr)); cfgPara.BAIParam.nDevicePort = Convert.ToUInt16(strDevPort); cfgPara.BAIParam.nLocalPort = Convert.ToUInt16(strLocalPort); cfgPara.BAIParam.CHParam[0].bChannelEn = 0; for (nIndex = 1; nIndex < 17; nIndex++) { cfgPara.BAIParam.CHParam[nIndex].bChannelEn = 1; cfgPara.BAIParam.CHParam[nIndex].nSampleRange = NET2991B.NET2991B_AI_SAMPRANGE_N10_P10V; cfgPara.BAIParam.CHParam[nIndex].nRefGround = NET2991B.NET2991B_AI_REFGND_RSE; } cfgPara.BAIParam.fSampleRate = 500000; cfgPara.BAIParam.nSampleMode = NET2991B.NET2991B_AI_SAMPMODE_CONTINUOUS; cfgPara.BAIParam.nSampsPerChan = 102400; cfgPara.BAIParam.nClockSource = NET2991B.NET2991B_AI_CLOCKSRC_LOCAL; cfgPara.BAIParam.nReserved0 = 0; cfgPara.BAIParam.nTriggerSource = NET2991B.NET2991B_AI_TRIGSRC_NONE; cfgPara.BAIParam.nTriggerDir = NET2991B.NET2991B_AI_TRIGDIR_FALLING; cfgPara.BAIParam.fTriggerLevel = 10; cfgPara.BAIParam.nDelaySamps = 0; cfgPara.BAIParam.nReTriggerCount = 1; cfgPara.BAIParam.bMasterEn = 0; cfgPara.BAIParam.nReserved1 = 0; cfgPara.BAIParam.nReserved2 = 0; cfgPara.nReadOffset = 0; cfgPara.nReadLength = 10240000; cfgPara.hDevice = (IntPtr)(-1); UInt16 nDataTranDir = NET2991B.NET2991B_AI_TRANDIR_CLIENT; NET2991B.NET2991B_DEV_SetSendSpeed(3); if (cfgPara.hDevice != (IntPtr)(-1)) { NET2991B.NET2991B_AI_StopTask(cfgPara.hDevice); NET2991B.NET2991B_DEV_Release(cfgPara.hDevice); } // 创建设备 cfgPara.hDevice = NET2991B.NET2991B_DEV_Create(cfgPara.BAIParam.nDeviceIP, cfgPara.BAIParam.nDevicePort, cfgPara.BAIParam.nLocalPort, nDataTranDir, 0.2, 0.2, 2); bool isSuccess = true; mstd = std; if (cfgPara.hDevice == (IntPtr)(-1)) { mstd("2991A创建设备失败!"); isSuccess = false; } // 判断是否连接 else if (NET2991B.NET2991B_DEV_IsLink(cfgPara.hDevice) == 0) { mstd("2991A设备没有连接!"); isSuccess = false; } // 初始化设备 else if (NET2991B.NET2991B_AI_InitTask(cfgPara.hDevice, ref cfgPara.BAIParam, (IntPtr)(-1)) == 0) { mstd("2991A初始化设备失败!"); isSuccess = false; } // 不保存文件 else if (NET2991B.NET2991B_AI_IsSaveFile(cfgPara.hDevice, 0) == 0) { mstd("2911A设置文件保存失败!"); isSuccess = false; } // 清除缓存 else if (NET2991B.NET2991B_AI_ClearBuffer(cfgPara.hDevice) == 0) { mstd("2991A清除缓存失败!"); } if (!isSuccess && cfgPara.hDevice != (IntPtr)(-1)) { NET2991B.NET2991B_AI_StopTask(cfgPara.hDevice); NET2991B.NET2991B_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); return(false); } return(true); }
static void ReadDataFun() { UInt16[] nAIArray = new UInt16[2048]; UInt32 dwReadChan = 0; UInt16[,] buffer = new UInt16[16, 500000]; //16 channel, 500k/s 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; Single fPerLsb = 0.0F; Int16 tmp = 0; switch (cfgPara.BAIParam.CHParam[nIndex].nSampleRange) { case NET2991B.NET2991B_AI_SAMPRANGE_N10_P10V: fPerLsb = 20000.0F / 65536; break; case NET2991B.NET2991B_AI_SAMPRANGE_N5_P5V: fPerLsb = 10000.0F / 65536; break; case NET2991B.NET2991B_AI_SAMPRANGE_N2D5_P2D5V: fPerLsb = 5000.0F / 65536; break; case NET2991B.NET2991B_AI_SAMPRANGE_N1D25_P1D25V: fPerLsb = 2500.0F / 65536; break; default: break; } // 根据用户设置的读取长度设置每通道数据的读取长度 UInt32 nChanSize = (uint)(cfgPara.nReadLength * 2); // 读数据 while (true) { try { if (needStop) { break; } dwReadSampsPerChan = 0; // 连续采集模式下数据排列顺序为16路DI第一个采样点、CH0第一个采样点、CH1第一个采样点、CH2第一采样点.....CH15第一个采样点、16路DI第二个采样点... if (NET2991B.NET2991B_AI_ReadBinary(cfgPara.hDevice, ref dwReadChan, nAIArray, dwReadSampsPerChan, ref dwSampsPerChanRead, ref dwAvailSampsPerChan, fTimeOut) == 0) { if (needStop) { break; } continue; } if (needStop) { break; } //16通道,采集够1秒数据在处理 bufferCount += dwSampsPerChanRead; if (bufferCount < 8000000) { for (UInt16 n = 0; n < 1000; n++) { if (((n + 1) << 4) > dwSampsPerChanRead) { break; } int bn = n << 4; for (int i = 8; i < 16; i++)//A0-A7 没有接收据 { buffer[i, indexPerChannel] = nAIArray[bn + i]; if (threaholdMarker[i] == 0 && (nAIArray[bn + i] > UserDef.d9003[i].max_th || nAIArray[bn + i] < UserDef.d9003[i].min_th)) { threaholdMarker[i] = 1; } if (nAIArray[bn + i] > UserDef.d9003[i].max) { UserDef.d9003[i].max = nAIArray[bn + i]; } if (nAIArray[bn + i] < UserDef.d9003[i].min) { UserDef.d9003[i].min = nAIArray[bn + i]; } } indexPerChannel++; } } else { bufferCount = 0; indexPerChannel = 0; k_length = 0; TimeSpan ts = DateTime.Now - k_lastSavedFileTime; UInt16 patternChecker = 0; for (UInt16 i = 8; i < 16; i++) { UserDef.d9003[i].max = 0; //归0,重新计算 UserDef.d9003[i].min = 65536; if (threaholdMarker[i] > 0) { k_buffer[k_length] = i; k_length++; for (int j = 0; j < 500000; j++) { k_buffer[k_length] = buffer[i, j]; k_length++; } patternChecker |= (UInt16)(1 << i); } if (ts.TotalSeconds > 6000) { threaholdMarker[i] = 1; } else { threaholdMarker[i] = 0; } } if (k_length > 0 && (patternChecker != k_last_pattern_check || ts.TotalSeconds > k_timer_threahold)) { Thread tDataSave = new Thread(saveFile); tDataSave.Start(); k_lastSavedFileTime = DateTime.Now; if (ts.TotalSeconds < 6000) { k_last_pattern_check = patternChecker; } if (ts.TotalSeconds < 6000) { k_timer_threahold = k_timer_threahold * 2; } else if (ts.TotalSeconds > 20000) { k_timer_threahold = 1; } } } } catch (Exception ex) { } } if (cfgPara.hDevice != (IntPtr)(-1)) { Thread.Sleep(500); NET2991B.NET2991B_AI_StopTask(cfgPara.hDevice); NET2991B.NET2991B_DEV_Release(cfgPara.hDevice); cfgPara.hDevice = (IntPtr)(-1); } }
public NET2991B() { cfgPara.BAIParam.szDevName = new SByte[32]; cfgPara.BAIParam.CHParam = new NET2991B.NET2991B_CH_PARAM[17]; NET2991B.NET2991B_DEV_Init(4096); }