1

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 Answer 1

2

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);
        }
    }
}
1
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Mar 27, 2020 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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