UWP C# UART RS485 通信问题

UWP C# UART RS485 Communication Issue

我一直在用 Windows IoT Core 测试 RaspberryPi3,以便通过 RS485 与我现有的 FreeScale 硬件进行通信。我一直在使用 SerialUART sample 作为参考。 我的UWP成功初始化UART口后,我的硬件好像收不到RS485传输过来的数据。

我的硬件 RS485 UART 配置为 4800 波特率、8 位数据格式、非奇偶校验和停用等待模式。我设法在 UWP 示例上成功初始化 4800-8-none-one,但硬件传输的数据不会触发并显示在 Read Data text block 上。 从我的硬件传输的数据是十六进制的,即 F5-01-55-4B

传输过程中出现错误。

RS485电路如下。

请告知我是否遗漏了什么? 谢谢。

您可以参考下面的代码。请注意 ReadString 方法需要 "code units" 的长度才能读取。这就是当 "on the wire" 时每个字符串前面都有其长度的原因。在您的场景中,您无法确定从代码单元中的硬件传输的数据。我不确定在 TextBox 中以十六进制格式显示数据是否适合您。

    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
        dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;

        using (var childCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
        {
            // Create a task object to wait for data on the serialPort.InputStream
            loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(childCancellationTokenSource.Token);

            // Launch the task and wait
            UInt32 bytesRead = await loadAsyncTask;
            if (bytesRead > 0)
            {
                //rcvdText.Text = dataReaderObject.ReadString(bytesRead);
                var bufferArray = dataReaderObject.ReadBuffer(bytesRead).ToArray();
                var content = string.Empty;
                foreach(var b in bufferArray)
                {
                    content += Convert.ToString(b,16).ToUpper() + " ";
                }

                rcvdText.Text = content;
                status.Text = "bytes read successfully!";
            }
        }
    }