Ejemplo n.º 1
0
        /// <summary>
        /// Считать данные с условием остановки чтения
        /// </summary>
        public override int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                                 out bool stopReceived, CommUtils.ProtocolLogFormats logFormat, out string logText)
        {
            try
            {
                int      readCnt = 0;
                DateTime nowDT   = DateTime.Now;
                DateTime startDT = nowDT;
                DateTime stopDT  = startDT.AddMilliseconds(timeout);
                NetStream.ReadTimeout = OneByteReadTimeout;

                stopReceived = false;
                int  curOffset = offset;
                byte stopCode  = stopCond.StopCode;

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

                    if (readOk)
                    {
                        stopReceived = buffer[curOffset] == stopCode;
                        curOffset++;
                        readCnt++;
                    }
                    else
                    {
                        // накопление данных во внутреннем буфере соединения
                        Thread.Sleep(DataAccumThreadDelay);
                    }

                    nowDT = DateTime.Now;
                }

                logText = BuildReadLogText(buffer, offset, readCnt, logFormat);

                if (readCnt > 0)
                {
                    UpdateActivityDT();
                }

                return(readCnt);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadDataWithStopCondError + ": " + ex.Message, ex);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Reads data with the stop condition.
 /// </summary>
 public override int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                          out bool stopReceived, ProtocolFormat format, out string logText)
 {
     stopReceived = false;
     logText      = "";
     return(0);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Считать данные с условиями остановки чтения
        /// </summary>
        public override int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                                 out bool stopReceived, CommUtils.ProtocolLogFormats logFormat, out string logText)
        {
            try
            {
                int        readCnt  = 0;
                DateTime   nowDT    = DateTime.Now;
                DateTime   startDT  = nowDT;
                DateTime   stopDT   = startDT.AddMilliseconds(timeout);
                IPEndPoint endPoint = CreateIPEndPoint();

                stopReceived = false;
                byte stopCode = stopCond.StopCode;
                UdpClient.Client.ReceiveTimeout = DatagramReceiveTimeout;

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

                    if (datagram != null && datagram.Length > 0)
                    {
                        // поиск кода остановки в считанных данных
                        int datagramLen = datagram.Length;
                        int stopCodeInd = -1;
                        for (int i = readPos; i < datagramLen && !stopReceived; i++)
                        {
                            if (datagram[i] == stopCode)
                            {
                                stopCodeInd  = i;
                                stopReceived = true;
                            }
                        }

                        // копирование полученных данных в заданный буфер
                        int requiredCnt = stopReceived ? stopCodeInd - readCnt + 1 : maxCount - readCnt;
                        int copyCnt     = Math.Min(datagram.Length - readPos, requiredCnt);
                        Array.Copy(datagram, readPos, buffer, readCnt, copyCnt);
                        readCnt += copyCnt;
                        readPos += copyCnt;
                    }

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

                    StoreDatagram(datagram, readPos);
                    nowDT = DateTime.Now;
                }

                logText = BuildReadLogText(buffer, offset, readCnt, logFormat);
                return(readCnt);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadDataWithStopCondError + ": " + ex.Message, ex);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Считать данные с условиями остановки чтения
        /// </summary>
        public override int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                                 out bool stopReceived, CommUtils.ProtocolLogFormats logFormat, out string logText)
        {
            try
            {
                int      readCnt  = 0;
                DateTime utcNowDT = DateTime.UtcNow;
                DateTime startDT  = utcNowDT;
                DateTime stopDT   = startDT.AddMilliseconds(timeout);

                stopReceived = false;
                int curInd = offset;
                SerialPort.ReadTimeout = 0;

                while (readCnt < maxCount && !stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT)
                {
                    bool readOk;
                    try { readOk = SerialPort.Read(buffer, curInd, 1) > 0; }
                    catch (TimeoutException) { readOk = false; }

                    if (readOk)
                    {
                        stopReceived = stopCond.CheckCondition(buffer, curInd);
                        curInd++;
                        readCnt++;
                    }
                    else
                    {
                        // накопление входных данных в буфере порта
                        Thread.Sleep(DataAccumThreadDelay);
                    }

                    utcNowDT = DateTime.UtcNow;
                }

                logText = BuildReadLogText(buffer, offset, readCnt, logFormat);
                return(readCnt);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadDataWithStopCondError + ": " + ex.Message, ex);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads and logs data with the stop condition.
        /// </summary>
        public virtual int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                                out bool stopReceived)
        {
            int readCnt = Read(buffer, offset, maxCount, timeout, stopCond,
                               out stopReceived, ProtocolFormat, out string logText);

            Log?.WriteLine(logText);
            return(readCnt);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Reads data with the stop condition.
 /// </summary>
 public abstract int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                          out bool stopReceived, ProtocolFormat format, out string logText);
Ejemplo n.º 7
0
        /// <summary>
        /// Считать данные с условиями остановки чтения и вывести информацию в журнал
        /// </summary>
        public virtual int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
                                out bool stopReceived)
        {
            string logText;
            int    readCnt = Read(buffer, offset, maxCount, timeout, stopCond,
                                  out stopReceived, DefaultLogFormat, out logText);

            WriteToLog(logText);
            return(readCnt);
        }