Windows 通用应用程序 SerialDevice 通信不是 sending/recieving 数据
Windows Universal App SerialDevice communication not sending/recieving data
我正在开发与 Arduino 设备通信的通用 Windows 10 应用程序。 我不想远程控制 Arduino。 我只想简单地向设备发送数据和从设备接收数据。我已经尝试了几种方法来执行此操作但无济于事。这是我现在正在尝试的:
SerialDevice tempDevice = await SerialDevice.FromIdAsync(devices.ElementAt(i).Id);
tempDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000);
tempDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000);
tempDevice.BaudRate = 115200;
tempDevice.Parity = SerialParity.None;
tempDevice.StopBits = SerialStopBitCount.One;
tempDevice.DataBits = 8;
tempDevice.Handshake = SerialHandshake.None;
tempDevice.IsRequestToSendEnabled = true;
DataWriter dataWriter = new DataWriter(tempDevice.OutputStream);
dataWriter.WriteString("Test string");
await dataWriter.FlushAsync(); // <-- program hangs here
testDevice.Dispose();
我正在使用 Windows.Devices.SerialCommunication.SerialDevice
和 Windows.Storage.Streams.DataWriter
我在标记的行周围添加了各种断点,并确定程序挂在那里。我什至尝试删除 await
关键字,程序仍然挂在同一行。为什么我无法向 Arduino 发送数据?
有几件事我不明白,除了为什么它不能完全工作之外:
- 首先,如果方法是异步的,为什么应用程序会停在那里? (就像我说的,我试过删除
await
关键字)
- 其次,为什么它在 1000 毫秒(1 秒)后不放弃,因为超时设置为 1000 毫秒?
我使用的是 Arduino Mega 2560,程序是 Windows 10 Universal App,用 C# 编码。
只需在打开端口之后和创建DataWriter之前添加暂停:
Task.Delay(1000).Wait();
这是我检查过的工作方法:
private async Task sendToPort(string sometext)
{
using (SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceId))
{
Task.Delay(1000).Wait();
if ((serialPort != null) && (sometext.Length != 0))
{
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
Task.Delay(1000).Wait();
try
{
using (DataWriter dataWriteObject = new DataWriter(serialPort.OutputStream))
{
Task<UInt32> storeAsyncTask;
dataWriteObject.WriteString(sometext);
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
txtStatus.Text = bytesWritten + " bytes written";
}
}
}
catch (Exception ex)
{
txtStatus.Text = ex.Message;
}
}
}
}
我正在开发与 Arduino 设备通信的通用 Windows 10 应用程序。 我不想远程控制 Arduino。 我只想简单地向设备发送数据和从设备接收数据。我已经尝试了几种方法来执行此操作但无济于事。这是我现在正在尝试的:
SerialDevice tempDevice = await SerialDevice.FromIdAsync(devices.ElementAt(i).Id);
tempDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000);
tempDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000);
tempDevice.BaudRate = 115200;
tempDevice.Parity = SerialParity.None;
tempDevice.StopBits = SerialStopBitCount.One;
tempDevice.DataBits = 8;
tempDevice.Handshake = SerialHandshake.None;
tempDevice.IsRequestToSendEnabled = true;
DataWriter dataWriter = new DataWriter(tempDevice.OutputStream);
dataWriter.WriteString("Test string");
await dataWriter.FlushAsync(); // <-- program hangs here
testDevice.Dispose();
我正在使用 Windows.Devices.SerialCommunication.SerialDevice
和 Windows.Storage.Streams.DataWriter
我在标记的行周围添加了各种断点,并确定程序挂在那里。我什至尝试删除 await
关键字,程序仍然挂在同一行。为什么我无法向 Arduino 发送数据?
有几件事我不明白,除了为什么它不能完全工作之外:
- 首先,如果方法是异步的,为什么应用程序会停在那里? (就像我说的,我试过删除
await
关键字) - 其次,为什么它在 1000 毫秒(1 秒)后不放弃,因为超时设置为 1000 毫秒?
我使用的是 Arduino Mega 2560,程序是 Windows 10 Universal App,用 C# 编码。
只需在打开端口之后和创建DataWriter之前添加暂停:
Task.Delay(1000).Wait();
这是我检查过的工作方法:
private async Task sendToPort(string sometext)
{
using (SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceId))
{
Task.Delay(1000).Wait();
if ((serialPort != null) && (sometext.Length != 0))
{
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
Task.Delay(1000).Wait();
try
{
using (DataWriter dataWriteObject = new DataWriter(serialPort.OutputStream))
{
Task<UInt32> storeAsyncTask;
dataWriteObject.WriteString(sometext);
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
txtStatus.Text = bytesWritten + " bytes written";
}
}
}
catch (Exception ex)
{
txtStatus.Text = ex.Message;
}
}
}
}