コード例 #1
0
        public static Serial GetChannel(SerialPortNames portName)
        {
            try
            {
                foreach (Channel c in m_channels)
                {
                    if (c.GetType() == typeof(Serial))
                    {
                        Serial serial = c as Serial;
                        if (serial.PortName == portName)
                        {
                            return(serial);
                        }
                    }
                }

                return(null);
            }
            catch (ComDriveExceptions ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                throw;
            }
        }
コード例 #2
0
        internal void Dispose()
        {
            // This function literally kills the socket, no matter if the message queue is not empty.
            try
            {
                if (!Disposed)
                {
                    ListenerClientsInfo.DecrementCount(localPort);
                    Disposed = true;
                    worker.Close();
                    if (OnConnectionClosed != null)
                    {
                        OnConnectionClosed(this);
                    }

                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                    }
                    catch
                    {
                    }

                    AbortAll();
                }
            }
            catch (Exception ex)
            {
                string exceptionText = "InnerListenerClientEcxeption (Dispose)" + " - " + ex.Message;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
            }
        }
コード例 #3
0
 public static void ActivationService(Object activationService)
 {
     try
     {
         if (activationService is String)
         {
             string activationString = activationService as String;
             System.Security.Cryptography.MD5CryptoServiceProvider MD5 =
                 new System.Security.Cryptography.MD5CryptoServiceProvider();
             byte[] encodedBytes = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(activationString));
             activationString = Utils.HexEncoding.GetHexTwoCharsPerByte(encodedBytes);
             if (activationString == "7C765F7F05BC73663FFA9ED33B9998D5")
             {
                 m_ActivationService = true;
             }
             else
             {
                 m_ActivationService = false;
             }
         }
     }
     catch (ComDriveExceptions ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
         ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
         throw;
     }
 }
コード例 #4
0
        public static Ethernet GetChannel(string remoteIp, Int32 remotePort)
        {
            try
            {
                foreach (Channel c in m_channels)
                {
                    if (c.GetType() == typeof(Ethernet))
                    {
                        Ethernet ethernet = c as Ethernet;
                        if ((ethernet.RemoteIP == remoteIp) && (ethernet.RemotePort == remotePort))
                        {
                            return(ethernet);
                        }
                    }
                }

                return(null);
            }
            catch (ComDriveExceptions ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                throw;
            }
        }
コード例 #5
0
        public static EthernetListener GetChannel(int port)
        {
            try
            {
                foreach (Channel c in m_channels)
                {
                    if (c.GetType() == typeof(EthernetListener))
                    {
                        EthernetListener listener = c as EthernetListener;
                        if (listener.LocalPort == port)
                        {
                            return(listener);
                        }
                    }
                }

                return(null);
            }
            catch (ComDriveExceptions ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                throw;
            }
        }
コード例 #6
0
        public static void GetChannel(int port, out ListenerServer listenerServer)
        {
            try
            {
                foreach (Channel c in m_channels)
                {
                    if (c.GetType() == typeof(ListenerServer))
                    {
                        ListenerServer listener = c as ListenerServer;
                        if (listener.LocalPort == port)
                        {
                            listenerServer = listener;
                            return;
                        }
                    }
                }

                listenerServer = null;
            }
            catch (ComDriveExceptions ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                throw;
            }
        }
コード例 #7
0
        public static void GetPLC(ListenerServer litenerServer)
        {
            try
            {
                bool           isConnected = false;
                Channel        plcChannel  = getChannel(litenerServer, ref isConnected);
                ListenerServer listener    = plcChannel as ListenerServer;
                if (listener == null)
                {
                    throw new ComDriveExceptions("Listener Server could not be intialized due to unexpected error",
                                                 ComDriveExceptions.ComDriveException.UnexpectedError);
                }

                listener.Listen();
            }
            catch (ComDriveExceptions ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                throw;
            }
        }
コード例 #8
0
        private void initSocket(Socket socket, int port)
        {
            try
            {
                worker    = socket;
                localPort = port;
                IPEndPoint ep = worker.RemoteEndPoint as IPEndPoint;
                RemoteIP = ep.Address.ToString();

                string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                try
                {
                    ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                       Unitronics.ComDriver.ConnectionStatus.ConnectionOpened.ToString() + ", Remote IP (Client): " +
                                                       RemoteIP + ", Client GUID: " + guid.ToString());
                }
                catch
                {
                }

                ListenerClientsInfo.IncrementCount(localPort);
                waitForData();
            }
            catch (Exception ex)
            {
                if (!Disposed)
                {
                    ListenerClientsInfo.DecrementCount(localPort);
                }
                try
                {
                    worker.Close();
                }
                catch
                {
                }

                if (!Disposed)
                {
                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                    }
                    catch
                    {
                    }
                }

                Disposed = true;

                string exceptionText = "InnerListenerClientEcxeption (initSocket)" + " - " + ex.Message;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);

                AbortAll();
            }
        }
コード例 #9
0
        public ComDriveExceptions(string pExceptionMsg, ComDriveException pErrCode)
            : base(pExceptionMsg)
        {
            _errCode = pErrCode;

            string exceptionText = pErrCode.ToString() + " - " + pExceptionMsg;

            ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
        }
コード例 #10
0
 public static void DisableLogger()
 {
     try
     {
         ComDriverLogger.Disable();
     }
     catch (ComDriveExceptions ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
         ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
         throw;
     }
 }
コード例 #11
0
        internal static PLC getPLC(ref Channel channel, int unitId, ref bool isConnected, bool suppressEthernetHeader)
        {
            try
            {
                isConnected = false;
                if (unitId < 0 || unitId > 127)
                {
                    throw new ComDriveExceptions("UnitID out of range! The value must be between 0 and 127.",
                                                 ComDriveExceptions.ComDriveException.InvalidUnitID);
                }

                Channel plcChannel = getChannel(channel, ref isConnected);
                channel = plcChannel;

                try
                {
                    return(new PLC(unitId, plcChannel, suppressEthernetHeader));
                }
                catch (ComDriveExceptions ex)
                {
                    if ((!isConnected) && (channel.GetType() != typeof(EthernetListener)))
                    {
                        plcChannel.Disconnect();
                    }

                    throw;
                }
                catch (Exception ex)
                {
                    string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                    ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                    throw;
                }
            }
            catch (ComDriveExceptions ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                throw;
            }
        }
コード例 #12
0
        private void SendQueueIterator(Object objectState)
        {
            while (queueHasItems())
            {
                MessageRecord messsage = null;
                lock (messageQueue)
                {
                    if (messageQueue.Any())
                    {
                        abortRetries = false;
                        messsage     = messageQueue.Dequeue();
                    }
                }

                if (messsage != null && !messsage.IsSent && messsage.MessageResponse == null)
                {
                    int  retry            = m_retry;
                    bool plcReplyReceived = false;
                    if (messsage.ReceiveStringDelegate != null)
                    {
                        while (!plcReplyReceived && retry > 0)
                        {
                            try
                            {
                                SendString(messsage.MessageRequest as string, messsage.MessageEnumerator,
                                           messsage.IsIdMessage, messsage.SuppressEthernetHeader);
                                //Log the requests
                                if (ComDriverLogger.Enabled)
                                {
                                    string retryLog =
                                        Utils.HelperComDriverLogger.GetLoggerCurrentRetry(m_retry - retry + 1, m_retry);

                                    ComDriverLogger.LogFullMessage(DateTime.Now, GetLoggerChannelText(),
                                                                   messsage.MessageGuid.ToString(), MessageDirection.Sent,
                                                                   retryLog, messsage.MessageRequest as string, messsage.ParentID,
                                                                   messsage.Description);
                                }

                                try
                                {
                                    messsage.MessageResponse = ReceiveString();
                                    plcReplyReceived         = true;

                                    //Log the requests
                                    if (ComDriverLogger.Enabled)
                                    {
                                        string retryLog =
                                            Utils.HelperComDriverLogger.GetLoggerCurrentRetry(m_retry - retry + 1,
                                                                                              m_retry);

                                        ComDriverLogger.LogFullMessage(DateTime.Now, GetLoggerChannelText(),
                                                                       messsage.MessageGuid.ToString(), MessageDirection.Received,
                                                                       retryLog, messsage.MessageResponse, messsage.ParentID,
                                                                       messsage.Description);
                                    }
                                }
                                catch (TimeoutException ex)
                                {
                                    if (abortRetries)
                                    {
                                        retry = 1;
                                    }

                                    if (OnRetry != null)
                                    {
                                        OnRetry((m_retry - retry) + 1, ex);
                                    }
                                    retry--;
                                    if (this is Serial)
                                    {
                                        try
                                        {
                                            bool channelInitialized = AlreadyInitialized;
                                            Disconnect();
                                            Connect();
                                            AlreadyInitialized = channelInitialized;
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                                catch (ComDriveExceptions ex)
                                {
                                    if (abortRetries)
                                    {
                                        retry = 1;
                                    }

                                    if (OnRetry != null)
                                    {
                                        OnRetry((m_retry - retry) + 1, ex);
                                    }
                                    retry--;
                                    if (this is Serial)
                                    {
                                        try
                                        {
                                            bool channelInitialized = AlreadyInitialized;
                                            Disconnect();
                                            Connect();
                                            AlreadyInitialized = channelInitialized;
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (abortRetries)
                                {
                                    retry = 1;
                                }

                                string exceptionText =
                                    ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                                if (OnRetry != null)
                                {
                                    OnRetry((m_retry - retry) + 1, ex);
                                }
                                retry--;
                                if (this is Serial)
                                {
                                    try
                                    {
                                        bool channelInitialized = AlreadyInitialized;
                                        Disconnect();
                                        Connect();
                                        AlreadyInitialized = channelInitialized;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            if (PLCFactory.MessageDelay != 0)
                            {
                                System.Threading.Thread.Sleep(PLCFactory.MessageDelay);
                            }
                        }

                        if (retry <= 0)
                        {
                            messsage.ReceiveStringDelegate(null, CommunicationException.Timeout, messsage.MessageGuid);
                        }
                        else
                        {
                            messsage.ReceiveStringDelegate(messsage.MessageResponse as string,
                                                           CommunicationException.None, messsage.MessageGuid);
                        }
                    }
                    else
                    {
                        while (!plcReplyReceived && retry > 0)
                        {
                            try
                            {
                                SendBytes(messsage.MessageRequest as byte[], messsage.MessageEnumerator);
                                string retryLog =
                                    Utils.HelperComDriverLogger.GetLoggerCurrentRetry(m_retry - retry + 1, m_retry);

                                //Log the requests
                                if (ComDriverLogger.Enabled)
                                {
                                    ComDriverLogger.LogFullMessage(DateTime.Now, GetLoggerChannelText(),
                                                                   messsage.MessageGuid.ToString(), MessageDirection.Sent,
                                                                   retryLog, messsage.MessageRequest as byte[], messsage.ParentID,
                                                                   messsage.Description);
                                }

                                try
                                {
                                    messsage.MessageResponse = ReceiveBytes();
                                    plcReplyReceived         = true;

                                    //Log the requests
                                    if (ComDriverLogger.Enabled)
                                    {
                                        ComDriverLogger.LogFullMessage(DateTime.Now, GetLoggerChannelText(),
                                                                       messsage.MessageGuid.ToString(), MessageDirection.Received,
                                                                       retryLog, messsage.MessageResponse, messsage.ParentID,
                                                                       messsage.Description);
                                    }
                                }
                                catch (TimeoutException ex)
                                {
                                    if (abortRetries)
                                    {
                                        retry = 1;
                                    }

                                    if (OnRetry != null)
                                    {
                                        OnRetry((m_retry - retry) + 1, ex);
                                    }
                                    retry--;
                                    if (this is Serial)
                                    {
                                        try
                                        {
                                            bool channelInitialized = AlreadyInitialized;
                                            Disconnect();
                                            Connect();
                                            AlreadyInitialized = channelInitialized;
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                                catch (ComDriveExceptions ex)
                                {
                                    if (abortRetries)
                                    {
                                        retry = 1;
                                    }

                                    if (OnRetry != null)
                                    {
                                        OnRetry((m_retry - retry) + 1, ex);
                                    }
                                    retry--;
                                    if (this is Serial)
                                    {
                                        try
                                        {
                                            bool channelInitialized = AlreadyInitialized;
                                            Disconnect();
                                            Connect();
                                            AlreadyInitialized = channelInitialized;
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (abortRetries)
                                {
                                    retry = 1;
                                }

                                string exceptionText =
                                    ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
                                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
                                if (OnRetry != null)
                                {
                                    OnRetry((m_retry - retry) + 1, ex);
                                }
                                retry--;
                                if (this is Serial)
                                {
                                    try
                                    {
                                        bool channelInitialized = AlreadyInitialized;
                                        Disconnect();
                                        Connect();
                                        AlreadyInitialized = channelInitialized;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            if (PLCFactory.MessageDelay != 0)
                            {
                                System.Threading.Thread.Sleep(PLCFactory.MessageDelay);
                            }
                        }

                        if (retry <= 0)
                        {
                            messsage.ReceiveBytesDelegate(null, CommunicationException.Timeout, messsage.MessageGuid);
                        }
                        else
                        {
                            messsage.ReceiveBytesDelegate(messsage.MessageResponse as byte[],
                                                          CommunicationException.None, messsage.MessageGuid);
                        }
                    }

                    messsage.IsSent = true;
                }
            }

            lock (messageQueue)
            {
                m_threadIsRunning = false;
                if (messageQueue.Any())
                {
                    messageQueue_Changed();
                }
            }
        }