【问题标题】:Reading Task on SerialDevice on Windows Iot Core在 Windows IoT Core 上的 SerialDevice 上读取任务
【发布时间】:2019-10-03 18:33:49
【问题描述】:

我正在尝试编写一个Task,它将一直运行到取消,它将读取 UART 接收到的任何数据并继续通过定义的event 发送接收到的数据。我希望读取循环读取 200 毫秒,然后返回它可能在该时间片中读取的任何数据(可能包括零数据)或读取完整缓冲区时,以先发生者为准;绝大多数情况下,超时将首先发生。我已成功从 UART 发送数据,但我未能接收到任何数据,即使已确认设备已通过线路接收数据。

SerialDevice device;

// Instantiate the SerialDevice.

device.ReadTimeout = new TimeSpan( 0, 0, 0, 0, 200 );
ReadDataCancellationTokenSource = new CancellationTokenSource();

const uint BufferSize_bytes = 50;

ReadDataTask = Task.Factory.StartNew( async () =>
{
    using( DataReader reader = new DataReader( device.InputStream ) )
    {
       reader.InputStreamOptions = InputStreamOptions.Partial;

       while( !ReadDataCancellationTokenSource.IsCancellationRequested )
       {
          uint dataRead = await reader.LoadAsync( BufferSize_bytes ).AsTask();

          if( dataRead > 0 )
          {
             IBuffer buffer = reader.ReadBuffer( dataRead );
             if( buffer.Length > 0 )
                PropogateReceivedData( ReadBuffer( buffer ) );
          }
       }
    }
}, ReadDataCancellationTokenSource.Token );

PropogateReceivedData( ReadBuffer( buffer ) )调用读取缓冲区中的所有数据,然后调度数据;众所周知,这可以正常工作。存储ReadDataCancellationTokenSource 成员以在需要时关闭Task

考虑到我在尝试采用这种方法时遇到的挑战,我想知道它是否应该奏效,或者如果不奏效,我需要做些什么才能让它发挥作用。

【问题讨论】:

    标签: serial-port raspberry-pi3 uart windows-10-iot-core


    【解决方案1】:

    我已成功从 UART 发送数据,但未能成功 接收任何数据,即使已确认设备已收到 电线上的数据。

    我不确定您是如何“确认设备接收到网络上的数据”的。当您设置InputStreamOptions.Partial 时,即使线路上只有一个字节,await reader.LoadAsync( BufferSize_bytes ).AsTask(); 将返回此行,您将在缓冲区中获得这一字节。

    另外我不知道你如何处理PropogateReceivedData( ReadBuffer( buffer ) )中接收到的数据,这里我使用以下几行将数据转换为字符串并显示在UI中,你可以参考:

                                    var dataReader = DataReader.FromBuffer(buffer);
                                    rcvdText.Text = dataReader.ReadString(buffer.Length);
    

    我的建议是,如果你想获取字符串数据,可以使用reader.ReadString(dataRead);,如果你想获取字节流,可以使用reader.ReadBytes(readData);。更多支持的API可以参考DataReader

    考虑到我在尝试采用这种方法时遇到的挑战,我 我想知道它是否应该工作,如果没有,我需要做什么才能得到它 上班。

    是的,你的逻辑应该有效,除了我,读取超时永远不会发生。您能否确认设置device.ReadTimeout = new TimeSpan( 0, 0, 0, 0, 200 ); 时读取超时对您有效?有没有超时信息?

    以下是我从你的问题中得到的要测试的全部代码行,它对我有用。

                    device.ReadTimeout = new TimeSpan(0, 0, 0, 0, 200);
                    var ReadDataCancellationTokenSource = new CancellationTokenSource();
    
                    const uint BufferSize_bytes = 50;
    
                    using (DataReader reader = new DataReader(device.InputStream))
                    {
                        reader.InputStreamOptions = InputStreamOptions.Partial;
    
                        while (!ReadDataCancellationTokenSource.IsCancellationRequested)
                        {
                            uint dataRead = await reader.LoadAsync(BufferSize_bytes).AsTask();
    
                            if (dataRead > 0)
                            {
                                IBuffer buffer = reader.ReadBuffer(dataRead);
                                if (buffer.Length > 0)
                                {
                                    //    rcvdText.Text = reader.ReadString(dataRead);
    
                                    //    var readData = new Byte[dataRead];
                                    //    reader.ReadBytes(readData);
    
                                    var dataReader = DataReader.FromBuffer(buffer);
                                    rcvdText.Text = dataReader.ReadString(buffer.Length);
                                    status.Text = "bytes read successfully!";
                                }
                            }
                        }
                    }
    

    Official serial sample你可以参考。

    【讨论】:

    • 为了完整起见,物理数据传输通过示波器读数确认为接收。我确实使用上面的代码使读数正常工作(因为系统中存在硬件错误),但我没有收到超时信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多