using CommonLib.LOG; using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Threading; namespace CommonLib.DAQ { public class MCM204:INotifyPropertyChanged { public EventHandler EventVoltage; public string HostName; protected static int AllCardCount; public int CardNO; #region .ctor public MCM204() { CardNO = AllCardCount; AllCardCount++; } #endregion public bool Enable { get { return m_Enable; } set { Boolean bPreEn = m_Enable; if (bPreEn != value) { if (value) { m_Enable = InitDevice(); } else { CloseDevice(); m_Enable = value; } if (bPreEn != m_Enable) OnPropertyChanged(); } } } EventHandler EventError = null; private void DoEventError(int errCode,string ErrorInfo) { ErrInfo info = new ErrInfo(errCode, ErrorInfo); EventError?.Invoke(this,info); } /// /// 断开连接 /// private void CloseDevice() { int err = -1; //杀死线程 try { if (ThreadRead != null) { while (ThreadRead.IsAlive) { EventKill.Set(); Thread.Sleep(10); } } } catch (Exception ex) { string Info = $"{HostName} {ex.Message} \n{ex.StackTrace}"; LogStorage.Logs.Add(LogLevel.Error, Info); DoEventError(err, Info); } finally { } } private bool InitDevice() { int error = 0; string Info = string.Empty; bool bResult = true; AIConfigParam loadDevAiConfigParam = new AIConfigParam(); loadDevAiConfigParam.chnsCount = MCM204Wrapper.MAX_CH_NUM; loadDevAiConfigParam.sampleRate = MCM204Wrapper.SAMPLING_RATE; // 设置采样率 loadDevAiConfigParam.sampleToAcquire = MCM204Wrapper.DATA_COUNT_PERCH; // 设置采样点数 try { error = MCM204Wrapper.OpenDevice(HostName); bResult = error == 0; if (bResult) { bResult = MCM204Wrapper.ConfigAIParam(HostName, ref loadDevAiConfigParam) == 0; ThreadRead = new Thread(TaskRead); ThreadRead.IsBackground = true; ThreadRead.Start(); if(bResult) bResult = MCM204Wrapper.StartDevice(HostName)==0; } } catch (Exception ex) { Info = $" {ex.Message} \n{ex.StackTrace}"; LogStorage.Logs.Add(LogLevel.Error, Info); } return bResult; } protected double[,] readData = null; public void DoEventVoltage() { EventVoltage?.Invoke(this, new EventArgs()); } protected ManualResetEvent EventKill = new ManualResetEvent(false); private void TaskRead() { try { Thread thread = Thread.CurrentThread; thread.Name = $"{HostName} Capture Thread"; EventKill.Reset(); readData = new double[4, MCM204Wrapper.DATA_COUNT_PERCH]; // 行为通道数,列为每通道采样点数 while (EventKill.WaitOne(10) == false) { MCM204Wrapper.GetDataDevice(HostName, readData); // 获取数据 DoEventVoltage(); } } catch (Exception ex) { string Info = $" {ex.Message} \n{ex.StackTrace}"; LogStorage.Logs.Add(LogLevel.Error, Info); } finally { MCM204Wrapper.StopDevice(HostName); MCM204Wrapper.CloseDevice(HostName); LogStorage.Logs.Add(LogLevel.Trace, $"{HostName}采样线程结束"); } } protected Thread ThreadRead = null; private bool m_Enable; #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; /// /// //属性更新事件 CallerMemberName 自动填写属性的名字 /// /// //[CallerMemberName] 需要 4.0以上才有 /// protected virtual void OnPropertyChanged([CallerMemberName] string propertyName=null) { //通过反射读取变量名,将值加入log if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } }