我正在尝试创建一个初始化树莓和arduino之间的串行连接的类。课程还应能够读写已建立的串行连接。为什么我的异步方法不等待“serialPort = await SerialDevice.FromIdAsync()”?
我使用的代码来自microsoft developers website。
using Windows.Storage.Streams;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
public async void Serial()
{
/* Find the selector string for the serial device */
string aqs = SerialDevice.GetDeviceSelector("UART0");
/* Find the serial device with our selector string */
var dis = await DeviceInformation.FindAllAsync(aqs);
/* Create an serial device with our selected device */
SerialDevice SerialPort = await SerialDevice.FromIdAsync(dis[0].Id);
/* Configure serial settings */
SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
SerialPort.BaudRate = 9600;
SerialPort.Parity = SerialParity.None;
SerialPort.StopBits = SerialStopBitCount.One;
SerialPort.DataBits = 8;
/* Write a string out over serial */
string txBuffer = "Hello Serial";
DataWriter dataWriter = new DataWriter();
dataWriter.WriteString(txBuffer);
uint bytesWritten = await SerialPort.OutputStream.WriteAsync(dataWriter.DetachBuffer());
/* Read data in from the serial port */
const uint maxReadLength = 1024;
DataReader dataReader = new DataReader(SerialPort.InputStream);
uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
string rxBuffer = dataReader.ReadString(bytesToRead);
}
我在serial.appxmanifest中添加了serialcommunication功能。
<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
到目前为止一切正常。树莓在另一端成功发送和接收来自Arduino的消息。
现在我试着单独做这些步骤。首先初始化串行连接,以便在应用程序需要时使用读取和写入功能。
的MainPage
namespace SerialUART
{
public sealed partial class MainPage : Page
{
private SerialController serial = new SerialController();
public MainPage()
{
this.InitializeComponent();
serial.Write();
serial.Read();
}
}
}
SerialController
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Storage.Streams;
namespace SerialUART
{
class SerialController
{
private SerialDevice SerialPort;
private DataWriter dataWriter;
private DataReader dataReader;
public SerialController()
{
InitSerial();
}
private async void InitSerial()
{
string aqs = SerialDevice.GetDeviceSelector("UART0");
var dis = await DeviceInformation.FindAllAsync(aqs);
SerialPort = await SerialDevice.FromIdAsync(dis[0].Id);
// The program does not wait here and executes Write()
// after Write() the following code will be executed
SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
SerialPort.BaudRate = 9600;
SerialPort.Parity = SerialParity.None;
SerialPort.StopBits = SerialStopBitCount.One;
SerialPort.DataBits = 8;
dataWriter = new DataWriter();
dataReader = new DataReader(SerialPort.InputStream);
}
public async void Read()
{
/* Read data in from the serial port */
const uint maxReadLength = 1024;
uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
string rxBuffer = dataReader.ReadString(bytesToRead);
Debug.WriteLine(rxBuffer);
}
public async void Write()
{
/* Write a string out over serial */
string txBuffer = "Hello Serial";
dataWriter.WriteString(txBuffer);
uint bytesWritten = await SerialPort.OutputStream.WriteAsync(dataWriter.DetachBuffer());
}
}
}
此代码不会等待一个串口,dataWriter和DataReader初始化。所以他们永远不会被分配到。
谁能向我解释为什么它不等待串行连接?它应该如何完成?
所有帮助表示赞赏。 在此先感谢!
感谢您的回复。这是我第一次需要处理异步,并期待完全不同的行为。我通过使InitSerial()成为任务来解决问题,并等待任务的完成。 – MKFreebird