using Automation.BDaq; using CommonUtils.内部存储; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace CustomModule.ViewModels { public class ViewAViewModel : CommonUtils.ViewModelBase.BaseViewModel { private string _message; public string Message { get { return _message; } set { SetProperty(ref _message, value); } } public ViewAViewModel() { Message = "View A from your Prism Module"; } public void OpenStartData() { try { AsynchronousOneBufferedAI.RunTest(); } catch (Exception ex) { isErr = true; } } } public class AsynchronousOneBufferedAI { public static bool isEnd = false; public static WaveformAiCtrl waveformAiCtrl = new WaveformAiCtrl(); public static ErrorCode errorCode = ErrorCode.Success; // Step 1: Create a 'WaveformAiCtrl' for Streaming AI function. public static void Init() { waveformAiCtrl.Stopped += new EventHandler(waveformAiCtrl_Stopped); //----------------------------------------------------------------------------------- // Configure the following parameters before running the demo //----------------------------------------------------------------------------------- string deviceDescription = "PCI-1711L,BID#0"; string profilePath = "../../../profile/PCI-1711.xml"; int startChannel = 0; int channelCount = 1; int sectionLength = 300; int sectionCount = 1; double convertClkRate = 100; try { // Step 3: Select a device by device number or device description and specify the access mode. // in this example we use ModeWrite mode so that we can fully control the device, including configuring, sampling, etc. waveformAiCtrl.SelectedDevice = new DeviceInformation(deviceDescription); errorCode = waveformAiCtrl.LoadProfile(profilePath);//Loads a profile to initialize the device. if (BioFailed(errorCode)) { throw new Exception(); } // Step 4: Set necessary parameters. Conversion conversion = waveformAiCtrl.Conversion; conversion.ChannelStart = startChannel; conversion.ChannelCount = channelCount; conversion.ClockRate = convertClkRate; Record record = waveformAiCtrl.Record; record.SectionCount = sectionCount;//The sectionCount is nonzero value, which means 'One Buffered' mode. record.SectionLength = sectionLength; // Step 5: prepare the buffered AI. errorCode = waveformAiCtrl.Prepare(); if (BioFailed(errorCode)) { throw new Exception(); } // Step 6: start Asynchronous Buffered AI, 'Asynchronous' means the method returns immediately // after the acquisition has been started. The 'bufferedAiCtrl_Stopped' method will be called // after the acquisition is completed. } catch { } } // Step 2: Set the notification event Handler by which we can known the state of operation effectively. public static void RunTest() { try { errorCode = waveformAiCtrl.Start(); if (BioFailed(errorCode)) { throw new Exception(); } } catch (Exception e) { // Something is wrong string errStr = BioFailed(errorCode) ? " Some error occurred. And the last error code is " + errorCode.ToString() : e.Message; Console.WriteLine(errStr); } } // process the acquired data static void waveformAiCtrl_Stopped(object sender, BfdAiEventArgs e) { try { Console.Write(" Acquisition has completed, sample count is " + e.Count.ToString() + ".\n"); WaveformAiCtrl waveformAiCtrl = (WaveformAiCtrl)sender; Int32 channelCountMax = waveformAiCtrl.Features.ChannelCountMax; Int32 startChan = waveformAiCtrl.Conversion.ChannelStart; Int32 channelCount = waveformAiCtrl.Conversion.ChannelCount; Int32 sectionLength = waveformAiCtrl.Record.SectionLength; Int32 bufSize = sectionLength * channelCount; Int32 getDataCount = 0, returnedCount = 0; Int32 remainingCount = e.Count; // e.Count notifys that how many samples had been gathered in the 'Stopped' event. Double[] allChanData = new Double[bufSize]; do { getDataCount = Math.Min(bufSize, remainingCount); waveformAiCtrl.GetData(getDataCount, allChanData, 0, out returnedCount); remainingCount -= returnedCount; } while (remainingCount > 0); InternalStorage.Buffs = allChanData; InternalStorage.ischange = true; isEnd = false; Console.WriteLine(" Show each channel's new data:"); for (int i = 0; i < channelCount; ++i) { Console.WriteLine(" Channel {0}: {1,13:f8}", (i % channelCount + startChan) % channelCountMax, allChanData[i]); } } catch { isEnd = false; } } static bool BioFailed(ErrorCode err) { return err < ErrorCode.Success && err >= ErrorCode.ErrorHandleNotValid; } } }