[英]UART Raspberry pi Windows IoT Core Arduino
UART數據傳輸Raspberry pi UART(RX TX)Arduino我知道應該避免異步無效。 它們僅用於事件處理。 對於其他方法,請返回Task。 異步等待在哪里出錯? 為什么數據不來?
誰知道顯示如何在UART上接收數據的鏈接?
using Windows.Storage.Streams;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
public async void Serial()
{
string aqs = SerialDevice.GetDeviceSelector("UART0"); /* Find the selector string for the serial device */
var dis = await DeviceInformation.FindAllAsync(aqs); /* Find the serial device with our selector string */
SerialDevice SerialPort = await SerialDevice.FromIdAsync(dis[0].Id); /* Create an serial device with our selected device */
/* Configure serial settings */
SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
SerialPort.BaudRate = 9600; /* mini UART: only standard baudrates */
SerialPort.Parity = SerialParity.None; /* mini UART: no parities */
SerialPort.StopBits = SerialStopBitCount.One; /* mini UART: 1 stop bit */
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);
}
通信UART(RX TX)arduino和Raspberry pi的工作代碼它通過5V-> 3.3V電壓轉換器工作。 RX TX別忘了交換。
public sealed partial class MainPage : Page
{
private SerialDevice SerialPort;
private DataWriter dataWriter;
private DataReader dataReader;
string rxBuffer;
CancellationTokenSource ReadCancellationTokenSource = new CancellationTokenSource();
public MainPage()
{
this.InitializeComponent();
InitSerial();
}
private async void InitSerial()
{
string aqs = SerialDevice.GetDeviceSelector("UART0");
var dis = await DeviceInformation.FindAllAsync(aqs);
SerialPort = await SerialDevice.FromIdAsync(dis[0].Id);
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);
Listen();
}
public async void SerialReceived()
{
/* Read data in from the serial port*/
const uint maxReadLength = 1024;
dataReader = new DataReader(SerialPort.InputStream);
uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
rxBuffer = dataReader.ReadString(bytesToRead);
receivedData.Text = rxBuffer;
}
public async void SerialSend(string txBuffer2)
{
/* Write a string out over serial */
string txBuffer = txBuffer2;
dataWriter.WriteString(txBuffer);
uint bytesWritten = await SerialPort.OutputStream.WriteAsync(dataWriter.DetachBuffer());
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SerialSend("Hello Serial");
}
private async void Listen()
{
try
{
if (SerialPort != null)
{
dataReader = new DataReader(SerialPort.InputStream);
// keep reading the serial input
while (true)
{
await ReadAsync(ReadCancellationTokenSource.Token);
}
}
}
catch (Exception ex)
{
throw new Exception("Uart Error", ex);
}
}
private async Task ReadAsync(CancellationToken cancellationToken)
{
Task<UInt32> loadAsyncTask;
uint ReadBufferLength = 1024;
// If task cancellation was requested, comply
cancellationToken.ThrowIfCancellationRequested();
// Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
dataReader.InputStreamOptions = InputStreamOptions.Partial;
// Create a task object to wait for data on the serialPort.InputStream
loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
// Launch the task and wait
UInt32 bytesRead = await loadAsyncTask;
if (bytesRead > 0)
{
receivedData.Text = dataReader.ReadString(bytesRead);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.