/// <summary> /// 读取寄存器 /// </summary> /// <returns></returns> public virtual bool ReadIni() { bool blRight = false; try { //起始寄存器 regStart = IniFile.I_I.ReadIniStr("StartReg", "RegName", "D0", PathRegIni); //寄存器个数 NumRegSet = IniFile.I_I.ReadIniInt("NumRegSet", "NumRegSet", PathRegIni); for (int i = 0; i < NumReg; i++) { string nameSection = i.ToString(); string strSelect = IniFile.I_I.ReadIniStr(nameSection, "Select", PathRegIni); string nameReg = IniFile.I_I.ReadIniStr(nameSection, "RegName", PathRegIni); string customReg = IniFile.I_I.ReadIniStr(nameSection, "NameCustomReg", PathRegIni); string PLCSend = IniFile.I_I.ReadIniStr(nameSection, "PLCSend", PathRegIni); string PCSend = IniFile.I_I.ReadIniStr(nameSection, "PCSend", PathRegIni); double min = IniFile.I_I.ReadIniDbl(nameSection, "Min", 0, PathRegIni); double max = IniFile.I_I.ReadIniDbl(nameSection, "Max", int.MaxValue, PathRegIni); double co = IniFile.I_I.ReadIniDbl(nameSection, "Co", 1, PathRegIni); string annotation = IniFile.I_I.ReadIniStr(nameSection, "Annotation", PathRegIni); RegPLC inst = new RegPLC() { No = i, NameReg = nameReg, NameCustomReg = customReg, PLCSend = PLCSend, PCSend = PCSend, DblMin = min, DblMax = max, Co = co, Annotation = annotation, //Explain = Annotation[i], }; Reg_L.Add(inst); } //生成写入PLC的寄存器 CreateRegWrite(); } catch (Exception ex) { blRight = false; Log.L_I.WriteError(NameClass, ex); } return(blRight); }
private void btnSave_Click(object sender, RoutedEventArgs e) { try { //权限返回 if (!EngineerReturn()) { return; } //备份文件到本地 FunBackup.F_I.BackupPLC(); for (int i = 0; i < g_RegPLCBase.NumReg; i++) { RegPLC rItem = dgReg.Items[i] as RegPLC; g_RegPLCBase.Reg_L[i].DblMin = rItem.DblMin; g_RegPLCBase.Reg_L[i].DblMax = rItem.DblMax; g_RegPLCBase.Reg_L[i].Co = rItem.Co; g_RegPLCBase.Reg_L[i].Annotation = rItem.Annotation; if (g_RegPLCBase.Reg_L[i].NameCustomReg.Trim() != "") { g_RegPLCBase.Reg_L[i].NameReg = g_RegPLCBase.Reg_L[i].NameCustomReg.ToUpper().Replace("N", "n"); g_RegPLCBase.Reg_L[i].NameCustomReg = g_RegPLCBase.Reg_L[i].NameCustomReg.ToUpper().Replace("N", "n"); } else { g_RegPLCBase.Reg_L[i].NameReg = g_RegPLCBase.Reg_L[i].NameReg.ToUpper().Replace("N", "n"); } } if (g_RegPLCBase.WriteIni()) { this.btnSave.RefreshDefaultColor("保存成功", true); SaveEvent(); } else { this.btnSave.RefreshDefaultColor("保存失败", false); } ShowPar_Invoke();//重新刷新显示 } catch (Exception ex) { Log.L_I.WriteError("BaseUCReg", ex); MessageBox.Show("配置出错!"); } }
/// <summary> /// 最终调用 /// </summary> double ConvertReadData(int value1, int value2, RegPLC regPLC) { g_MtConvertRead.WaitOne(); try { return(Math.Round(ConvertReadData(value1, value2) * regPLC.Co, 4)); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(int.MaxValue); } finally { g_MtConvertRead.ReleaseMutex(); } }
/// <summary> /// 清除PLC寄存器 /// </summary> public void ClearPLC(RegPLC regPLC) { g_mtClear.WaitOne(); try { //离线,不写入PLC if (RegeditPLC.R_I.BlOffLinePLC) { return; } ClearPLC(regPLC.NameReg.Replace("\\n", "\n")); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } finally { g_mtClear.ReleaseMutex(); } }
bool ConvertJudgeData_Write(RegPLC reg, double data, out double realData) { g_mtConvertWrite.WaitOne(); realData = 0; string strError = ""; try { if (data > reg.DblMax || data < reg.DblMin) { strError = reg.NameReg + ",Max:" + reg.DblMax + ",Min:" + reg.DblMin + ",Real:" + data.ToString(); WriteDataOverFlow(strError);//数据超限 return(false); } else { //为了兼容老版本的发送是乘以系数,新版本发送是除以系数 if (reg.Co > 2) { realData = data * reg.Co; } else { realData = data / reg.Co; } } return(true); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } finally { g_mtConvertWrite.ReleaseMutex(); } }
/// <summary> /// 可以批量设置选择的寄存器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGetReg_Click(object sender, RoutedEventArgs e) { try { base.IndexP = this.dgPLCReg.SelectedIndex; base.IntPosP = this.dgDataAndReg.SelectedIndex; if (dgPLCReg.SelectedItems.Count == 0) { g_ParCommWritePLC[base.IntPosP].Reg = RegWrite_L[base.IndexP].NameReg; g_ParCommWritePLC[base.IntPosP].DblMax = RegWrite_L[base.IndexP].DblMax; g_ParCommWritePLC[base.IntPosP].DblMin = RegWrite_L[base.IndexP].DblMin; g_ParCommWritePLC[base.IntPosP].Co = RegWrite_L[base.IndexP].Co; g_ParCommWritePLC[base.IntPosP].Annotation = RegWrite_L[base.IndexP].Explain; } else { for (int i = 0; i < dgPLCReg.SelectedItems.Count; i++) { RegPLC inst = (RegPLC)dgPLCReg.SelectedItems[i]; g_ParCommWritePLC[base.IntPosP + i].Reg = inst.NameReg; g_ParCommWritePLC[base.IntPosP + i].DblMax = inst.DblMax; g_ParCommWritePLC[base.IntPosP + i].DblMin = inst.DblMin; g_ParCommWritePLC[base.IntPosP + i].Co = inst.Co; g_ParCommWritePLC[base.IntPosP + i].Annotation = inst.Explain; } } //刷新显示 RefreshDgDataAndReg(); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } }
/// <summary> /// 按照两个D寄存器进行生成 /// </summary> public virtual void CreateRegTwo() { try { int intSum = g_RegPLCBase.regStart.Length; int intNo = int.Parse(g_RegPLCBase.regStart.Substring(1, intSum - 1)); string strLetter = g_RegPLCBase.regStart.Substring(0, 1); //是否清除注释 if ((bool)chkClearCoAnno.IsChecked) { g_RegPLCBase.Reg_L.Clear(); for (int i = 0; i < g_RegPLCBase.NumReg; i++) { string PLCSendPC = ""; string PCSendPLC = ""; string Annotation = ""; double Co = 1; if (g_RegPLCBase.PLCSendPC != null) { PLCSendPC = g_RegPLCBase.PLCSendPC[i]; } if (g_RegPLCBase.PCSendPLC != null) { PCSendPLC = g_RegPLCBase.PCSendPLC[i]; } if (g_RegPLCBase.Annotation != null) { Annotation = g_RegPLCBase.Annotation[i]; } if (g_RegPLCBase.Co != null) { Co = g_RegPLCBase.Co[i]; } //选择读取寄存器为单个或是两个 RegPLC inst = new RegPLC() { No = i, NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n", NameCustomReg = "", PLCSend = PLCSendPC, PCSend = PCSendPLC, Annotation = Annotation, Explain = Annotation, Co = Co, DblMax = int.MaxValue, DblMin = int.MinValue }; g_RegPLCBase.Reg_L.Add(inst); } } else { if (dgReg.Items.Count < g_RegPLCBase.NumReg) { for (int i = 0; i < dgReg.Items.Count; i++) { RegPLC rItem = dgReg.Items[i] as RegPLC; g_RegPLCBase.Reg_L[i].NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n"; g_RegPLCBase.Reg_L[i].NameCustomReg = rItem.NameCustomReg; g_RegPLCBase.Reg_L[i].Co = rItem.Co; g_RegPLCBase.Reg_L[i].DblMin = rItem.DblMin; g_RegPLCBase.Reg_L[i].DblMax = rItem.DblMax; g_RegPLCBase.Reg_L[i].Annotation = rItem.Annotation; g_RegPLCBase.Reg_L[i].Explain = rItem.Annotation; } for (int i = dgReg.Items.Count; i < g_RegPLCBase.NumReg; i++) { RegPLC inst = new RegPLC() { No = i, NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n", NameCustomReg = g_RegPLCBase.Reg_L[i].NameCustomReg, PLCSend = g_RegPLCBase.PLCSendPC[i], PCSend = g_RegPLCBase.PCSendPLC[i], Annotation = "", Explain = "", Co = g_RegPLCBase.Co[i], DblMax = int.MaxValue, DblMin = int.MinValue }; g_RegPLCBase.Reg_L.Add(inst); } } else { for (int i = 0; i < g_RegPLCBase.NumReg; i++) { RegPLC rItem = dgReg.Items[i] as RegPLC; g_RegPLCBase.Reg_L[i].NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n"; g_RegPLCBase.Reg_L[i].NameCustomReg = rItem.NameCustomReg; g_RegPLCBase.Reg_L[i].Co = rItem.Co; g_RegPLCBase.Reg_L[i].DblMin = rItem.DblMin; g_RegPLCBase.Reg_L[i].DblMax = rItem.DblMax; g_RegPLCBase.Reg_L[i].Annotation = rItem.Annotation; g_RegPLCBase.Reg_L[i].Explain = rItem.Annotation; } } } } catch (Exception ex) { Log.L_I.WriteError("BaseUCReg", ex); } }
public override void CreateRegTwo() { try { if (ParSetPLC.P_I.TypePLCProtocol_e == TypePLCProtocol_enum.FullNew)//精简版本协议 { int intSum = g_RegPLCBase.regStart.Length; int intNo = int.Parse(g_RegPLCBase.regStart.Substring(1, intSum - 1)); string strLetter = g_RegPLCBase.regStart.Substring(0, 1); //是否清除注释 if ((bool)chkClearCoAnno.IsChecked) { g_RegPLCBase.Reg_L.Clear(); for (int i = 0; i < g_RegPLCBase.NumReg; i++) { string PLCSendPC = ""; string PCSendPLC = ""; string Annotation = ""; double Co = 1; if (g_RegPLCBase.PLCSendPC != null) { PLCSendPC = g_RegPLCBase.PLCSendPC[i]; } if (g_RegPLCBase.PCSendPLC != null) { PCSendPLC = g_RegPLCBase.PCSendPLC[i]; } if (g_RegPLCBase.Annotation != null) { Annotation = g_RegPLCBase.Annotation[i]; } if (g_RegPLCBase.Co != null) { Co = g_RegPLCBase.Co[i]; } if (i < RegMonitor.R_I.NumTrigger) { //选择读取寄存器为单个或是两个 RegPLC inst = new RegPLC() { No = i, NameReg = strLetter + (intNo + i).ToString() + "\\n", NameCustomReg = "", PLCSend = PLCSendPC, PCSend = PCSendPLC, Annotation = Annotation, Explain = Annotation, Co = Co, DblMax = int.MaxValue, DblMin = int.MinValue }; g_RegPLCBase.Reg_L.Add(inst); } else { //选择读取寄存器为单个或是两个 RegPLC inst = new RegPLC() { No = i, NameReg = strLetter + (intNo + RegMonitor.R_I.NumTrigger + 2 * (i - RegMonitor.R_I.NumTrigger)).ToString() + "\\n" + strLetter + (intNo + RegMonitor.R_I.NumTrigger + 2 * (i - +RegMonitor.R_I.NumTrigger) + 1).ToString() + "\\n", NameCustomReg = "", PLCSend = PLCSendPC, PCSend = PCSendPLC, Annotation = Annotation, Explain = Annotation, Co = Co, DblMax = int.MaxValue, DblMin = int.MinValue }; g_RegPLCBase.Reg_L.Add(inst); } } } else { if (dgReg.Items.Count < g_RegPLCBase.NumReg) { for (int i = 0; i < dgReg.Items.Count; i++) { RegPLC rItem = dgReg.Items[i] as RegPLC; g_RegPLCBase.Reg_L[i].NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n"; g_RegPLCBase.Reg_L[i].NameCustomReg = rItem.NameCustomReg; g_RegPLCBase.Reg_L[i].Co = rItem.Co; g_RegPLCBase.Reg_L[i].DblMin = rItem.DblMin; g_RegPLCBase.Reg_L[i].DblMax = rItem.DblMax; g_RegPLCBase.Reg_L[i].Annotation = rItem.Annotation; g_RegPLCBase.Reg_L[i].Explain = rItem.Annotation; } for (int i = dgReg.Items.Count; i < g_RegPLCBase.NumReg; i++) { RegPLC inst = new RegPLC() { No = i, NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n", NameCustomReg = g_RegPLCBase.Reg_L[i].NameCustomReg, PLCSend = g_RegPLCBase.PLCSendPC[i], PCSend = g_RegPLCBase.PCSendPLC[i], Annotation = "", Explain = "", Co = g_RegPLCBase.Co[i], DblMax = int.MaxValue, DblMin = int.MinValue }; g_RegPLCBase.Reg_L.Add(inst); } } else { for (int i = 0; i < g_RegPLCBase.NumReg; i++) { RegPLC rItem = dgReg.Items[i] as RegPLC; g_RegPLCBase.Reg_L[i].NameReg = strLetter + (intNo + 2 * i).ToString() + "\\n" + strLetter + (intNo + 2 * i + 1).ToString() + "\\n"; g_RegPLCBase.Reg_L[i].NameCustomReg = rItem.NameCustomReg; g_RegPLCBase.Reg_L[i].Co = rItem.Co; g_RegPLCBase.Reg_L[i].DblMin = rItem.DblMin; g_RegPLCBase.Reg_L[i].DblMax = rItem.DblMax; g_RegPLCBase.Reg_L[i].Annotation = rItem.Annotation; g_RegPLCBase.Reg_L[i].Explain = rItem.Annotation; } } } } else { base.CreateRegTwo(); } } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } }
public BaseRegPLC ReadExcel(string path) { BaseRegPLC baseRegPLC = new BaseRegPLC(); try { CloseProcess("EXCEL"); Excel.Application xlsApp = new Excel.Application(); //创建Application对象 Excel.Workbook xlsBook; object missing = Missing.Value; xlsBook = xlsApp.Workbooks.Open(path, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); Excel.Worksheet xlsSheet = (Excel.Worksheet)xlsBook.ActiveSheet; //起始寄存器 Excel.Range range = (Excel.Range)xlsSheet.Cells[1, 2]; baseRegPLC.regStart = range.Value2.ToString(); range = (Excel.Range)xlsSheet.Cells[2, 2]; baseRegPLC.NumRegSet = int.Parse(range.Value2.ToString()); for (int i = 0; i < baseRegPLC.NumRegSet; i++) { RegPLC regPLC = new RegPLC(); try { range = (Excel.Range)xlsSheet.Cells[i + 4, 1]; string low = range.Value2.ToString(); range = (Excel.Range)xlsSheet.Cells[i + 4, 2]; string high = range.Value2.ToString(); regPLC.NameReg = low + high; try { range = (Excel.Range)xlsSheet.Cells[i + 4, 3]; regPLC.PLCSend = range.Value2.ToString(); } catch (Exception ex) { } try { range = (Excel.Range)xlsSheet.Cells[i + 4, 4]; regPLC.PCSend = range.Value2.ToString(); } catch (Exception ex) { } range = (Excel.Range)xlsSheet.Cells[i + 4, 5]; regPLC.DblMin = double.Parse(range.Value2.ToString()); range = (Excel.Range)xlsSheet.Cells[i + 4, 6]; regPLC.DblMax = double.Parse(range.Value2.ToString()); range = (Excel.Range)xlsSheet.Cells[i + 4, 7]; regPLC.Co = double.Parse(range.Value2.ToString()); try { range = (Excel.Range)xlsSheet.Cells[i + 4, 8]; regPLC.Annotation = range.Value2.ToString(); } catch (Exception ex) { } regPLC.NameCustomReg = ""; regPLC.No = i; } catch (Exception) { MessageBox.Show("Excel寄存器配置有误"); return(baseRegPLC); } baseRegPLC.Reg_L.Add(regPLC); } try { xlsSheet = null; xlsBook.Close(true, Type.Missing, Type.Missing); xlsBook = null; xlsApp.Quit(); xlsApp = null; } catch (Exception ex) { } return(baseRegPLC); } catch (Exception ex) { return(null); } finally { CloseProcess("EXCEL"); } }