Exemple #1
0
        public bool Connect()
        {
            try
            {
                byte[] bytStatus = new byte[1];
                int    num       = MXIO_CS.MXEIO_Init();
                switch (MXIO_CS.MXEIO_E1K_Connect(Encoding.UTF8.GetBytes(this.ipAddress), this.port, this.timeOut, this.hConnection, Encoding.UTF8.GetBytes(this.password)))
                {
                case 0:
                    switch (MXIO_CS.MXEIO_CheckConnection(this.hConnection[0], this.timeOut, bytStatus))
                    {
                    case 0:
                        return(true);

                    case 0x7d1:
                    case 0xfa2:
                        MXIO_CS.MXEIO_Exit();
                        break;
                    }
                    return(false);

                case 0x7d1:
                case 0xfa2:
                    MXIO_CS.MXEIO_Exit();
                    break;
                }
                return(false);
            }
            catch (Exception exception)
            {
                CLog.WriteErrLogInTrace(string.Format("在连接IOLogik时出错,{0}", exception.Message));
                return(false);
            }
        }
Exemple #2
0
        public override void Open()
        {
            int ret;

            Manager.Logger.Log(LogLevel.INFO, "IP address: {0}", IP);
            Manager.Logger.Log(LogLevel.INFO, "Port: {0}", Port);

            // Ping.
            Ping      ping = new Ping();
            PingReply rep;

            rep = ping.Send(IP, (int)Manager.Timeout);
            if (rep.Status != IPStatus.Success)
            {
                throw new MXIOException("Cannot reach the IO module.");
            }

            // Connect.
            int[] conn = new int[1];
            ret = MXIO_CS.MXEIO_E1K_Connect(Encoding.UTF8.GetBytes(IP), Port, Manager.Timeout, conn, new byte[0]);
            if (ret != MXIO_CS.MXIO_OK)
            {
                throw new MXIOException("MXEIO_E1K_Connect failed: {0}.", ret);
            }
            this.connection = conn[0];

            // Check connection.
            byte[] bytCheckStatus = new byte[1];
            ret = MXIO_CS.MXEIO_CheckConnection(connection, Manager.Timeout, bytCheckStatus);
            if (ret != MXIO_CS.MXIO_OK)
            {
                throw new MXIOException("MXEIO_CheckConnection failed: {0}.", ret);
            }
            if (bytCheckStatus[0] != MXIO_CS.CHECK_CONNECTION_OK)
            {
                string msg;
                switch (bytCheckStatus[0])
                {
                case MXIO_CS.CHECK_CONNECTION_FAIL:
                    msg = "fail";
                    break;

                case MXIO_CS.CHECK_CONNECTION_TIME_OUT:
                    msg = "timeout";
                    break;

                default:
                    msg = "unknown: " + bytCheckStatus[0];
                    break;
                }
                throw new MXIOException("MXEIO_CheckConnection status failed: {0}.", msg);
            }
        }
Exemple #3
0
        private void CheckConnection(bool attemptReconnect)
        {
            // Check Connection
            Connected = false;

            byte[]     checkStatus = new byte[1];
            MoxaStatus status      = (MoxaStatus)MXIO_CS.MXEIO_CheckConnection(_hConn[0], _timeout, checkStatus);

            if (status != MoxaStatus.Ok)
            {
                throw new IOException(this, String.Format("Failed to check connection to Moxa ioLogic1200 device '{0}'.", Name));
            }

            if (checkStatus[0] == MXIO_CS.CHECK_CONNECTION_OK)
            {
                Connected = true;
                return;
            }

            if (attemptReconnect)
            {
                status = (MoxaStatus)MXIO_CS.MXEIO_E1K_Connect(_ipAddress, _port, _timeout, _hConn, _password);
                if (status == MoxaStatus.Ok)
                {
                    if (Logging)
                    {
                        Log.Trace(IOComponent.Instance.Name).Warn(this.ToString(), "Connection to Moxa ioLogic1200 device restored.");
                    }

                    return;
                }
            }

            switch (checkStatus[0])
            {
            case MXIO_CS.CHECK_CONNECTION_FAIL:
                throw new IOException(this, String.Format("Connection failed to Moxa ioLogic1200 device '{0}'.", Name));

            case MXIO_CS.CHECK_CONNECTION_TIME_OUT:
                throw new IOException(this, String.Format("Connection timed-out to Moxa ioLogic1200 device '{0}'.", Name));

            default:
                throw new IOException(this, String.Format("Unknown connection failure ({1}) to Moxa ioLogic1200 device '{0}'.", Name, checkStatus[0]));
            }
        }
Exemple #4
0
        private int CheckConnection(bool ShowDialog)
        {
            //--------------------------------------------------------------------------
            //Check Connection
            byte[] bytCheckStatus = new byte[1];
            ret = MXIO_CS.MXEIO_CheckConnection(hConnection[0], ioLogicTimeout, bytCheckStatus);
            MXEIO_Error.CheckErr(ret, "MXEIO_CheckConnection");
            if (ret == MXIO_CS.MXIO_OK)
            {
                switch (bytCheckStatus[0])
                {
                case MXIO_CS.CHECK_CONNECTION_OK:
                    if (ShowDialog)
                    {
                        tbCommunication.AppendText(string.Format("MXEIO_CheckConnection: Check connection ok => {0}{1}", bytCheckStatus[0], "\r\n"));
                    }
                    break;

                case MXIO_CS.CHECK_CONNECTION_FAIL:
                    if (ShowDialog)
                    {
                        tbCommunication.AppendText(string.Format("MXEIO_CheckConnection: Check connection fail => {0}{1}", bytCheckStatus[0], "\r\n"));
                    }
                    break;

                case MXIO_CS.CHECK_CONNECTION_TIME_OUT:
                    if (ShowDialog)
                    {
                        tbCommunication.AppendText(string.Format("MXEIO_CheckConnection: Check connection time out => {0}{1}", bytCheckStatus[0], "\r\n"));
                    }
                    break;

                default:
                    if (ShowDialog)
                    {
                        tbCommunication.AppendText(string.Format("MXEIO_CheckConnection: Check connection status unknown => {0}{1}", bytCheckStatus[0], "\r\n"));
                    }
                    break;
                }
            }
            return(ret);
            //--------------------------------------------------------------------------
        }