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(); } } }