0

UART data transmission Raspberry pi UART (RX TX) Arduino I know should avoid async void. They are only used for event handling. For other methods return 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?

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);
}
1

Working code for communication UART (RX TX) arduino and Raspberry pi It works through a voltage converter 5V -> 3.3V. RX TX do not forget to swap.

    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 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);
        }
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.