private void buttonWritePlc_Click(object sender, EventArgs e) { string addr = this.textBoxPlcAddr.Text; int val = int.Parse(this.textBoxPlcVal.Text); if (!plcRwObj2.WriteDB(addr, val)) { AddLog("PLC 写入地址:" + addr + "失败," + plcRwObj2.GetLastErrorinfo()); } else { AddLog("PLC 写入地址:" + addr + "成功"); } }
private void BusinessLoop()//List<CtlNodeBaseModel> myNodeList { try { DateTime commSt = System.DateTime.Now; TimeSpan ts = commSt - this.lastStTime; string dispCommInfo = string.Format("PLC通信周期:{0}毫秒", (int)ts.TotalMilliseconds); view.DispCommInfo(dispCommInfo); if (ts.TotalMilliseconds > 500) { logRecorder.AddDebugLog(objectName, dispCommInfo); } lastStTime = commSt; if (!NodeFactory.SimMode) { PLCRwMCPro plcRW = plcRWs[0] as PLCRwMCPro; string heartAddr = "D2700"; if (!plcRW.WriteDB(heartAddr, 1)) { Console.WriteLine("PLC通信失败!"); } //Console.WriteLine("P2"); //return; if (SysCfgModel.PlcCommSynMode) { return; } #region 读DB2 short[] tempDb2Vals = new short[SysCfgModel.DB2Len]; if (!plcRW.ReadMultiDB(SysCfgModel.DB2Start, SysCfgModel.DB2Len, ref tempDb2Vals)) { // logRecorder.AddLog(new LogModel(objectName, "PLC通信失败!", EnumLoglevel.错误)); Console.WriteLine("PLC通信失败!"); string reStr = ""; plcRW.CloseConnect(); if (!plcRW.ConnectPLC(ref reStr)) { // logRecorder.AddLog(new LogModel(objectName, "PLC重新连接失败!", EnumLoglevel.错误)); Console.WriteLine("PLC重新连接失败!"); foreach (CtlNodeBaseModel node in nodeList) { node.CurrentStat.Status = EnumNodeStatus.设备故障; node.CurrentStat.StatDescribe = "PLC通信断开"; } return; } else { logRecorder.AddLog(new LogModel(objectName, "PLC重新连接成功!", EnumLoglevel.错误)); return; } } plcRW.DB2Switch(tempDb2Vals); #endregion #region 写DB1 short[] tempDB1ValsSnd = new short[SysCfgModel.DB1Len]; plcRW.DB1Switch(ref tempDB1ValsSnd); if (!plcRW.WriteMultiDB(SysCfgModel.DB1Start, SysCfgModel.DB1Len, tempDB1ValsSnd)) { //logRecorder.AddLog(new LogModel(objectName, "PLC通信失败!", EnumLoglevel.错误)); Console.WriteLine("PLC重新连接失败!"); string reStr = ""; plcRW.CloseConnect(); if (!plcRW.ConnectPLC(ref reStr)) { //logRecorder.AddLog(new LogModel(objectName, "PLC重新连接失败!", EnumLoglevel.错误)); Console.WriteLine("PLC重新连接失败!"); return; } else { logRecorder.AddLog(new LogModel(objectName, "PLC重新连接成功!", EnumLoglevel.错误)); return; } } plcRW.PlcRWStatUpdate(); #endregion return; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }