Example #1
0
        internal void Send(byte[] bytes, ReceiveBytesDelegate receiveBytesDelegate, GuidClass messageGuid,
                           string parentID, string description, Guid plcGuid, byte messageEnumerator)
        {
            MessageRecord message = new MessageRecord()
            {
                MessageGuid           = messageGuid,
                PlcGuid               = plcGuid,
                ParentID              = parentID,
                UnitId                = GetUnitIdFromBinaryMessage(bytes),
                IsSent                = false,
                MessageRequest        = bytes,
                RemainingRetries      = m_retry,
                Description           = description,
                ReceiveBytesDelegate  = new ReceiveBytesDelegate(receiveBytesDelegate),
                ReceiveStringDelegate = null,
                MessageEnumerator     = messageEnumerator
            };

            lock (messageQueue)
            {
                abortRetries = false;
                messageQueue.Enqueue(message);
                messageQueue_Changed();
            }
        }
Example #2
0
        internal void Send(string text, ReceiveStringDelegate receiveStringDelegate, GuidClass messageGuid,
                           string parentID, string description, Guid plcGuid, bool isIdMessage = false,
                           bool suppressEthernetHeader = false)
        {
            MessageRecord message = new MessageRecord()
            {
                MessageGuid            = messageGuid,
                PlcGuid                = plcGuid,
                ParentID               = parentID,
                UnitId                 = GetUnitIdFromStringMessage(text),
                IsSent                 = false,
                MessageRequest         = text,
                RemainingRetries       = m_retry,
                Description            = description,
                IsIdMessage            = isIdMessage,
                SuppressEthernetHeader = suppressEthernetHeader,
                ReceiveBytesDelegate   = null,
                ReceiveStringDelegate  = new ReceiveStringDelegate(receiveStringDelegate)
            };

            lock (messageQueue)
            {
                abortRetries = false;
                messageQueue.Enqueue(message);
                messageQueue_Changed();
            }
        }
Example #3
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();
                }
            }
        }