/// <summary> /// 添加一个IO ,IO类型由属性 IsDigitOut 决定 /// </summary> /// <param name="module"></param> /// <param name="ioIndex"></param> /// <param name="name"></param> public void AddIO(IJFModule_DIO module, int ioIndex, string name = null, LampButton.LColor offColor = LampButton.LColor.Gray, LampButton.LColor onColor = LampButton.LColor.Green) { if (name == null) { name = (IsDigitOut ? "Out_" : "In_") + ioIndex.ToString("D2"); } foreach (DIOInfo ii in ioInfos) { if (module == ii.Module && ioIndex == ii.Index) { return; } } UcDioChn io = new UcDioChn(); io.OffColor = offColor; io.OnColor = onColor; if (IsDigitOut) { io.Click += new EventHandler(DOButtonClick); } io.IOName = name; Controls.Add(io); ucIOs.Add(io); ioInfos.Add(new DIOInfo(module, ioIndex)); //dictIOs.Add(io, new DIOInfo(module, ioIndex)); if (isLoaded) { AdjustIOView(); } }
/// <summary> /// DO按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void DOButtonClick(object sender, EventArgs e) { if (!IsDigitOut) { return; } for (int i = 0; i < ucIOs.Count; i++) { if (sender == ucIOs[i] || sender == (ucIOs[i] as UcDioChn).LButton) { UcDioChn ucIO = ucIOs[i] as UcDioChn; DIOInfo ii = ioInfos[i]; if (ii.Module == null) { lbInfo.Text = "Failed By:" + ucIO.IOName + "'s Module is null"; return; } if (ii.Index < 0 || ii.Index >= ii.Module.DOCount) { lbInfo.Text = "Failed By:" + ucIO.IOName + "'s index is out of rang 0~" + (ii.Module.DOCount - 1); return; } bool isOn = false; int err = ii.Module.GetDO(ii.Index, out isOn); if (err != 0) { lbInfo.Text = "Failed By: Get DO=\"" + ucIO.IOName + "\"failed ,errorcode = " + err; return; } err = ii.Module.SetDO(ii.Index, !isOn); if (err != 0) { lbInfo.Text = "Failed By: Set DO=\"" + ucIO.IOName + "\"failed ,errorcode = " + err; return; } lbInfo.Text = "Success! Set DO=\"" + ucIO.IOName + "\"" + (isOn? "Off":"On"); return; } } }
/// <summary> /// 将IO状态更新到界面上 /// </summary> public void UpdateIOStatus() { if (InvokeRequired) { Invoke(new Action(UpdateIOStatus)); return; } if (ucIOs.Count == 0) { return; } StringBuilder sb = new StringBuilder("Update Err:"); bool isUpdateOK = true; do { bool[] updateds = new bool[ucIOs.Count]; //更新过的IO列表 int updatedCount = 0; //已经更新过的数量 for (int i = 0; i < ucIOs.Count; i++) { if (updatedCount == ucIOs.Count) //全部更新完成 { break; } if (updateds[i]) //这个IO更新过 { continue; } UcDioChn ucio = ucIOs[i] as UcDioChn; DIOInfo ii = ioInfos[i]; if (ii.Module == null) { updateds[i] = true; updatedCount++; sb.Append(ucio.IOName + " "); isUpdateOK = false; continue; } if (ii.Index < 0 || ii.Index >= (IsDigitOut ? ii.Module.DOCount : ii.Module.DICount)) { updateds[i] = true; updatedCount++; sb.Append(ucio.IOName + " "); isUpdateOK = false; continue; } bool isOn = false; int err = 0; err = IsDigitOut? ii.Module.GetDO(ii.Index, out isOn):ii.Module.GetDI(ii.Index, out isOn); if (err != 0) { updateds[i] = true; updatedCount++; sb.Append(ucio.IOName + " "); isUpdateOK = false; continue; } else { ucio.IsTurnOn = isOn; } updateds[i] = true; updatedCount++; //尝试更新后继IO bool isSameModuleExisted = false; for (int j = updatedCount; j < ucIOs.Count; j++) { if (ioInfos[j].Module == ii.Module) { isSameModuleExisted = true; break; } } if (!isSameModuleExisted) { continue; } bool[] isOns = null; err = IsDigitOut ? ii.Module.GetAllDOs(out isOns) : ii.Module.GetAllDIs(out isOns); if (err != 0) { continue; } for (int j = updatedCount; j < ucIOs.Count; j++) { if (ioInfos[j].Module == ii.Module && ioInfos[j].Index > 0 && ioInfos[j].Index <= (IsDigitOut ? ioInfos[j].Module.DOCount: ioInfos[j].Module.DICount)) { UcDioChn ui = ucIOs[j] as UcDioChn; ui.IsTurnOn = isOns[j]; updateds[j] = true; updatedCount++; i++; } } } } while (false); if (!isUpdateOK) { lbInfo.Text = sb.ToString(); } }