/// <summary> /// Setups the acquisition callbacks. /// </summary> /// <param name="halfReadCallbackAddr">The half read callback addr.</param> /// <param name="AiEndCallbackAddrr">The ai end callback addrr.</param> /// <returns>System.Int16.</returns> /// <exception cref="System.InvalidOperationException"> /// AI_AsyncDblBufferMode Fail, error: " + err /// or /// AI_ContBufferSetup Fail, error: " + err /// or /// AI_ContBufferSetup Fail, error: " + err /// or /// AI_EventCallBack Fail, error: " + err /// or /// AI_EventCallBack Fail, error: " + err /// </exception> private short SetupAcqCallbacks(MulticastDelegate halfReadCallbackAddr, MulticastDelegate AiEndCallbackAddrr) { short err = DASK.NoError; // Double Buffer Setup config_para.ai_raw_data_buf = new IntPtr[2]; config_para.ai_raw_data_buf[0] = Marshal.AllocHGlobal((int)(sizeof(uint) * config_para.ai_all_data_count)); config_para.ai_raw_data_buf[1] = Marshal.AllocHGlobal((int)(sizeof(uint) * config_para.ai_all_data_count)); config_para.is_ai_set_buf = true; err = DASK.AI_AsyncDblBufferMode(GetHandle(), true); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferMode Fail, error: " + err); } err = DASK.AI_ContBufferSetup(GetHandle(), config_para.ai_raw_data_buf[0], config_para.ai_all_data_count, out config_para.ai_buffer_id[0]); if (err < 0) { throw new InvalidOperationException("AI_ContBufferSetup Fail, error: " + err); } err = DASK.AI_ContBufferSetup(GetHandle(), config_para.ai_raw_data_buf[1], config_para.ai_all_data_count, out config_para.ai_buffer_id[1]); if (err < 0) { throw new InvalidOperationException("AI_ContBufferSetup Fail, error: " + err); } // Set AI buffer Ready event if (halfReadCallbackAddr != null) { err = DASK.AI_EventCallBack(GetHandle(), 1 /*add*/, DASK.DBEvent /*EventType*/, halfReadCallbackAddr); if (err != DASK.NoError) { throw new InvalidOperationException("AI_EventCallBack Fail, error: " + err); } } // Set AI done event if (AiEndCallbackAddrr != null) { err = DASK.AI_EventCallBack(GetHandle(), 1 /*add*/, DASK.AIEnd /*EventType*/, AiEndCallbackAddrr); if (err != DASK.NoError) { throw new InvalidOperationException("AI_EventCallBack Fail, error: " + err); } } return(err); }
private void DAQConfig() { err = DASK.AI_9221_Config(cardRegNumber, ConfigCtrl, TrigCtrl, true); if (err < 0) { MessageBox.Show("config"); } /*Set Scan and Sampling Rate*/ ; err = DASK.AI_9221_CounterInterval(cardRegNumber, ScanIntrv, SampIntrv); if (err < 0) { MessageBox.Show("AI_9222_CounterInterval"); } /*Enable Double Buffer Mode*/ err = DASK.AI_AsyncDblBufferMode(cardRegNumber, true); if (err < 0) { MessageBox.Show("AI_AsyncDblBufferMode"); } /*Setup Buffer for AI DMA Transfer*/ err = DASK.AI_ContBufferSetup(cardRegNumber, _ptr[0], AI_TotalReadCount, out _bufferId[0]); if (err < 0) { MessageBox.Show("AI_ContBufferSetup"); } err = DASK.AI_ContBufferSetup(cardRegNumber, _ptr[1], AI_TotalReadCount, out _bufferId[1]); if (err < 0) { MessageBox.Show("AI_ContBufferSetup"); } err = DASK.AI_EventCallBack(cardRegNumber, 1 /*add*/, DASK.DBEvent /*EventType*/, ai_buf_ready_cbdel); if (err < 0) { MessageBox.Show("AI_EventCallBack"); } err = DASK.AI_ContScanChannels(cardRegNumber, (ushort)(Channel - 1), AdRange, _bufferId, AI_TotalReadCount, 0 /*Ignore*/, DASK.ASYNCH_OP); if (err < 0) { MessageBox.Show("AI_ContScanChannels"); } }
static void Main(string[] args) { short err; ushort card_num = 0; short card; uint ReadCount = 60; ushort Group = 0; // Load Cell Channel Group ushort XMode = DASK.P9524_AI_XFER_DMA; ushort ADC_Range = 0; ushort ADC_SampRate = DASK.P9524_ADC_60_SPS; ushort ConfigCtrl = DASK.P9524_VEX_Range_10V | DASK.P9524_AI_BufAutoReset; ushort TrigCtrl = 0; uint dwTrigValue = 0; ushort GCtr0 = DASK.P9524_CTR_QD0; ushort Mode = DASK.P9524_x4_AB_Phase_Decoder; ushort Channel = DASK.P9524_AI_LC_CH0; byte Stopped = 0; byte HalfReady = 0; uint AccessCnt; int viewidx = 0; ushort overrunFlag = 0; double ActualRate; IntPtr BufPtr1; ushort BufID1; IntPtr BufPtr2; ushort BufID2; double[] voltageArray = new double[ReadCount]; ushort DFStage = 2; uint SPKRejThreshold = 16; StreamWriter sw = new StreamWriter("acq.csv"); BufPtr1 = Marshal.AllocHGlobal((int)(sizeof(uint) * (ReadCount))); BufPtr2 = Marshal.AllocHGlobal((int)(sizeof(uint) * (ReadCount))); card = DASK.Register_Card(DASK.PCI_9524, card_num); if (card < 0) { throw new InvalidOperationException("DSA_Register_Card Fail, error: " + card); } //Enable Decoder0 err = DASK.GPTC_Clear((ushort)card, GCtr0); if (err < 0) { throw new InvalidOperationException("GPTC_Clear Fail, error: " + err); } err = DASK.GPTC_Setup((ushort)card, GCtr0, Mode, 0, 0, 0, 0); if (err < 0) { throw new InvalidOperationException("GPTC_Setup Fail, error: " + err); } //apply parameter 4 to "2" to combine data to AI err = DASK.GPTC_Control((ushort)card, GCtr0, DASK.P9524_CTR_Enable, 2); if (err < 0) { throw new InvalidOperationException("GPTC_Control Fail, error: " + err); } err = DASK.AI_AsyncDblBufferMode((ushort)card, true); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferMode Fail, error: " + err); } /*In Double Buffer Mode, you should setup two buffers*/ err = DASK.AI_ContBufferSetup((ushort)card, BufPtr1, ReadCount, out BufID1); if (err < 0) { throw new InvalidOperationException("AI_ContBufferSetup Fail, error: " + err); } err = DASK.AI_ContBufferSetup((ushort)card, BufPtr2, ReadCount, out BufID2); if (err < 0) { throw new InvalidOperationException("AI_ContBufferSetup Fail, error: " + err); } /*Load Cell Group*/ /*Set DSP - it is necessary fot Load Cell Group*/ err = DASK.AI_9524_SetDSP((ushort)card, Channel, DASK.P9524_SPIKE_REJ_ENABLE, DFStage, (ushort)SPKRejThreshold); if (err < 0) { throw new InvalidOperationException("AI_9524_SetDSP Fail, error: " + err); } err = DASK.AI_9524_Config((ushort)card, Group, XMode, ConfigCtrl, TrigCtrl, (ushort)dwTrigValue); if (err < 0) { throw new InvalidOperationException("AI_9524_Config Fail, error: " + err); } Console.WriteLine("\nPress any key to start AI Infinite Acquisition\n"); Console.ReadKey(true); Console.WriteLine("\nYou can press any key to stop...\n\n"); err = DASK.AI_ContReadChannel((ushort)card, Channel, ADC_Range, new ushort[] { BufID1 }, ReadCount, ADC_SampRate, DASK.ASYNCH_OP); if (err < 0) { throw new InvalidOperationException("AI_ContReadChannel Fail, error: " + err); } do { err = DASK.AI_AsyncDblBufferHalfReady((ushort)card, out HalfReady, out Stopped); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferHalfReady Fail, error: " + err); } System.Threading.Thread.Sleep(1); if (HalfReady == 1) { if (viewidx == 0) { Console.WriteLine("Buffer 1 HalfReady, Process the data of Buffer 1\n"); /*Process buffer 1 data*/ DASK.AI_ContVScale((ushort)card, ADC_Range, BufPtr1, voltageArray, (int)ReadCount); for (int j = 0; j < ReadCount; j = j + 2) { //fprintf(fin, "AI0:,%13.9f,2's Complement Decoder Valuse:, %d\n", voltageArray[i], (int)(Buffer1[i + 1] * 256) / 256); sw.WriteLine(voltageArray[j] + "," + (int)(((uint *)(void *)BufPtr1)[j + 1] * 256) / 256); } viewidx = 1; /*Tell the driver you complete the buffer 1 process*/ err = DASK.AI_AsyncDblBufferHandled((ushort)card); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferHandled Fail, error: " + err); } } else { Console.WriteLine("Buffer 2 HalfReady, Process the data of Buffer 2\n"); /*Process buffer 2 data*/ DASK.AI_ContVScale((ushort)card, ADC_Range, BufPtr2, voltageArray, (int)ReadCount); for (int j = 0; j < ReadCount; j = j + 2) { //fprintf(fin, "AI0:,%13.9f,2's Complement Decoder Valuse:, %d\n", voltageArray[i], (int)(Buffer1[i + 1] * 256) / 256); sw.WriteLine(voltageArray[j] + "," + (int)(((uint *)(void *)BufPtr2)[j + 1] * 256) / 256); } viewidx = 0; /*Tell the driver you complete the buffer 2 process*/ err = DASK.AI_AsyncDblBufferHandled((ushort)card); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferHandled Fail, error: " + err); } } // This function can check if the overrun occurs. If the // function is called, AI_AsyncDblBufferHandled() should // be called to let driver to know user buffer is processed // completely err = DASK.AI_AsyncDblBufferOverrun((ushort)card, 0, out overrunFlag); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferOverrun Fail, error: " + err); } if (overrunFlag != 0) { Console.WriteLine("OVERRUN:\n" + overrunFlag); DASK.AI_AsyncDblBufferOverrun((ushort)card, 1, out overrunFlag); } } } while ((Stopped == 0) && (Console.KeyAvailable == false)); DASK.GetActualRate_9524((ushort)card, Group, ADC_SampRate, out ActualRate); Console.WriteLine("\n\nGeneral Purpose Channel 0 Acquisition Done in " + ActualRate + "Hz"); Console.WriteLine("The acquired data stored in acq.dat\n"); /*Clear AI setting while existing*/ err = DASK.AI_AsyncClear((ushort)card, out AccessCnt); if (err < 0) { Console.WriteLine("AI_AsyncClear Error:" + err + "\n"); DASK.AI_ContBufferReset((ushort)card); DASK.Release_Card((ushort)card); } if ((ConfigCtrl & DASK.P9524_AI_BufAutoReset) == 0) { err = DASK.AI_ContBufferReset((ushort)card); if (err < 0) { throw new InvalidOperationException("AI_ContBufferReset Fail, error: " + err); } } DASK.GPTC_Clear((ushort)card, GCtr0); DASK.Release_Card((ushort)card); Console.Write("Press any key to save data . . ."); sw.Close(); Console.ReadKey(true); }
private void PCI9222_Load(object sender, EventArgs e) { short ret = DASK.Register_Card(DASK.PCI_9222, 0); if (ret >= 0) { cardRegNumber = (ushort)ret;//Register_Card的时候,把卡号给cardRegNumber } else { MessageBox.Show("9222卡初始化失败!"); } //ushort ConfigCtrl = DASK.P922x_AI_SingEnded | DASK.P922x_AI_CONVSRC_INT;//?差分在哪区分 //ushort TrigCtrl = DASK.P922x_AI_TRGMOD_POST | DASK.P922x_AI_TRGSRC_SOFT;//? ushort ConfigCtrl = DASK.P922x_AI_Differential | DASK.P922x_AI_CONVSRC_INT;; //?差分在哪区分 ushort TrigCtrl = DASK.P922x_AI_TRGMOD_POST | DASK.P922x_AI_TRGSRC_SOFT;; //? uint ReTriggerCount = 0; /*Ignore in Non-Retrigger*/ bufindex = 0;//是用来两个交替读吗? //Channel = 4; /*AI Channel Number to be read*/ //uint SampIntrv = 8000; /*Sampling Rate: P922X_TIMEBASE/320 = 250K Hz*/ //这个设置,如果要快一点,怎么设置,变小? //P922X_TIMEBASE 时钟,80000000, intrv=80000000/5000=16000;5000为每秒5000个数据,每个通道1000个数据/s uint SampIntrv = 16000; /*Sampling Rate: P922X_TIMEBASE/320 = 250K Hz*/ //采样率=80 000000/500=160000,每个通道100个数据,有5个通道 //AI_ReadCount设置为500, //建议每秒钟采集10次-50次,这样不会丢数据。如果采集20次,怎么设置了? //目标:1S1000个数据,1ms一个 intrv=80000000/5000=16000;5000为每秒5000个数据,每个通道1000个数据/s SampIntrv = 16000, //AI_ReadCount=5000,则1S采集一次,如果设置成500,则0.1S采集一次,一次100个数据每通道、如果设置成250,则0.05采集一次,1次50个数据 uint ScanIntrv = (uint)(SampIntrv * (Channel + 1)); /*Scan Rate: P922X_TIMEBASE/320 = 250K Hz*/ ushort AdRange = DASK.AD_B_10_V; /*AI range*/ ai_buf_ready_cbdel = new CallbackDelegate(ai_buf_ready_cbfunc); _ptr[0] = Marshal.AllocHGlobal((sizeof(ushort) * ((Int32)AI_ReadCount)));//5000 _ptr[1] = Marshal.AllocHGlobal((sizeof(ushort) * ((Int32)AI_ReadCount))); VBuffer[0] = Marshal.AllocHGlobal((sizeof(double) * ((Int32)AI_ReadCount))); /*Configure AI*/ err = DASK.AI_9222_Config(cardRegNumber, ConfigCtrl, TrigCtrl, ReTriggerCount, true); if (err < 0) { MessageBox.Show("config"); } /*Set Scan and Sampling Rate*/ err = DASK.AI_9222_CounterInterval(cardRegNumber, ScanIntrv, SampIntrv); if (err < 0) { MessageBox.Show("AI_9222_CounterInterval"); } /*Enable Double Buffer Mode*/ err = DASK.AI_AsyncDblBufferMode(cardRegNumber, true); if (err < 0) { MessageBox.Show("AI_AsyncDblBufferMode"); } /*Setup Buffer for AI DMA Transfer*/ err = DASK.AI_ContBufferSetup(cardRegNumber, _ptr[0], AI_ReadCount, out _bufferId[0]); if (err < 0) { MessageBox.Show("AI_ContBufferSetup"); } err = DASK.AI_ContBufferSetup(cardRegNumber, _ptr[1], AI_ReadCount, out _bufferId[1]); if (err < 0) { MessageBox.Show("AI_ContBufferSetup"); } err = DASK.AI_EventCallBack(cardRegNumber, 1 /*add*/, DASK.DBEvent /*EventType*/, ai_buf_ready_cbdel);//???????????????????? if (err < 0) { MessageBox.Show("AI_EventCallBack"); } err = DASK.AI_ContScanChannels(cardRegNumber, Channel, AdRange, _bufferId, AI_ReadCount, 0 /*Ignore*/, DASK.ASYNCH_OP);////?????????? if (err < 0) { MessageBox.Show("AI_ContScanChannels"); } this.timer1.Enabled = true; }
public void PCI922Init() { short ret = DASK.Register_Card(DASK.PCI_9222, 0); if (ret >= 0) { cardRegNumber = (ushort)ret;//Register_Card的时候,把卡号给cardRegNumber Globals.addList("9222卡初始化成功!", Mycolor.None); #region PCI9222设置 //ushort ConfigCtrl = DASK.P922x_AI_SingEnded | DASK.P922x_AI_CONVSRC_INT;//?差分在哪区分 //ushort TrigCtrl = DASK.P922x_AI_TRGMOD_POST | DASK.P922x_AI_TRGSRC_SOFT;//? ushort ConfigCtrl = DASK.P922x_AI_Differential; //?差分在哪区分 ushort TrigCtrl = DASK.P922x_AI_TRGMOD_POST; //? uint ReTriggerCount = 0; /*Ignore in Non-Retrigger*/ bufindex = 0;//是用来两个交替读吗? //Channel = 4; /*AI Channel Number to be read*/ //uint SampIntrv = 8000; /*Sampling Rate: P922X_TIMEBASE/320 = 250K Hz*/ //这个设置,如果要快一点,怎么设置,变小? //AI_ReadCount设置为500, //建议每秒钟采集10次-50次,这样不会丢数据。如果采集20次,怎么设置了? //目标:1S1000个数据,1ms一个 intrv=80000000/5000=16000;5000为每秒5000个数据,每个通道1000个数据/s SampIntrv = 16000, //AI_ReadCount=5000,则1S采集一次,如果设置成500,则0.1S采集一次,一次100个数据每通道、如果设置成250,则0.05采集一次,1次50个数据 uint SampIntrv = 16000; /*Sampling Rate: P922X_TIMEBASE/320 = 250K Hz*/ uint ScanIntrv = (uint)(SampIntrv * (Channel + 1)); /*Scan Rate: P922X_TIMEBASE/320 = 250K Hz*/ ushort AdRange = DASK.AD_B_10_V; /*AI range*/ ai_buf_ready_cbdel = new CallbackDelegate(ai_buf_ready_cbfunc); _ptr[0] = Marshal.AllocHGlobal((sizeof(ushort) * ((Int32)AI_ReadCount)));//5000 _ptr[1] = Marshal.AllocHGlobal((sizeof(ushort) * ((Int32)AI_ReadCount))); VBuffer[0] = Marshal.AllocHGlobal((sizeof(double) * ((Int32)AI_ReadCount))); /*Configure AI*/ err = DASK.AI_9222_Config(cardRegNumber, ConfigCtrl, TrigCtrl, ReTriggerCount, true); if (err < 0) { Globals.addList("9222卡config失败!", Mycolor.ErrorRed); } /*Set Scan and Sampling Rate*/ err = DASK.AI_9222_CounterInterval(cardRegNumber, ScanIntrv, SampIntrv); if (err < 0) { Globals.addList("9222卡AI_9222_CounterInterval设置失败!", Mycolor.ErrorRed); } /*Enable Double Buffer Mode*/ err = DASK.AI_AsyncDblBufferMode(cardRegNumber, true); if (err < 0) { Globals.addList("9222卡AI_AsyncDblBufferMode设置失败!", Mycolor.ErrorRed); } /*Setup Buffer for AI DMA Transfer*/ err = DASK.AI_ContBufferSetup(cardRegNumber, _ptr[0], AI_ReadCount, out _bufferId[0]); if (err < 0) { Globals.addList("9222卡AI_ContBufferSetup设置失败!", Mycolor.ErrorRed); } err = DASK.AI_ContBufferSetup(cardRegNumber, _ptr[1], AI_ReadCount, out _bufferId[1]); if (err < 0) { Globals.addList("9222卡AI_ContBufferSetup设置失败!", Mycolor.ErrorRed); } err = DASK.AI_EventCallBack(cardRegNumber, 1 /*add*/, DASK.DBEvent /*EventType*/, ai_buf_ready_cbdel);//???????????????????? if (err < 0) { Globals.addList("9222卡AI_EventCallBack设置失败!", Mycolor.ErrorRed); } err = DASK.AI_ContScanChannels(cardRegNumber, Channel, AdRange, _bufferId, AI_ReadCount, 0 /*Ignore*/, DASK.ASYNCH_OP);////?????????? if (err < 0) { Globals.addList("9222卡AI_ContScanChannels设置失败!", Mycolor.ErrorRed); } #endregion } else { Globals.PostAlarmMachine(XAlarmLevel.STOP, (int)AlarmCode.凌华PCI9222板卡异常, AlarmCategory.SYSTEM.ToString(), "凌华PCI9222板卡异常"); if (ShowList != null) { ShowList("9222卡初始化失败!"); } } }
/// <summary> /// Gets the load cell offset. /// Use this function to get an average of offset when load cell is in steady status /// </summary> /// <returns>System.Double.</returns> /// <exception cref="InvalidOperationException"> /// AI_AsyncDblBufferMode Fail, error: " + err /// or /// AI_ContBufferSetup Fail, error: " + err /// or /// GetLoadCellOffset SetDSP Fail, error: " + err /// or /// AI_ContScanChannels Fail, error: " + err /// </exception> public double GetLoadCellOffset() { double offset = 0.0; short err = -1; IntPtr OffsetCalBf; double[] OffsetVolbuf; uint calSampleNum = 512; ushort BufID; OffsetCalBf = Marshal.AllocHGlobal((int)(sizeof(uint) * calSampleNum)); OffsetVolbuf = new double[((int)calSampleNum)]; ///////Disable fun of AI data combined with enconder, otherwise the caculated offset is going to BOOM err = DASK.GPTC_9524_SetCombineEcdData(GetHandle(), false); if (err < 0) { throw new InvalidOperationException("GPTC_9524_SetCombineEcdData Fail, error: " + err); } for (int i = 0; i < gptcCtrl.GPTCNum; i++) { err = DASK.GPTC_Clear(GetHandle(), gptcCtrl.GCtr[i]); if (err < 0) { throw new InvalidOperationException("GPTC_Clear Fail, error: " + err); } } /////// err = DASK.AI_AsyncDblBufferMode((ushort)GetHandle(), false); if (err < 0) { throw new InvalidOperationException("AI_AsyncDblBufferMode Fail, error: " + err); } err = DASK.AI_ContBufferSetup((ushort)GetHandle(), OffsetCalBf, calSampleNum, out BufID); if (err < 0) { throw new InvalidOperationException("AI_ContBufferSetup Fail, error: " + err); } err = SetDSP(); if (err < 0) { throw new InvalidOperationException("GetLoadCellOffset SetDSP Fail, error: " + err); } // Start Acquisition. The acquired raw data will be stored in the set buffer. //ushort ADC_SampRate = DASK.P9524_ADC_2K_SPS; err = DASK.AI_ContScanChannels((ushort)GetHandle(), config_para.ai_select_channel, config_para.ai_chnl_range, new ushort[] { BufID }, calSampleNum, config_para.ai_sample_rate, DASK.SYNCH_OP); if (err < 0) { throw new InvalidOperationException("AI_ContScanChannels Fail, error: " + err); } DASK.AI_ContVScale(GetHandle(), config_para.ai_chnl_range, OffsetCalBf, OffsetVolbuf, (int)calSampleNum); for (int i = 0; i < calSampleNum; i++) { offset = offset + OffsetVolbuf[i]; } offset = offset / calSampleNum; DASK.AI_ContBufferReset(GetHandle()); uint ai_access_cnt; DASK.AI_AsyncClear(GetHandle(), out ai_access_cnt); return(offset); }