public static void MyCallback(int bd, MccDaq.EventType et, uint interruptCount, IntPtr pUserData) { //MyCallback is a static member function. So, we must do some work to get the reference // to this object. Recall that we passed in a pointer to a struct that wrapped the // class reference as UserData. TUserData userStruct = (TUserData)Marshal.PtrToStructure(pUserData, typeof(TUserData)); frmEventDisplay ThisObj = (frmEventDisplay)userStruct.ThisObj; ThisObj._eventCount++; // these updates below are "expensive"; so, only do them every // _updateSize interrupts. if (interruptCount >= ThisObj._intCount + _updateSize) { ThisObj._intCount = interruptCount; //read the digital ushort digVal = 0; MccDaq.ErrorInfo ULStat = ThisObj.DaqBoard.DIn (ThisObj.PortNum, out digVal); if (ULStat.Value != MccDaq.ErrorInfo.ErrorCode.NoErrors) { ThisObj.DaqBoard.DisableEvent(MccDaq.EventType.AllEventTypes); } ThisObj.lblInterruptCount.Text = Convert.ToString(ThisObj._intCount); ThisObj.lblInterruptsMissed.Text = (ThisObj._intCount - ThisObj._eventCount).ToString(); ThisObj.lblDigitalIn.Text = "0x" + digVal.ToString("x2"); } ThisObj.lblEventCount.Text = Convert.ToString(ThisObj._eventCount); }
private void cmdEnableEvent_Click(object sender, System.EventArgs e) { /// Enable and connect one or more event types to a single user callback /// function using MccDaq.MccBoard.EnableEvent(). /// /// Parameters: /// eventType :the condition that will cause an event to fire /// eventSize :only used for MccDaq.EventType.OnDataAvailable to determine how /// many samples to collect before firing an event /// _ptrMyCallback : a pointer to the user function or event handler /// to call when above event type occurs. Note that the handler /// can be a delegate or a static member function. Here, we use /// a pointer to a static member function. /// /// _ptrUserData : a pointer to a value type that will be used within the event /// handler. Since our handler is a static member function which /// does NOT include a reference to this class instance, we're /// sending the pointer to a struct that holds a reference to the class. MccDaq.EventType eventType = MccDaq.EventType.OnExternalInterrupt; uint eventSize = 0; // not used for OnExternalInterrupt MccDaq.ErrorInfo ULStat = DaqBoard.EnableEvent(eventType, eventSize, _ptrMyCallback, _ptrUserData); if (ULStat.Value == MccDaq.ErrorInfo.ErrorCode.NoErrors) { this._eventCount = 0; this._intCount = 0; this.lblEventCount.Text = _eventCount.ToString(); this.lblInterruptsMissed.Text = "0"; this.lblInterruptCount.Text = "0"; this.lblDigitalIn.Text = "NA"; } }
public static void OnScanError(int bd, MccDaq.EventType et, uint scanError, IntPtr pdata) { //OnScanError is a static member function. So, we must do some work to get the reference // to this object. Recall that we passed in a pointer to a struct that wrapped the // class reference as UserData... TUserData thisStruct = (TUserData)Marshal.PtrToStructure(pdata, typeof(TUserData)); frmEventDisplay ThisObj = (frmEventDisplay)thisStruct.ThisObj; ThisObj.DaqBoard.StopBackground(MccDaq.FunctionType.AiFunction); // Reset the chkAutoRestart such that the 'OnEndOfAiScan' event does //not automatically start a new scan ThisObj.chkAutoRestart.Checked = false; }
public static void MyCallback(int bd, MccDaq.EventType et, uint sampleCount, IntPtr pUserData) { //MyCallback is a static member function. So, we must do some work to get the reference // to this object. Recall that we passed in a pointer to a struct that wrapped the // class reference as UserData. TUserData userStruct = (TUserData)Marshal.PtrToStructure(pUserData, typeof(TUserData)); frmEventDisplay ThisObj = (frmEventDisplay)userStruct.ThisObj; //Calculate the index of the latest sample in the buffer. int sampleIdx = ((int)sampleCount - 1) % ThisObj.TotalCount; ushort rawData; float voltData; //Retrieve the latest sample and convert it to engineering units. MccDaq.MccService.WinBufToArray(ThisObj.hBuffer, out rawData, (int)sampleIdx, 1); ThisObj.DaqBoard.ToEngUnits(ThisObj.Range, rawData, out voltData); // Update display values. ThisObj.lblSampleCount.Text = sampleCount.ToString(); ThisObj.lblLatestSample.Text = voltData.ToString("F4") + " V"; ThisObj.lblStatus.Text = "RUNNING"; if (et == MccDaq.EventType.OnEndOfAiScan) { //If the event is the end of acquisition, release the resources in preparation for // the next scan. ThisObj.DaqBoard.StopBackground(MccDaq.FunctionType.AiFunction); if (ThisObj.chkAutoRestart.Checked) { int rate = ThisObj.DesiredRate; ThisObj.DaqBoard.AInScan(0, 0, ThisObj.TotalCount, ref rate, ThisObj.Range, ThisObj.hBuffer, ThisObj.Options); } else { ThisObj.lblStatus.Text = "IDLE"; } } }
private void cmdEnableEvent_Click(object sender, System.EventArgs e) { /// Enable and connect one or more event types to a single user callback /// function using MccDaq.MccBoard.EnableEvent(). /// /// If we want to attach a single callback function to more than one event /// type, we can do it in a single call to MccDaq.MccBoard.EnableEvent, or we can do this in /// separate calls for each event type. The main disadvantage of doing this in a /// single call is that if the call generates an error, we will not know which /// event type caused the error. In addition, the same error condition could /// generate multiple error messages. /// /// Parameters: /// eventType :the condition that will cause an event to fire /// eventSize :only used for MccDaq.EventType.OnDataAvailable to determine how /// many samples to collect before firing an event /// _ptrMyCallback : a pointer to the user function or event handler /// to call when above event type occurs. Note that the handler /// can be a delegate or a static member function. Here, we use /// a pointer to a static member function. /// /// _ptrUserData : a pointer to a value type that will be used within the event /// handler. Since our handler is a static member function which /// does NOT include a reference to this class instance, we're /// sending the pointer to a struct that holds a reference to the class. MccDaq.EventType eventType = MccDaq.EventType.OnDataAvailable; eventType |= MccDaq.EventType.OnEndOfAiScan; uint eventSize = (uint)this.EventSize.Value; MccDaq.ErrorInfo ULStat = DaqBoard.EnableEvent(eventType, eventSize, _ptrMyCallback, _ptrUserData); eventType = MccDaq.EventType.OnScanError; ULStat = DaqBoard.EnableEvent(eventType, 0, _ptrOnScanError, _ptrUserData); }