一种委托
public class IOControl { #region 全局变量 public delegate void GetDiInfoEvent(int diNum); //捕捉DI上升沿信号 public GetDiInfoEvent ReturnDiUpNum; //捕捉DI下降沿信号 public GetDiInfoEvent ReturnDiDownNum; /// <summary> /// IO卡实时监控反馈 /// </summary> /// <param name="isOpen"></param> public delegate void showIOStautsDelegate(bool isOpen); public showIOStautsDelegate _showIOStauts; /// <summary> /// 展示信息 /// </summary> /// <param name="ptr"></param> public delegate void showMessageDelegate(string message, string color = ColorCode.DefaultMessageColor); public showMessageDelegate _showMessage; private void ShowDODIStatusOnlyOne(byte[] rst) { byte distatus = rst[8]; for (int i = 0; i < _dodiCount; i++) { if ((distatus & (1 << i)) == 0x00) { if (_diList[i] == OnOffEnum.On) { lock (_diLock) { _diList[i] = OnOffEnum.Off; ReturnDiDownNum?.BeginInvoke(i, null, null); } } } else { if (_diList[i] == OnOffEnum.Off) { lock (_diLock) { _diList[i] = OnOffEnum.On; ReturnDiUpNum?.BeginInvoke(i, null, null); } } } } } public partial class WAChartOne : Form { public void SetDIO(IOControl dio) { if (dio == null) { return; } if (_dio != null) { _dio.ReturnDiUpNum -= new IOControl.GetDiInfoEvent(GetDiUp); _dio.ReturnDiDownNum -= new IOControl.GetDiInfoEvent(GetDiDown); _dio._showIOStauts -= new IOControl.showIOStautsDelegate(ShowIOStatus); } _dio = dio; _dio.ReturnDiUpNum += new IOControl.GetDiInfoEvent(GetDiUp); _dio.ReturnDiDownNum += new IOControl.GetDiInfoEvent(GetDiDown); _dio._showIOStauts += new IOControl.showIOStautsDelegate(ShowIOStatus); _dio.ReturnDiStatus(); } private void ShowIOStatus(bool isOpen) { if (IOStatusLB.InvokeRequired) { IOStatusLB.Invoke(new Action(() => { IOStatusLB.Text = isOpen ? "已连接" : "未连接"; IOStatusLB.BackColor = isOpen ? Color.Green : Color.Red; if (isOpen) { ResetBT.Enabled = true; StartBT.Enabled = true; } else { ResetBT.Enabled = false; StartBT.Enabled = false; } })); } else { IOStatusLB.Text = isOpen ? "已连接" : "未连接"; IOStatusLB.ForeColor = isOpen ? Color.Green : Color.Red; if (isOpen) { ResetBT.Enabled = true; StartBT.Enabled = true; } else { ResetBT.Enabled = false; StartBT.Enabled = false; } } } //上升沿 private void GetDiUp(int index) { switch (index) { case 0: //使能 _canRun = true; ResetBT.Enabled = true; StartBT.Enabled = true; break; case 1: //复位 snap.Enabled = true; grab.Enabled = true; freeze.Enabled = true; ResetBT.Enabled = true; StartBT.Enabled = true; _dio.OnOffDoDi(2, false, _iOConfig); _showMessage?.Invoke("复位完成"); break; case 2: //到位 snap.Enabled = true; grab.Enabled = true; freeze.Enabled = true; ResetBT.Enabled = true; StartBT.Enabled = true; _dio.OnOffDoDi(0, false, _iOConfig); _camera.Freeze(); _showMessage?.Invoke("运动并拍照完成"); break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; default: break; } } //下降沿 private void GetDiDown(int index) { switch (index) { case 0: _canRun = false; ResetBT.Enabled = false; StartBT.Enabled = false; break; case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; default: break; } } }
开心是个奢饰的东西

浙公网安备 33010602011771号