public void Start(object node) { int AddrOffset = 1280; int CstRobotStation = 9; Node Target = (Node)node; //SpinWait.SpinUntil(() => Target.GetController().GetStatus().Equals("Connected"), 9999999); //McProtocolTcp PLC = new McProtocolTcp("192.168.3.39", 2000); McProtocolTcp PLC = new McProtocolTcp(this.IPAdress, this.Port); this._IsConnected = true; byte[] result = new byte[512]; int[] WResult = new int[32]; bool isInit = false; int RecieveIndex_1 = 0; int RecieveIndex_2 = 0; //PLC.SetBitDevice(PlcDeviceType.Y, new Dictionary<int, byte>() { { 1280,1}, { 1281, 1 }, { 1285, 1 } }); while (true) { try { if (!isInit) { PLC.Open(); //INIT result = new byte[512]; PLC.GetBitDevice(PlcDeviceType.Y, AddrOffset, 512, result); Target.SetIO("OUTPUT", result); result = new byte[512]; Target.SetIO("OUTPUT_OLD", result); result = new byte[512]; PLC.GetBitDevice(PlcDeviceType.X, AddrOffset, 512, result); Target.SetIO("INPUT", result); result = new byte[512]; Target.SetIO("INPUT_OLD", result); WResult = new int[32]; PLC.ReadDeviceBlock(PlcDeviceType.D, 24576 + 1, 32, WResult); result = ConvertToBit(WResult); Target.SetIO("PRESENCE", result); result = new byte[512]; Target.SetIO("PRESENCE_OLD", result); WResult = new int[2]; PLC.ReadDeviceBlock(PlcDeviceType.D, 25856, 2, WResult); RecieveIndex_1 = WResult[0]; RecieveIndex_2 = WResult[1]; isInit = true; } if (!ch1Send.Equals("")) { int[] SendDataBytes = ByteArrayToIntArray(Encoding.ASCII.GetBytes(ch1Send)); PLC.WriteDeviceBlock(PlcDeviceType.D, 24848, SendDataBytes.Length, SendDataBytes); PLC.WriteDeviceBlock(PlcDeviceType.D, 24834, 1, new int[] { SendDataBytes.Length }); PLC.SetBitDevice(PlcDeviceType.Y, 1776, 1, new byte[] { 1 }); ch1Send = ""; } if (!ch2Send.Equals("")) { int[] SendDataBytes = ByteArrayToIntArray(Encoding.ASCII.GetBytes(ch2Send)); PLC.WriteDeviceBlock(PlcDeviceType.D, 25104, SendDataBytes.Length, SendDataBytes); PLC.WriteDeviceBlock(PlcDeviceType.D, 25090, 1, new int[] { SendDataBytes.Length }); PLC.SetBitDevice(PlcDeviceType.Y, 1778, 1, new byte[] { 1 }); ch2Send = ""; } //SpinWait.SpinUntil(() => false, 10); WResult = new int[90]; PLC.ReadDeviceBlock(PlcDeviceType.D, 25856, 2, WResult); if (RecieveIndex_1 != WResult[0]) { RecieveIndex_1 = WResult[0]; int[] WResult1 = new int[90]; PLC.ReadDeviceBlock(PlcDeviceType.D, 25360, 90, WResult1); string rData1 = ""; foreach (int dec in WResult1) { rData1 += dec.ToString("X4").Substring(2, 2) + dec.ToString("X4").Substring(0, 2); } rData1 = Encoding.ASCII.GetString(StringToByteArray(rData1)).Trim('\0'); //On_Connection_Message(rData1); ThreadPool.QueueUserWorkItem(new WaitCallback(On_Connection_Message), rData1); } if (RecieveIndex_2 != WResult[1]) { RecieveIndex_2 = WResult[1]; int[] WResult1 = new int[90]; PLC.ReadDeviceBlock(PlcDeviceType.D, 25616, 90, WResult1); string rData2 = ""; foreach (int dec in WResult1) { rData2 += dec.ToString("X4").Substring(2, 2) + dec.ToString("X4").Substring(0, 2); } rData2 = Encoding.ASCII.GetString(StringToByteArray(rData2)).Trim('\0'); //On_Connection_Message(rData2); ThreadPool.QueueUserWorkItem(new WaitCallback(On_Connection_Message), rData2); } if (!Target.GetIO("OUTPUT").SequenceEqual(Target.GetIO("OUTPUT_OLD"))) { Dictionary <int, byte> changedList = new Dictionary <int, byte>(); for (int i = 0; i < Target.GetIO("OUTPUT").Length; i++) { if (Target.GetIO("OUTPUT")[i] != Target.GetIO("OUTPUT_OLD")[i]) { changedList.Add(i + AddrOffset, Target.GetIO("OUTPUT")[i]); //_TaskReport.On_Message_Log("IO", "Y Area [" + (i + AddrOffset).ToString("X4") + "] " + Target.GetIO("OUTPUT_OLD")[i] + "->" + Target.GetIO("OUTPUT")[i]); Target.SetIO("OUTPUT_OLD", i, Target.GetIO("OUTPUT")[i]); //UpdateUI("OUTPUT", i, Target.GetIO("OUTPUT")[i], Target); _ReportTarget.On_DIO_Data_Chnaged(i.ToString(), Target.GetIO("OUTPUT")[i].ToString(), "OUTPUT"); } } PLC.SetBitDevice(PlcDeviceType.Y, changedList); } PLC.GetBitDevice(PlcDeviceType.X, AddrOffset, 512, result); Target.SetIO("INPUT", result); if (!Target.GetIO("INPUT").SequenceEqual(Target.GetIO("INPUT_OLD"))) { for (int i = 0; i < Target.GetIO("INPUT").Length; i++) { if (Target.GetIO("INPUT")[i] != Target.GetIO("INPUT_OLD")[i]) { //_TaskReport.On_Message_Log("IO", "X Area [" + (i + AddrOffset).ToString("X4") + "] " + Target.GetIO("INPUT_OLD")[i] + "->" + Target.GetIO("INPUT")[i]); //UpdateUI("INPUT", i, Target.GetIO("INPUT")[i], Target); _ReportTarget.On_DIO_Data_Chnaged(i.ToString(), Target.GetIO("INPUT")[i].ToString(), "INPUT"); } } if (Target.GetIO("INPUT")[6 + (CstRobotStation - 1) * 32] == 1 && Target.GetIO("INPUT_OLD")[6 + (CstRobotStation - 1) * 32] == 0) { string errAry = ""; for (int i = 32; i <= 64; i++) { errAry += Target.GetIO("INPUT")[i + (CstRobotStation - 1) * 32].ToString(); } string error = new String(errAry.Reverse().ToArray()); error = Convert.ToInt32(error, 2).ToString("X"); _ReportTarget.On_Alarm_Happen(AlarmManagement.NewAlarm(Target, error, "")); } Target.SetIO("INPUT_OLD", Target.GetIO("INPUT")); } WResult = new int[32]; PLC.ReadDeviceBlock(PlcDeviceType.D, 24576 + 1, 32, WResult); result = ConvertToBit(WResult); Target.SetIO("PRESENCE", result); if (!Target.GetIO("PRESENCE").SequenceEqual(Target.GetIO("PRESENCE_OLD"))) { for (int i = 0; i < Target.GetIO("PRESENCE").Length; i++) { if (Target.GetIO("PRESENCE")[i] != Target.GetIO("PRESENCE_OLD")[i]) { //_TaskReport.On_Message_Log("IO", "X Area [" + (i + AddrOffset).ToString("X4") + "] " + Target.GetIO("INPUT_OLD")[i] + "->" + Target.GetIO("INPUT")[i]); //UpdateUI("PRESENCE", i, Target.GetIO("PRESENCE")[i], Target); _ReportTarget.On_DIO_Data_Chnaged(i.ToString(), Target.GetIO("PRESENCE")[i].ToString(), "PRESENCE"); } } Target.SetIO("PRESENCE_OLD", Target.GetIO("PRESENCE")); } } catch (Exception e) { _ReportTarget.On_Message_Log("IO", "Lost connection with PLC"); SpinWait.SpinUntil(() => false, 5000); try { if (isInit) { PLC.Open(); } } catch (Exception eeee) { _ReportTarget.On_Message_Log("IO", eeee.StackTrace); } } } }