简体   繁体   English

UART Raspberry Pi Windows IoT核心版Arduino

[英]UART Raspberry pi Windows IoT Core Arduino

UART data transmission Raspberry pi UART (RX TX) Arduino I know should avoid async void. UART数据传输Raspberry pi UART(RX TX)Arduino我知道应该避免异步无效。 They are only used for event handling. 它们仅用于事件处理。 For other methods return Task. 对于其他方法,请返回Task。 async and await Where can there be a mistake? 异步等待在哪里出错? Why the data does not come? 为什么数据不来?

Who knows the link where it is shown how to receive data on the UART? 谁知道显示如何在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);

} }

Working code for communication UART (RX TX) arduino and Raspberry pi It works through a voltage converter 5V -> 3.3V. 通信UART(RX TX)arduino和Raspberry pi的工作代码它通过5V-> 3.3V电压转换器工作。 RX TX do not forget to swap. 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.

相关问题 Windows 10 IoT Raspberry Pi 2 上的 UART - UART on Windows 10 IoT Raspberry Pi 2 使用Raspberry Pi 3无法在Windows IoT上打开UART端口 - Unable to open UART port on Windows IoT with Raspberry Pi 3 .NET Core应用程序远程部署到Raspberry Pi上的Windows IoT Core - .NET Core app remote deployment to Windows IoT Core on Raspberry Pi 使用Windows 10 IOT Core在Raspberry Pi中播放wav文件 - play wav file in Raspberry Pi with Windows 10 IOT Core 语音识别在使用UWP的raspberry pi 3(Windows IoT核心版)中不起作用 - speech recognition not working in raspberry pi 3(windows iot core) using UWP Windows 10 iot上的.net核心安装(Raspberry PI3 - ARM) - .net core installation on Windows 10 iot (Raspberry PI3 - ARM) Windows 10 IoT核心版上的BackgroundDownloader在Raspberry Pi上未完成 - BackgroundDownloader on Windows 10 IoT Core does not complete on Raspberry Pi Arduino UNO通过I2c将DHT11传感器的温度和湿度数据发送到Raspberry Pi 2(运行Windows IoT核心) - Arduino UNO sending Temperature and Humidity data from DHT11 sensor to Raspberry pi 2(running windows iot core) through I2c Windows IoT Raspberry Pi 3 USB连接到C#后台应用程序中的Arduino - Windows IoT Raspberry Pi 3 USB Connection to Arduino in C# Background App Raspberry pi-如何在Windows IOT中使用Arduino Nano CH340 - Raspberry pi - How to use Arduino Nano CH340 with Windows IOT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM