Exemplo n.º 1
0
 public bool TcpDisConnect(out string msg)
 {
     try
     {
         if (this.Company == PlcCompany.Mitsubishi.ToString())
         {
             if (melsec_net != null)
             {
                 melsec_net.ConnectClose();// 关闭长连接,并切换为短连接,在系统退出时可以调用
             }
             IsAlive = false;
         }
         else if (this.Company == PlcCompany.Siemens.ToString())
         {
             if (siemensS7Net != null)
             {
                 siemensS7Net.ConnectClose();
                 siemensS7Net = null;
             }
             IsAlive = false;
         }
         else if (this.Company == PlcCompany.OMRON.ToString() && (this.Model == "SYSMAC CP1H" || this.Model == "NX102"))
         {
             if (omron_net != null)
             {
                 omron_net.ConnectClose();
                 omron_net = null;
             }
             IsAlive = false;
         }
         else if (this.Company == PlcCompany.OMRON.ToString() && this.Model == "NX1P2")
         {
             if (Socket != null)
             {
                 Socket.Close();
                 Socket.Dispose();
                 Socket = null;
             }
             if (point != null)
             {
                 point = null;
             }
             IsAlive = false;
         }
         else if (this.Company == "MCGS" && this.Model == "TPC1061TI(Hi)")
         {
             if (busTcpClient != null)
             {
                 busTcpClient.ConnectClose();
                 busTcpClient = null;
             }
             IsAlive = false;
         }
         else if (Socket != null)
         {
             Socket.Close();
             Socket.Dispose();
             Socket  = null;
             IsAlive = false;
         }
     }
     catch (Exception ex)
     {
         msg     = ex.Message;
         IsAlive = true;
         return(false);
     }
     msg = string.Empty;
     return(true);
 }
Exemplo n.º 2
0
        public bool TcpConnect(out string msg)
        {
            msg = string.Empty;
            try
            {
                if (this.Company == PlcCompany.Mitsubishi.ToString())
                {
                    //初始化
                    melsec_net.PLCIpAddress         = System.Net.IPAddress.Parse(this.IP); // PLC的IP地址
                    melsec_net.PortRead             = Port;                                // 端口
                    melsec_net.PortWrite            = 6001;                                // 写入端口,最好和读取分开
                    melsec_net.NetworkNumber        = 0;                                   // 网络号
                    melsec_net.NetworkStationNumber = 0;                                   // 网络站号
                    melsec_net.ConnectTimeout       = 500;                                 // 连接超时时间

                    // 如果需要长连接,就取消下面这行代码的注释,对于数据读写的代码,没有影响
                    melsec_net.ConnectServer(); // 切换长连接,这行代码可以放在其他任何地方
                    IsAlive = true;
                }
                else if (this.Company == PlcCompany.Siemens.ToString())
                {
                    siemensS7Net           = new HslCommunice533.Profinet.Siemens.SiemensS7Net(HslCommunice533.Profinet.Siemens.SiemensPLCS.S1200);
                    siemensS7Net.IpAddress = this.IP;    // PLC的IP地址
                    //siemens_net.PortRead = Port;                                           // 端口
                    //siemens_net.PortWrite = 102;                                          // 写入端口,最好和读取分开
                    //siemens_net.ConnectTimeout = 500;                                      // 连接超时时间
                    siemensS7Net.ConnectServer();
                    IsAlive = true;
                }
                else if (this.Company == PlcCompany.OMRON.ToString() && (this.Model == "SYSMAC CP1H" || this.Model == "NX102"))
                {
                    if (omron_net == null)
                    {
                        omron_net = new HslCommunice533.Profinet.Omron.OmronFinsNet(this.IP, this.Port);
                        var ips = this.IP.Split('.');
                        omron_net.SA1 = TengDa.Net.GetIpLastValue(Net.GetLocalIpByRegex(string.Format("{0}.{1}.{2}.*", ips[0], ips[1], ips[2]))); // PC网络号,PC的IP地址的最后一个数
                        omron_net.DA1 = TengDa.Net.GetIpLastValue(this.IP);                                                                       // PLC网络号,PLC的IP地址的最后一个数0
                        omron_net.DA2 = 0x00;                                                                                                     // PLC单元号,通常为0

                        omron_net.ReceiveTimeOut = 1000;
                        omron_net.ConnectTimeOut = 1000;
                        HslCommunice533.OperateResult result = omron_net.ConnectServer();
                        if (!result.IsSuccess)
                        {
                            msg     = result.Message;
                            IsAlive = false;
                            return(false);
                        }
                    }
                    IsAlive = true;
                }
                else if (this.Company == "MCGS" && this.Model == "TPC1061TI(Hi)")
                {
                    if (busTcpClient == null)
                    {
                        busTcpClient?.ConnectClose();
                        busTcpClient = new HslCommunice533.ModBus.ModbusTcpNet(this.IP, this.Port, 1);
                        busTcpClient.AddressStartWithZero = true;
                        busTcpClient.IsStringReverse      = false;

                        HslCommunice533.OperateResult result = busTcpClient.ConnectServer();
                        if (!result.IsSuccess)
                        {
                            msg     = result.Message;
                            IsAlive = false;
                            return(false);
                        }
                    }
                    IsAlive = true;
                }
                else if (this.Company == PlcCompany.OMRON.ToString() && this.Model == "NX1P2")
                {
                    point   = new IPEndPoint(IPAddress.Parse(this.IP), this.Port);
                    IsAlive = true;
                }
                else if (!Socket.Connected)
                {
                    Socket.Connect(IpAddress, Port);
                    IsAlive = true;
                }
            }
            catch (Exception ex)
            {
                msg     = ex.Message;
                IsAlive = false;
                return(false);
            }
            return(true);
        }