Serial port Windows Iot Core上串行设备上的读取任务
我正在尝试编写一个Serial port Windows Iot Core上串行设备上的读取任务,serial-port,raspberry-pi3,uart,windows-10-iot-core,Serial Port,Raspberry Pi3,Uart,Windows 10 Iot Core,我正在尝试编写一个任务,该任务将一直运行到取消,该任务将读取UART接收到的任何数据,并通过定义的事件继续发送接收到的数据。我希望读取循环读取200毫秒,然后返回它在该时间片中读取的任何数据(可能包括零数据),或者读取了完整的缓冲区,以先发生的为准;大多数情况下,超时将首先发生。我已成功地将数据发送出UART,但我未能接收任何数据,即使已确认设备已在线路上接收到数据 SerialDevice device; // Instantiate the SerialDevice. device.Re
任务
,该任务将一直运行到取消,该任务将读取UART接收到的任何数据,并通过定义的事件
继续发送接收到的数据。我希望读取循环读取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
成员,以便在需要时关闭任务
考虑到我在尝试这种方法时遇到的挑战,我想知道它是否应该工作,如果不工作,我需要做些什么才能让它工作
我已成功地将数据发送出UART,但未能
接收任何数据,即使已确认设备已接收
电线上的数据
我不知道你是如何“确认设备在线路上收到数据的”。设置InputStreamOptions.Partial时,即使线路上只有一个字节,这一行wait 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
考虑到我在尝试这种方法时遇到的挑战,我
我想知道它是否应该工作,如果不是,我需要做什么才能得到它
工作
是的,你的逻辑应该有效,除了我,读取超时从未发生。当您设置device.ReadTimeout=newtimespan(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!";
}
}
}
}
您可以参考。了解完整性,物理数据传输已通过示波器读数确认为已接收。我确实使用上述代码使读取工作正常(因为系统中存在硬件错误),但我没有收到超时信息。