我正在嘗試創建一個初始化樹莓和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