1.初始化
1 void sfcs::OnSetPCI4461_AITask() 2 { 3 long error = 0; 4 char errBuff[2048] = { '\0' }; 5 if (mKZYHandle_AI != 0) { 6 DAQmxStopTask(mKZYHandle_AI); 7 DAQmxClearTask(mKZYHandle_AI); 8 mKZYHandle_AI = 0; 9 } 10 11 /*********************************************/ 12 // DAQmx Configure Code 13 /*********************************************/ 14 DAQmxErrChk(DAQmxCreateTask("", &mKZYHandle_AI));//创建一个任务 15 DAQmxErrChk(DAQmxCreateAIVoltageChan(mKZYHandle_AI, "Dev1/ai0:1", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));//创建测量电压的通道 16 DAQmxErrChk(DAQmxCfgSampClkTiming(mKZYHandle_AI, "", RATE_ACQPCI4461, DAQmx_Val_Rising, DAQmx_Val_ContSamps, RATE_ACQ4461));//配置采样时钟 采样频率+连续采样+每个通道采样点 17 DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(mKZYHandle_AI, DAQmx_Val_Acquired_Into_Buffer, RATE_ACQ4461, 0, EveryNCallback_PCI4461, this));//将设定数量的数据从PC缓冲区传输到设备,或者将设定数量的数据从设备传输到缓冲区 18 19 //DAQmxErrChk(DAQmxCfgSampClkTiming(mKZYHandle_AI, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000)); 20 //DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(mKZYHandle_AI, DAQmx_Val_Acquired_Into_Buffer, 1000, 0, EveryNCallback_PCI4461, this)); 21 22 DAQmxErrChk(DAQmxRegisterDoneEvent(mKZYHandle_AI, 0, DoneCallback, this));//注册回调函数,以便在任务因错误而停止或有限获取任务或有限生成任务完成执行时接收事件 23 24 /*********************************************/ 25 // DAQmx Start Code 26 /*********************************************/ 27 DAQmxErrChk(DAQmxStartTask(mKZYHandle_AI)); 28 Error: 29 if (DAQmxFailed(error)) { 30 DAQmxGetExtendedErrorInfo(errBuff, 2048); 31 /*********************************************/ 32 // DAQmx Stop Code 33 /*********************************************/ 34 DAQmxStopTask(mKZYHandle_AI); 35 DAQmxClearTask(mKZYHandle_AI); 36 //CString str(errBuff); 37 //AfxMessageBox(str); 38 } 39 }
2.回调函数处理-接收数据
1 #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else 2 int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData); 3 int32 CVICALLBACK EveryNCallback_PCI4461(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData); 4 5 int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData) 6 { 7 int32 error = 0; 8 char errBuff[2048] = { '\0' }; 9 sfcs *dlg = (sfcs*)callbackData; 10 // Check to see if an error stopped the task. 11 DAQmxErrChk(status); 12 if (taskHandle == dlg->mKZYHandle) { 13 dlg->m_is4461stop = true; 14 } 15 16 Error: 17 if (DAQmxFailed(error)) { 18 DAQmxGetExtendedErrorInfo(errBuff, 2048); 19 DAQmxClearTask(taskHandle); 20 printf("DAQmx Error: %s\n", errBuff); 21 } 22 return 0; 23 } 24 int32 CVICALLBACK EveryNCallback_PCI4461(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData) 25 { 26 int32 error = 0; 27 char errBuff[2048] = { '\0' }; 28 int32 read = 0; 29 double data[2 * RATE_ACQ4461]; 30 int nLen = sizeof(double) * RATE_ACQ4461; 31 sfcs *dlg = (sfcs*)callbackData; 32 33 /*********************************************/ 34 // DAQmx Read Code 35 /*********************************************/ 36 DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, RATE_ACQ4461, 10.0, DAQmx_Val_GroupByChannel, data, RATE_ACQ4461 * 2, &read, NULL));//从设备中读数据 37 if (read > 0) { 38 39 //memcpy(&mTZ[mTZLen], &data[1 * RATE_ACQ4461], nLen); 40 // 41 //memcpy(data1, &data[0 * RATE_ACQ4461], nLen); 42 //memcpy(data2, &data[1 * RATE_ACQ4461], nLen); 43 //dlg->OnDataModify(mTZLen); 44 //return 0; 45 46 dlg->OnSetShowTzyData(data); 47 if (mTZLen < ARRAYSIZE(mTZ)) { 48 memcpy(&mTZ[mTZLen], &data[1 * RATE_ACQ4461], nLen); 49 dlg->OnDataModify(mTZLen); 50 //if (dlg->m_isTzTy == 0) { 51 // // TZ 52 // if (true == dlg->mIsClearWave[0]) { 53 // //波形显示 54 // dlg->OnDataModify(mTZLen); 55 // dlg->mIsClearWave[0] = false; 56 // } 57 // else { 58 // //波形显示 59 // //dlg->mpage2.PostMessage(WM_TZWAVE, (WPARAM)&mTZ[mTZLen], (LPARAM)false); 60 // } 61 //} 62 //else { 63 // // TY 64 // if (true == dlg->mIsClearWave[0]) { 65 // //波形显示 66 // dlg->OnDataModify(mTZLen); 67 // dlg->mIsClearWave[0] = false; 68 // } 69 // else { 70 // //波形显示 71 // //dlg->mpage2.PostMessage(WM_TYWAVE, (WPARAM)&mTZ[mTZLen], (LPARAM)false); 72 // } 73 //} 74 mTZLen += RATE_ACQ4461; 75 } 76 if (m4461outLen < ARRAYSIZE(m4461out)) { 77 // 4461输出数据 78 memcpy(&m4461out[m4461outLen], &data[0 * RATE_ACQ4461], nLen); 79 m4461outLen += RATE_ACQ4461; 80 } 81 } 82 Error: 83 if (DAQmxFailed(error)) { 84 DAQmxGetExtendedErrorInfo(errBuff, 2048); 85 /*********************************************/ 86 // DAQmx Stop Code 87 /*********************************************/ 88 DAQmxStopTask(taskHandle); 89 DAQmxClearTask(taskHandle); 90 //CString str(errBuff); 91 //AfxMessageBox(str); 92 } 93 return 0; 94 }
3.发送数据
1 void sfcs::OnSetAOKZY(double value, double startFreq, double stopFreq, int pointNum) 2 { 3 int32 error = 0; 4 //float64 data[1000]; 5 char errBuff[2048] = { '\0' }; 6 int aoChannelIndex = 0; 7 char phyChannel[100]; 8 9 if (mKZYHandle != 0) { 10 /*********************************************/ 11 // DAQmx Stop Code 12 /*********************************************/ 13 DAQmxStopTask(mKZYHandle); 14 DAQmxClearTask(mKZYHandle); 15 mKZYHandle = NULL; 16 m_is4461stop = true; 17 } 18 // 19 sprintf(phyChannel, "Dev1/ao1"); 20 21 double *data = NULL; 22 int dataNum = 0; 23 int written = 0; 24 genSwapeData(&data, &dataNum, value, startFreq, stopFreq, pointNum, 10); 25 m_is4461stop = false; 26 DAQmxErrChk(DAQmxCreateTask("", &mKZYHandle)); 27 DAQmxErrChk(DAQmxCreateAOVoltageChan(mKZYHandle, phyChannel, "", -10, 10, DAQmx_Val_Volts, NULL)); 28 DAQmxErrChk(DAQmxCfgSampClkTiming(mKZYHandle, "", RATE_ACQPCI4461, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, dataNum)); 29 30 DAQmxErrChk(DAQmxRegisterDoneEvent(mKZYHandle, 0, DoneCallback, this)); 31 32 /*********************************************/ 33 // DAQmx Write Code 34 /*********************************************/ 35 DAQmxErrChk(DAQmxWriteAnalogF64(mKZYHandle, dataNum, 0, 10.0, DAQmx_Val_GroupByChannel, data, NULL, NULL)); 36 37 /*********************************************/ 38 // DAQmx Start Code 39 /*********************************************/ 40 DAQmxErrChk(DAQmxStartTask(mKZYHandle)); 41 42 Error: 43 if (DAQmxFailed(error)) { 44 DAQmxGetExtendedErrorInfo(errBuff, 2048); 45 if (mKZYHandle != 0) { 46 /*********************************************/ 47 // DAQmx Stop Code 48 /*********************************************/ 49 DAQmxStopTask(mKZYHandle); 50 DAQmxClearTask(mKZYHandle); 51 mKZYHandle = NULL; 52 m_is4461stop = true; 53 } 54 } 55 56 if (DAQmxFailed(error)) { 57 58 } 59 return; 60 }
标签:dlg,mKZYHandle,AI,DAQmx,DAQmxErrChk,RATE,PC4461 From: https://www.cnblogs.com/wuyuan2011woaini/p/18190800