public void EasyClose()
 {
     if (mythread != null)
     {
         run = false;
         if (mythread.IsAlive)
         {
             Thread.Sleep(100);
             if (mythread.IsAlive)
             {
                 mythread.Abort();
                 if (_plc != null)
                 {
                     if (_plc.Connected)
                     {
                         _plc.Disconnect();
                     }
                 }
             }
         }
     }
 }
Beispiel #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);
         }
     }
 }
Beispiel #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);
        }