Example #1
0
        /// <summary>
        /// Считать строки
        /// </summary>
        public override List <string> ReadLines(int timeout, TextStopCondition stopCond,
                                                out bool stopReceived, out string logText)
        {
            try
            {
                List <string> lines = new List <string>();
                stopReceived = false;

                DateTime   utcNowDT = DateTime.UtcNow;
                DateTime   startDT  = utcNowDT;
                DateTime   stopDT   = startDT.AddMilliseconds(timeout);
                IPEndPoint endPoint = CreateIPEndPoint();
                UdpClient.Client.ReceiveTimeout = DatagramReceiveTimeout;

                while (!stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT)
                {
                    // считывание данных
                    byte[] datagram = ReceiveDatagram(ref endPoint, out int readPos, out bool isNew);

                    if (datagram != null && datagram.Length > 0)
                    {
                        // получение строк из считанных данных
                        int           datagramLen = datagram.Length;
                        StringBuilder sbLine      = new StringBuilder(datagramLen);

                        while (readPos < datagramLen && !stopReceived)
                        {
                            sbLine.Append(Encoding.Default.GetChars(datagram, readPos, 1));
                            readPos++;
                            bool newLineFound = StringBuilderEndsWith(sbLine, NewLine);
                            if (newLineFound || readPos == datagramLen)
                            {
                                string line = newLineFound ?
                                              sbLine.ToString(0, sbLine.Length - NewLine.Length) :
                                              sbLine.ToString();
                                lines.Add(line);
                                sbLine.Clear();
                                stopReceived = stopCond.CheckCondition(lines, line);
                            }
                        }
                    }

                    // накопление данных во внутреннем буфере соединения
                    if (!stopReceived && isNew)
                    {
                        Thread.Sleep(DataAccumThreadDelay);
                    }

                    StoreDatagram(datagram, readPos);
                    utcNowDT = DateTime.UtcNow;
                }

                logText = BuildReadLinesLogText(lines);
                return(lines);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadLinesError + ": " + ex.Message, ex);
            }
        }
Example #2
0
        /// <summary>
        /// Считать строки
        /// </summary>
        public override List <string> ReadLines(int timeout, TextStopCondition stopCond,
                                                out bool stopReceived, out string logText)
        {
            try
            {
                List <string> lines = new List <string>();
                stopReceived = false;

                DateTime utcNowDT = DateTime.UtcNow;
                DateTime startDT  = utcNowDT;
                DateTime stopDT   = startDT.AddMilliseconds(timeout);
                SerialPort.ReadTimeout = 0;

                while (!stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT)
                {
                    string line;
                    try { line = SerialPort.ReadLine().Trim(); }
                    catch (TimeoutException) { line = ""; }

                    if (line != "")
                    {
                        lines.Add(line);
                        stopReceived = stopCond.CheckCondition(lines, line);
                    }

                    // накопление входных данных в буфере порта
                    if (!stopReceived)
                    {
                        Thread.Sleep(DataAccumThreadDelay);
                    }

                    utcNowDT = DateTime.UtcNow;
                }

                logText = BuildReadLinesLogText(lines);
                return(lines);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadLinesError + ": " + ex.Message, ex);
            }
        }
Example #3
0
        /// <summary>
        /// Считать строки
        /// </summary>
        public override List <string> ReadLines(int timeout, TextStopCondition stopCond,
                                                out bool stopReceived, out string logText)
        {
            try
            {
                List <string> lines = new List <string>();
                stopReceived = false;

                DateTime nowDT   = DateTime.Now;
                DateTime startDT = nowDT;
                DateTime stopDT  = startDT.AddMilliseconds(timeout);
                NetStream.ReadTimeout = OneByteReadTimeout;

                StringBuilder sbLine = new StringBuilder(maxLineSize);
                byte[]        buffer = new byte[1];

                while (!stopReceived && startDT <= nowDT && nowDT <= stopDT)
                {
                    // считывание одного байта данных
                    bool readOk;
                    try { readOk = NetStream.Read(buffer, 0, 1) > 0; }
                    catch (IOException) { readOk = false; }

                    if (readOk)
                    {
                        sbLine.Append(Encoding.Default.GetChars(buffer));
                    }
                    else
                    {
                        // накопление данных во внутреннем буфере соединения
                        Thread.Sleep(DataAccumThreadDelay);
                    }

                    bool newLineFound = StringBuilderEndsWith(sbLine, NewLine);
                    if (newLineFound || sbLine.Length == maxLineSize)
                    {
                        string line = newLineFound ?
                                      sbLine.ToString(0, sbLine.Length - NewLine.Length) :
                                      sbLine.ToString();
                        lines.Add(line);
                        sbLine.Clear();
                        stopReceived = stopCond.CheckCondition(lines, line);
                    }

                    nowDT = DateTime.Now;
                }

                logText = BuildReadLinesLogText(lines);

                if (lines.Count > 0)
                {
                    UpdateActivityDT();
                }

                return(lines);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadLinesError + ": " + ex.Message, ex);
            }
        }