Пример #1
0
        private byte[] ReadResponse()
        {
            byte[] response   = new byte[m_bufSize];
            int    header_len = 0;
            int    data_len   = 0;
            int    h_l        = 0;
            int    d_l        = 0;
            int    retry      = 0;

            while (header_len < 3 && retry < config.RetryCount)
            {
                h_l = m_serialPort.Read(response, header_len, 3 - header_len);
                if (h_l > 0)
                {
                    header_len += h_l;
                }
                else
                {
                    retry++;
                    Task.Delay(config.RetryInterval.Value).Wait();
                }
            }

            int byte_counts = response[1] >= ModbusConstants.ModbusExceptionCode ? 2 : response[2] + 2;

            while (data_len < byte_counts && retry < config.RetryCount)
            {
                d_l = m_serialPort.Read(response, 3 + data_len, byte_counts - data_len);
                if (d_l > 0)
                {
                    data_len += d_l;
                }
                else
                {
                    retry++;
                    Task.Delay(config.RetryInterval.Value).Wait();
                }
            }

            if (retry >= config.RetryCount)
            {
                response = null;
            }

            return(response);
        }
Пример #2
0
        public int Read(byte[] data)
        {
            var response = serialDevice.Read(data.Length, -1, timeout);
            var count    = response.Length;

            for (var i = 0; i < count; i++)
            {
                data[i] = response[i];
            }
            if (monitor != null)
            {
                monitor('<', data, count);
            }
            return(count);
        }