private void DataProcess() { while (queue.IsAlive) { while (myqueue.Count > 0) { err = DASK.AI_ContVScale(cardRegNumber, DASK.AD_B_5_V, myqueue.Dequeue(), VBuffer[0], (int)AI_TotalReadCount); if (err < 0) { MessageBox.Show("AI_ContVScale"); } this.Invoke((MethodInvoker) delegate { Displayaidata(VBuffer[0]); writefftdataCsv(0, ch0); //writefftdataCsv(1, ch1); //writefftdataCsv(2, ch2); //writefftdataCsv(3, ch3); //writefftdataCsv(4, ch4); textBoxQueueCount.Text = myqueue.Count.ToString(); }); Thread.Sleep(1); } Thread.Sleep(1); } }
private void ai_buf_ready_cbfunc()//是两个交替读吗?callback { if (bufindex == 0) { DASK.AI_ContVScale(cardRegNumber, DASK.AD_B_10_V, _ptr[0], VBuffer[0], (int)AI_ReadCount);//?? bufindex = 1; } else { DASK.AI_ContVScale(cardRegNumber, DASK.AD_B_10_V, _ptr[1], VBuffer[0], (int)AI_ReadCount);//? bufindex = 0; } DisplayTrend displaytrendInvoke = new DisplayTrend(Displayaidata); BeginInvoke(displaytrendInvoke, new object[] { VBuffer[0] }); }
/// <summary> /// Converts the raw data to position and force arrays. /// </summary> /// <param name="readingArray">The reading array.</param> /// <param name="loadCellOffset">The load cell offset.</param> /// <param name="unitTomm">The scalling factor to convert encoder unit to millimeter.</param> /// <param name="sensitivity">The sensitivity of loadcell.</param> /// <param name="position">The position array.</param> /// <param name="force">The force array.</param> /// <returns>System.Int16.</returns> public short ConvertRawData(IntPtr readingArray, double loadCellOffset, double unitTomm, double sensitivity, double[] position, double[] force) { short err = DASK.NoError; double[] ai_scale_data_buf = new double[config_para.ai_all_data_count]; DASK.AI_ContVScale(GetHandle(), config_para.ai_chnl_range, readingArray, ai_scale_data_buf, (int)config_para.ai_all_data_count); //seperate data unsafe { int *offsetPtr = (int *)readingArray.ToPointer(); for (int i = 0; i < config_para.ai_all_data_count / 2; i++) { //Use two list, list of AI voltage (forece) and list of Encoder (position), to replace position[i] = unitTomm * Math.Abs((double)(*(offsetPtr + i * 2 + 1) * 256) / 256); force[i] = 1000.0 * sensitivity * (ai_scale_data_buf[i * 2] - loadCellOffset); } } return(err); }
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); }
/// <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); }