/// <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); IPEndPoint endPoint = CreateIPEndPoint(); stopReceived = false; UdpClient.Client.ReceiveTimeout = DatagramReceiveTimeout; while (readCnt < maxCount && !stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT) { // считывание данных byte[] datagram = ReceiveDatagram(ref endPoint, out int readPos, out bool isNew); if (datagram != null && datagram.Length > 0) { // поиск кода остановки в считанных данных int stopCodeInd = -1; for (int i = readPos, len = datagram.Length; i < len && !stopReceived; i++) { if (stopCond.CheckCondition(datagram, i)) { 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 + offset, copyCnt); readCnt += copyCnt; readPos += copyCnt; } // накопление данных во внутреннем буфере соединения if (readCnt < maxCount && !stopReceived && isNew) { Thread.Sleep(DataAccumThreadDelay); } StoreDatagram(datagram, readPos); utcNowDT = DateTime.UtcNow; } logText = BuildReadLogText(buffer, offset, readCnt, logFormat); return(readCnt); } catch (Exception ex) { throw new InvalidOperationException(CommPhrases.ReadDataWithStopCondError + ": " + ex.Message, ex); } }
/// <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); NetStream.ReadTimeout = OneByteReadTimeout; stopReceived = false; int curOffset = offset; while (readCnt < maxCount && !stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT) { // считывание одного байта данных bool readOk; try { readOk = NetStream.Read(buffer, curOffset, 1) > 0; } catch (IOException) { readOk = false; } if (readOk) { stopReceived = stopCond.CheckCondition(buffer, curOffset); curOffset++; readCnt++; } else { // накопление данных во внутреннем буфере соединения Thread.Sleep(DataAccumThreadDelay); } utcNowDT = DateTime.UtcNow; } logText = BuildReadLogText(buffer, offset, readCnt, logFormat); if (readCnt > 0) { UpdateActivityDT(); } return(readCnt); } catch (Exception ex) { throw new InvalidOperationException(CommPhrases.ReadDataWithStopCondError + ": " + ex.Message, ex); } }
/// <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); } }