Example #1
0
        public int ConnectPLC()
        {
            if (IsConnected)
            {
                return(-1);
            }
            //PLC连不上时临时使用
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(IpAddress), TcpPort);

            cliSocket = TimeOutSocket.Connect(iep, 1000);
            if (cliSocket != null)
            {
                mFds.rfd = OpenSocket(102, IpAddress);
                if (mFds.rfd <= 0)
                {
                    return(mFds.rfd);               //FD一般是不可能为0的,0、1、2、3、4等估计早被操作系统或者其他程序占用了
                }
                mFds.wfd    = mFds.rfd;
                mDI         = new DaveInterface(mFds, "Test", 0, Protocols.ISOTCP, ProfiBusSpeed.Speed187k);
                mDI.Timeout = 1;
                mConn       = new DaveConnection(mDI, 0, PlcRack, PlcSlot);
                int rc = mConn.ConnectPLC();
                if (rc == 0)
                {
                    IsConnected = true;
                    return(rc);
                }
                return(rc);
            }
            return(-1);
        }
Example #2
0
        /// <summary>
        /// 连接PLC
        /// </summary>
        /// <returns></returns>
        public int ConnectPLC()
        {
            if (IsConnected)
            {
                return(0);
            }

            IPEndPoint    ipe     = new IPEndPoint(IPAddress.Parse(IpAddress), TcpPort);
            TimeOutSocket timeOut = new TimeOutSocket();

            cliSocket = timeOut.Connect(ipe, 300);
            if (cliSocket != null)
            {
                mFds.rfd = API.OpenSocket(TcpPort, IpAddress);
                if (mFds.rfd <= 0)
                {
                    //FD一般是不可能为0的,0、1、2、3、4等估计早被操作系统或者其他程序占用了
                    return(mFds.rfd);
                }

                mFds.wfd    = mFds.rfd;
                mDI         = new DaveInterface(mFds, "PLC_1", 0, PLCEnum.Protocols.ISOTCP, PLCEnum.ProfiBusSpeed.Speed187k);
                mDI.Timeout = 1;
                mConn       = new DaveConnection(mDI, 0, PlcRack, PlcSlot);
                int rc = mConn.ConnectPL();
                if (rc == 0)
                {
                    IsConnected = true;
                    return(rc);
                }
                return(rc);
            }
            return(-1);
        }
Example #3
0
        public void DisConnectPLC()
        {
            if (!IsConnected)
            {
                return;
            }

            if (mConn != null)
            {
                mConn.DisconnectPLC();
                mConn.Dispose();
            }

            if (mDI != null)
            {
                mDI.Dispose();
            }

            API.CloseSocket(mFds.rfd);
            mConn       = null;
            mDI         = null;
            IsConnected = false;
        }