예제 #1
0
 private void ReadOmron()
 {
     try
     {
         if (Omron.ConnectionState)
         {
             object value;
             Omron.ReadTagValue(txt_node.Text, out value);
             lbl_result.Text = "结果:" + value.ToString();
             return;
         }
         lbl_result.Text = "结果:连接断开";
     }
     catch (Exception ex)
     {
         lbl_result.Text = "结果:读取异常!原因:" + ex.Message;
     }
 }
예제 #2
0
        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
            }
        }