예제 #1
0
        private readonly TextStopCondition textStopCond; // the stop condition for reading text


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DevTesterLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
            : base(commContext, lineContext, deviceConfig)
        {
            options      = new TesterOptions(deviceConfig.PollingOptions.CustomOptions);
            inBuf        = new byte[options.BufferLength];
            binStopCond  = options.BinStopCode > 0 ? new BinStopCondition(options.BinStopCode) : null;
            textStopCond = string.IsNullOrEmpty(options.StopEnding) ?
                           TextStopCondition.OneLine : new TextStopCondition(options.StopEnding);

            CanSendCommands = true;
        }
예제 #2
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;
 }
예제 #3
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)
        {
            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);
            }
        }
예제 #4
0
 /// <summary>
 /// Считать данные с условиями остановки чтения
 /// </summary>
 public abstract int Read(byte[] buffer, int offset, int maxCount, int timeout, BinStopCondition stopCond,
     out bool stopReceived, CommUtils.ProtocolLogFormats logFormat, out string logText);
예제 #5
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)
        {
            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);
            }
        }
예제 #6
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);
            }
        }
예제 #7
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)
        {
            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);
            }
        }
예제 #8
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);
            }
        }