Пример #1
0
        public void ConnectToPlc()
        {
            string[] ipArray;
            byte[]   values;
            deviceDriver = new OmronFinsNet(_device.Ip, _device.Port)
            {
                ConnectTimeOut = 2000
            };
            ipArray          = _device.Ip.Split('.');
            values           = System.BitConverter.GetBytes(int.Parse(ipArray[ipArray.Length - 1]));
            deviceDriver.SA1 = values[0]; // PC网络号,PC的IP地址的最后一个数.假如你的电脑的Ip地址为192.168.0.13,那么这个值就是13
            ipArray          = _device.Ip.Split('.');
            values           = System.BitConverter.GetBytes(int.Parse(ipArray[ipArray.Length - 1]));
            deviceDriver.DA1 = values[0]; // 0x10 PLC网络号,PLC的IP地址的最后一个数.假如你的PLC的Ip地址为192.168.0.10,那么这个值就是10
            //omronTcpNet.SA1 = 0x12; // PC网络号,PC的IP地址的最后一个数
            //omronTcpNet.DA1 = 0x10; // PLC网络号,PLC的IP地址的最后一个数
            deviceDriver.DA2 = 0x00;                // PLC单元号,通常为0
            deviceDriver.ConnectServer();
            deviceDriver.SetPersistentConnection(); // 设置长连接
            OperateResult res = deviceDriver.ConnectServer();

            if (res.IsSuccess)
            {
                _log.WriteLog($"{_device.Name}{_device.Ip}连接成功");
            }
            else
            {
                _log.WriteLog($"{_device.Name}{_device.Ip}连接失败");
            }
            _device.IsConnected = res.IsSuccess;
        }
Пример #2
0
        public Result Connect(Commor commor)
        {
            var omronFinsNet   = new OmronFinsNet();
            var ethernetCommor = (EthernetCommor)commor.Communicator;

            try
            {
                omronFinsNet.ConnectTimeOut           = 2000;
                omronFinsNet.IpAddress                = ethernetCommor.IP;
                omronFinsNet.Port                     = ethernetCommor.Port;
                omronFinsNet.SA1                      = NetHelper.GetIpLastValue(NetHelper.GetLocalIpByRegex("192.168.1.*"));;
                omronFinsNet.DA2                      = 0;
                omronFinsNet.ByteTransform.DataFormat = (HslCommunication.Core.DataFormat)Enum.Parse(typeof(HslCommunication.Core.DataFormat), "CDAB");

                OperateResult connect = omronFinsNet.ConnectServer();
                if (connect.IsSuccess)
                {
                    commor.Connector = omronFinsNet;
                    return(Result.Success);
                }
                else
                {
                    return(new Result(string.Format("连接{0}失败({1} :{2})", ethernetCommor.Name, ethernetCommor.IP, ethernetCommor.Port)));
                }
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
        }
Пример #3
0
        public bool Connect(out string msg)
        {
            msg = "";
            if (!Net.IsPingSuccess(this.IP))
            {
                msg = "无法连接到 " + this.IP;
                return(false);
            }

            if (omron_net == null)
            {
                omron_net = new OmronFinsNet(this.IP, this.Port);
                var ips = this.IP.Split('.');
                omron_net.SA1 = Net.GetIpLastValue(Net.GetLocalIpByRegex(string.Format("{0}.{1}.{2}.*", ips[0], ips[1], ips[2]))); // PC网络号,PC的IP地址的最后一个数
                omron_net.DA1 = Net.GetIpLastValue(this.IP);                                                                       // PLC网络号,PLC的IP地址的最后一个数0
                omron_net.DA2 = 0x00;                                                                                              // PLC单元号,通常为0

                omron_net.ReceiveTimeOut = 1000;
                omron_net.ConnectTimeOut = 1000;
                OperateResult result = omron_net.ConnectServer();
                System.Diagnostics.Debug.WriteLine("连接欧姆龙 :" + result.IsSuccess);
                if (!result.IsSuccess)
                {
                    msg = result.Message;
                    this.IsConnected = false;
                    return(false);
                }
            }
            this.IsConnected = true;
            return(true);
        }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show(DemoUtils.IpAddressInputWrong);
                return;
            }

            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show(DemoUtils.PortInputWrong);
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte SA1))
            {
                MessageBox.Show("SA1 Input Wrong!");
                return;
            }


            if (!byte.TryParse(textBox16.Text, out byte DA2))
            {
                MessageBox.Show("PLC DA2 input wrong!");
                return;
            }

            if (!byte.TryParse(textBox17.Text, out byte DA1))
            {
                MessageBox.Show("PLC DA1 input wrong!");
                return;
            }

            omronFinsNet.IpAddress = textBox1.Text;
            omronFinsNet.Port      = port;
            omronFinsNet.SA1       = SA1;
            omronFinsNet.DA1       = DA1;
            omronFinsNet.DA2       = DA2;
            omronFinsNet.ByteTransform.DataFormat = (HslCommunication.Core.DataFormat)comboBox1.SelectedItem;

            // OperateResult connect = OperateResult.CreateSuccessResult( );
            OperateResult connect = omronFinsNet.ConnectServer( );

            if (connect.IsSuccess)
            {
                MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
                button2.Enabled = true;
                button1.Enabled = false;
                panel2.Enabled  = true;

                userControlCurve1.ReadWriteNet = omronFinsNet;
            }
            else
            {
                MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed);
            }
        }
Пример #5
0
        /// <summary>
        /// 连接PLC
        /// </summary>
        public bool openConnectPLC()
        {
            OperateResult connect = omronFinsNet.ConnectServer();

            if (connect.IsSuccess)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show("Ip地址输入不正确!");
                return;
            }

            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show("端口号输入不正确!");
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte SA1))
            {
                MessageBox.Show("本机网络号输入不正确!");
                return;
            }

            omronFinsNet.IpAddress = textBox1.Text;
            omronFinsNet.Port      = port;
            omronFinsNet.SA1       = SA1;

            try
            {
                OperateResult connect = omronFinsNet.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show("连接成功!");
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;
                }
                else
                {
                    MessageBox.Show("连接失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #7
0
 public bool Connect()
 {
     if (omr != null)
     {
         omr.ConnectClose();
         omr = null;
     }
     omr = new OmronFinsNet(Properties.Settings.Default.PLC_IP, Properties.Settings.Default.PLC_PORT)
     {
         SA1 = 0,
         DA1 = 0
     };
     Task.Factory.StartNew(() =>
     {
         while (true)
         {
             var op      = omr.ConnectServer();
             IsConnected = op.IsSuccess;
             if (!op.IsSuccess)
             {
                 Events.Publish(new MsgItem {
                     Time = DateTime.Now, Level = "E", Value = op.Message
                 });
             }
             if (IsConnected)
             {
                 foreach (var v in AM.alarms)
                 {
                     var b = omr.ReadBool(v.Address);
                     if (b.IsSuccess)
                     {
                         if (v.Value != b.Content)
                         {
                             v.Value = b.Content;
                             Events.Publish(v);
                         }
                     }
                 }
                 omr.ConnectClose();
             }
             System.Threading.Thread.Sleep(10);
         }
     });
     return(IsConnected);
 }
Пример #8
0
        public override void DeviceConn(OmronFinsNetConfig config)
        {
            OmronFinsNet omronFinsNet = new OmronFinsNet();

            omronFinsNet.ConnectTimeOut             = config.ConnectTimeOut;
            omronFinsNet.IpAddress                  = config.IP;
            omronFinsNet.Port                       = config.Port;
            omronFinsNet.SA1                        = config.SA1;
            omronFinsNet.SA2                        = config.SA2;
            omronFinsNet.ByteTransform.DataFormat   = (HslCommunication.Core.DataFormat)config.ByteTransformDataFormat;
            omronFinsNet.IsChangeSA1AfterReadFailed = config.IsChangeSA1AfterReadFailed;
            OperateResult connect = omronFinsNet.ConnectServer();

            NetworkDevice = omronFinsNet;
            if (!connect.IsSuccess)
            {
                throw new Exception("Connect Failed");
            }
        }
Пример #9
0
        public bool OpenPLC(string IPAddress, Byte sa1, Byte da1, Byte da2)
        {
            try
            {
                _OmronFinsNet     = new OmronFinsNet(IPAddress, 9600);
                _OmronFinsNet.SA1 = sa1;
                _OmronFinsNet.DA1 = da1;
                _OmronFinsNet.DA2 = da2;

                //_OmronFinsNet.ConnectTimeOut = 5000;
                //_SiementsTcpNet.Port = 102;
                OperateResult result = _OmronFinsNet.ConnectServer();

                //MessageBox.Show((result.IsSuccess).ToString());

                if (result.IsSuccess)
                {
                    //AtuoRun = true;
                    //MessageBox.Show("连接PLC成功");

                    //NetLog.WriteTextLog("连接PLC成功");
                    PLCIsopen = result.IsSuccess;
                    return(true);
                }
                else
                {
                    //MessageBox.Show("连接PLC失败");

                    //NetLog.WriteTextLog("连接PLC失败");
                    PLCIsopen = result.IsSuccess;
                    return(false);
                }
            }
            catch (Exception ex)
            {
                PLCIsopen = false;
                //MessageBox.Show(ex.Message);

                //NetLog.WriteTextLog("连接PLC错误"+ex.Message);
                return(false);
            }
        }
 protected override bool Connect()
 {
     try
     {
         // PLC.ConnectTimeOut = 1000;
         OperateResult opres = PLC.ConnectServer();
         if (opres.IsSuccess)
         {
             LOG.Info($"Connect to OmronDataSource [{SourceName}] success.");
         }
         else
         {
             LOG.Warn($"Connect to OmronDataSource [{SourceName}] failed.");
         }
         return(opres.IsSuccess);
     }
     catch (Exception ex)
     {
         LOG.Error($"Connect to [{SourceName}] failed. Message [{ex.Message}]");
         return(false);
     }
 }
Пример #11
0
        public MainWindow()
        {
            InitializeComponent();
            #region 启动时串口最大化显示
            this.WindowState = WindowState.Maximized;
            Rect rc = SystemParameters.WorkArea; //获取工作区大小
            //this.Topmost = true;
            this.Left   = 0;                     //设置位置
            this.Top    = 0;
            this.Width  = rc.Width;
            this.Height = rc.Height;
            #endregion

            LoadJsonData();
            Init();

            //plc = new SiemensS7Net(SiemensPLCS.S1200, config.IpAddress)
            //{
            //    ConnectTimeOut = 5000
            //};

            plc = new OmronFinsNet(config.IpAddress, config.IpPort)
            {
                SA1            = 10,
                ConnectTimeOut = 5000
            };


            connect = plc.ConnectServer();

            #region PLC连接定时器
            //timer = new System.Windows.Threading.DispatcherTimer();
            //timer.Tick += new EventHandler(ThreadCheck);
            //timer.Interval = new TimeSpan(0, 0, 0, 5);
            //timer.Start();
            #endregion
        }
Пример #12
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show(DemoUtils.PortInputWrong);
                return;
            }

            if (!byte.TryParse(textBox16.Text, out byte DA2))
            {
                MessageBox.Show("PLC DA2 input wrong!");
                return;
            }

            omronFinsNet.IpAddress = textBox1.Text;
            omronFinsNet.Port      = port;
            omronFinsNet.DA2       = DA2;
            omronFinsNet.ByteTransform.DataFormat = (HslCommunication.Core.DataFormat)comboBox1.SelectedItem;

            OperateResult connect = omronFinsNet.ConnectServer( );

            if (connect.IsSuccess)
            {
                MessageBox.Show(StringResources.Language.ConnectedSuccess);
                button2.Enabled = true;
                button1.Enabled = false;
                panel2.Enabled  = true;

                textBox15.Text = omronFinsNet.SA1.ToString( );
                textBox3.Text  = omronFinsNet.DA1.ToString( );
                userControlReadWriteOp1.SetReadWriteNet(omronFinsNet, "D100", true);
            }
            else
            {
                MessageBox.Show(StringResources.Language.ConnectedFailed + " " + connect.ToMessageShowString( ));
            }
        }
Пример #13
0
        public void ClassTest2( )
        {
            #region Usage2

            // 实例化对象,指定PLC的ip地址和端口号
            OmronFinsNet omronFinsNet = new OmronFinsNet("192.168.1.110", 6000);
            omronFinsNet.SA1 = 10;
            omronFinsNet.DA1 = 110;

            // 连接对象
            OperateResult connect = omronFinsNet.ConnectServer( );
            if (!connect.IsSuccess)
            {
                Console.WriteLine("connect failed:" + connect.Message);
                return;
            }

            // 举例读取D100的值
            short D100 = omronFinsNet.ReadInt16("D100").Content;

            omronFinsNet.ConnectClose( );

            #endregion
        }
Пример #14
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show("Ip地址输入不正确!");
                return;
            }

            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show("端口号输入不正确!");
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte SA1))
            {
                MessageBox.Show("本机网络号输入不正确!");
                return;
            }


            if (!byte.TryParse(textBox16.Text, out byte DA2))
            {
                MessageBox.Show("PLC的单元号输入不正确!");
                return;
            }

            if (!byte.TryParse(textBox17.Text, out byte DA1))
            {
                MessageBox.Show("PLC的节点号输入不正确!");
                return;
            }

            omronFinsNet.IpAddress = textBox1.Text;
            omronFinsNet.Port      = port;
            omronFinsNet.SA1       = SA1;
            omronFinsNet.DA1       = DA1;
            omronFinsNet.DA2       = DA2;
            omronFinsNet.ByteTransform.DataFormat = HslCommunication.Core.DataFormat.ABCD;

            try
            {
                // OperateResult connect = OperateResult.CreateSuccessResult( );
                OperateResult connect = omronFinsNet.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show("连接成功!");
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;
                }
                else
                {
                    MessageBox.Show("连接失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }