Example #1
0
        // Connect to PLC
        private void textToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mythread != null)
            {
                if (run)
                {
                    run = false;
                }

                if (mythread.IsAlive)
                {
                    Thread.Sleep(100);
                }
            }

            if (!run)
            {
                mythread   = new Thread(new ThreadStart(ThreadWork));
                pingthread = new Thread(new ThreadStart(PingThread));

                if (_plc.Connect())
                {
                    run = true;
                    textToolStripMenuItem.Enabled   = false;
                    uložitToolStripMenuItem.Enabled = false;
                    PrepareChart();
                    mythread.Start();
                    pingthread.Start();
                }
                else
                {
                    MessageBox.Show("Unable to connect to PLC", "PLC connection error");
                }
            }
        }
Example #2
0
 private void ConnectDisconectAction(object obj)
 {
     if (plc.ConnectionStatus == ConnectionStatusEnum.CONNECTING ||
         plc.ConnectionStatus == ConnectionStatusEnum.CONNECTED ||
         plc.ConnectionStatus == ConnectionStatusEnum.RECONNECTING)
     {
         plc.Disconnect();
         connectCancellationTokenSource.Cancel();
     }
     else
     {
         if (plc.ConnectionStatus != ConnectionStatusEnum.CONNECTING &&
             plc.ConnectionStatus != ConnectionStatusEnum.DISCONNECTING &&
             plc.ConnectionStatus != ConnectionStatusEnum.RECONNECTING)
         {
             connectCancellationTokenSource = new CancellationTokenSource();
             Task.Run(new Action(() =>
             {
                 plc.Connect(ip, port);
             }), connectCancellationTokenSource.Token);
         }
     }
 }
Example #3
0
        private Boolean Test()
        {
            Boolean res    = false;
            Ping    pinger = null;

            try
            {
                if (!string.IsNullOrEmpty(txt_ip.Text))
                {
                    pinger = new Ping();
                    PingReply reply = pinger.Send(txt_ip.Text, 3000);
                    if (reply.Status == IPStatus.Success)
                    {
                        string msg = "PLC is reachable";

                        S7Type type = S7Type.S7300;

                        switch (combo_typ.SelectedValue)
                        {
                        case "s7-300":
                            type = S7Type.S7300;
                            break;

                        case "s7-400":
                            type = S7Type.S7400;
                            break;

                        case "s7-1200":
                            type = S7Type.S71200;
                            break;

                        case "s7-1500":
                            type = S7Type.S71500;
                            break;
                        }

                        Plc plc = null;

                        try
                        {
                            plc = new Plc(txt_ip.Text, type);
                            plc.Connect();
                            if (plc.Connected)
                            {
                                msg += "\r\nPLC connection is ok";
                                res  = true;
                            }
                            else
                            {
                                msg += "\r\nUnable to connect to PLC";
                                MessageBox.Show(msg, "PLC connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                res = false;
                            }
                        }
                        catch (Exception ex0)
                        {
                            msg += "\r\nPLC connection exception\r\n " + ex0.Message;
                            MessageBox.Show(msg, "PLC connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            res = false;
                        }
                        finally
                        {
                            plc.Disconnect();
                            plc = null;
                        }
                    }
                    else
                    {
                        MessageBox.Show("PLC unreachable", "PLC connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        res = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC connection exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                res = false;
            }
            finally
            {
                if (pinger != null)
                {
                    pinger.Dispose();
                }
            }

            return(res);
        }