private void WriteOmron() { try { if (Omron.ConnectionState) { if (Omron.WriteTagValue(txt_node.Text, txt_value.Text)) { lbl_result.Text = "结果:写入成功!"; return; } } lbl_result.Text = "结果:连接断开"; } catch (Exception ex) { lbl_result.Text = "结果:写入异常!原因:" + ex.Message; } }
private void PC_PLC_SignalExchange_Omron(CancellationToken token, bool v) { while (!token.IsCancellationRequested) { #region 判断plc是否断线 if (!IsConnectPLC) { UpdateShowMessage("正在连接PLC……"); new Task(() => ConnectOmronPLC()).Start(); Thread.Sleep(3000); continue; } #endregion Thread.Sleep(1); #region 发送握手信号 if (IsHandshake) { if (IsStart) { IsOne = !IsOne; PLCPulse = IsOne ? 1 : 2; IsHandshake = false; bool back = false; back = PLCOmron.WriteTagValue(null, PLCPulse); if (!back) { IsConnectPLC = false; PLCPulse = 0; } } else { PLCPulse = 0; } } #endregion #region 与PLC 交互 if (v) { try { object isRead; PLCOmron.ReadTagValue(null, out isRead); ReadTRG = (1 == (int)isRead) ? 1 : 0; if (1 != ReadTRG) { OldReadTRG = 0; } else { if (0 < ReadTRG - OldReadTRG) { //读取数据 ReadDataFromPLC_Omron(); } OldReadTRG = 1; } } catch (Exception ex) { UpdateShowMessage("读取PLC 数据异常:" + ex.Message); LogHelper.Fatal("与plc交互异常!", ex); } } #endregion } }