/// <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) { try { int readCnt = 0; IPEndPoint endPoint = CreateRemoteEndPoint(); stopReceived = false; UdpClient.Client.ReceiveTimeout = DatagramReceiveTimeout; Stopwatch stopwatch = Stopwatch.StartNew(); while (readCnt < maxCount && !stopReceived && stopwatch.ElapsedMilliseconds <= timeout) { // read data byte[] datagram = ReceiveDatagram(ref endPoint, out int readPos, out bool isNew); if (datagram != null && datagram.Length > 0) { // search for stop code int stopCodeInd = -1; for (int i = readPos, len = datagram.Length; i < len && !stopReceived; i++) { if (stopCond.CheckCondition(datagram, i)) { stopCodeInd = i; stopReceived = true; } } // copy received data to the buffer int requiredCnt = stopReceived ? stopCodeInd - readCnt + 1 : maxCount - readCnt; int copyCnt = Math.Min(datagram.Length - readPos, requiredCnt); Buffer.BlockCopy(datagram, readPos, buffer, readCnt + offset, copyCnt); readCnt += copyCnt; readPos += copyCnt; } // accumulate data in the internal connection buffer if (readCnt < maxCount && !stopReceived && isNew) { Thread.Sleep(DataAccumDelay); } StoreDatagram(datagram, readPos); } logText = BuildReadLogText(buffer, offset, readCnt, format); return(readCnt); } catch (Exception ex) { throw new ScadaException(CommPhrases.ReadDataStopCondError + ": " + ex.Message, ex); } }
/// <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) { try { int readCnt = 0; int curOffset = offset; stopReceived = false; NetStream.ReadTimeout = OneByteReadTimeout; Stopwatch stopwatch = Stopwatch.StartNew(); while (readCnt < maxCount && !stopReceived && stopwatch.ElapsedMilliseconds <= timeout) { // read one byte bool readOk; try { readOk = NetStream.DataAvailable && NetStream.Read(buffer, curOffset, 1) > 0; } catch (IOException) { readOk = false; } if (readOk) { stopReceived = stopCond.CheckCondition(buffer, curOffset); curOffset++; readCnt++; } else { Thread.Sleep(DataAccumDelay); } } logText = BuildReadLogText(buffer, offset, readCnt, format); if (readCnt > 0) { UpdateActivityTime(); } return(readCnt); } catch (Exception ex) { throw new ScadaException(CommPhrases.ReadDataStopCondError + ": " + ex.Message, ex); } }
/// <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) { try { int readCnt = 0; int curInd = offset; stopReceived = false; SerialPort.ReadTimeout = 0; Stopwatch stopwatch = Stopwatch.StartNew(); while (readCnt < maxCount && !stopReceived && stopwatch.ElapsedMilliseconds <= timeout) { 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(DataAccumDelay); } } logText = BuildReadLogText(buffer, offset, readCnt, format); return(readCnt); } catch (Exception ex) { throw new ScadaException(CommPhrases.ReadDataStopCondError + ": " + ex.Message, ex); } }