微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

始终打印使用 Zigbee 读取的最后一条消息

如何解决始终打印使用 Zigbee 读取的最后一条消息

我使用带有 Zigbee 的 Arduino 来发送来自传感器的数据,使用带有另一个 Zigbee 的 RaspBerry Pi 来读取 10 字节的数据,其中包含一个头部和一个停止点。 我在 Windows 10 IoT 上使用 VS 2019 进行编码。 我的问题是我的 RPI 读取数据但越来越晚,所以我不再实时了。 我知道问题来自于迫使我阅读每条消息的缓冲区,我愿意改变它。 我只想读取缓冲区中的最后一条消息,但我不知道该怎么做。 我试图比发送数据更快地读取但错误,我还搜索了如何清空缓冲区但我没有找到。 谢谢您的帮助, 罗曼。

private async Task<ZigBeeCommResult> Read()
    {
        var retvalue = new ZigBeeCommResult();
        try
        {
            
            _dataReader = new DataReader(_zbCoordinator.InputStream);
            
            var numBytesRecvd = await _dataReader.LoadAsync(10);
            byte[] data = new byte[10];
            retvalue.TextResult = "";
            Debug.WriteLine(_dataReader.UnconsumedBufferLength);
            while (_dataReader.UnconsumedBufferLength > 0)
            {
                for (uint j = 0; j < 10; j++)
                {
                    data[j] = _dataReader.ReadByte();
                    //Debug.WriteLine(data[j]);
                    retvalue.TextResult += data[j];

                }
            }
            txtHeader.Text = retvalue.TextResult;


        }
        catch (Exception ex)
        {
            retvalue.IsSuccessful = false;
            retvalue.TextResult = ex.Message;
        }
        finally
        {
            if (_dataReader != null)
            {
                _dataReader.DetachStream();
                _dataReader = null;
            }
        }
        return retvalue;
    }

public void dispatcherTimerReaderSetup()
    {
        // configure dispatcher
        _dispatcherTimerReader = new dispatcherTimer();
        _dispatcherTimerReader.Tick += dispatcherTimerReader_Tick;
        _dispatcherTimerReader.Interval = new TimeSpan(0,500);
        _dispatcherTimerReader.Start();
    }

    public void dispatcherTimerReader_Tick(object sender,object e)
    {

        Read();
        
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。