0

I have been testing RaspberryPi3 with Windows IoT Core to communicate with my existing FreeScale Hardware via RS485. I have been using SerialUART sample as reference. After my UWP successfully init the UART port, I don't seems to be able to receive the RS485 data transmitted by my hardware.

My hardware RS485 UART are configured at 4800baudrate, 8-bit data format, non-parit & stop-in-wait-mode disabled. I managed to successfully init 4800-8-none-oneon UWP sample but the data transmitted by the hardware does not trigger and display on Read Data text block. The data transmitted from my hardware is in hex which is F5-01-55-4B

An error appear during the transmission. An exception error appear.

The RS485 circuit is as follow. enter image description here

Please advise did I miss out anything? Thanks.

mylim
  • 313
  • 4
  • 16
  • Have you checked the RX & TX connected correctly? You should connect Pi RX to RS485 UART TX, and Pi TX to RS485 UART RX. @mylim – Michael Xu Jan 22 '19 at 07:43
  • For the exception, which encode did you use to decode/encode the data? You can try to use UTF8 or Unicode? – Michael Xu Jan 22 '19 at 08:08
  • @MichaelXu-MSFT may i know where do I lookup to change the decode/encode? in `private async Task ReadAsync(CancellationToken cancellationToken)`, the `byteRead` isUInt32`. – mylim Jan 22 '19 at 10:03
  • @MichaelXu-MSFT I have also tried swapping the pinout for `Rx` and `Tx` .. the pin connection is correct. – mylim Jan 22 '19 at 10:05
  • Is the exception thrown at the the line `dataReaderObject.ReadString`? I'm not sure if you have modified the code in `ReadAsync` method. From the exception message, i think you need to make sure the received data could be encoded by Unicode. – Michael Xu Jan 23 '19 at 02:23
  • I think if the client did not receive the data transmitted from the hardware, the exception would not be thrown. – Michael Xu Jan 23 '19 at 02:36
  • @MichaelXu-MSFT the exceptionwas thrown at `rcvdText.Text = dataReaderObject.ReadString(bytesRead);` .. i have not yet modify the sample code. – mylim Jan 23 '19 at 03:23
  • You can try to use `dataReaderObject.ReadBuffer` instead of `dataReaderObject.ReadString` to read the byte data. – Michael Xu Jan 23 '19 at 05:42
  • @MichaelXu-MSFT it worked. `rcvdText.Text = dataReaderObject.ReadBuffer(bytesRead).ToString();` it's displayed as `system._comobject` .. – mylim Jan 23 '19 at 09:41

1 Answers1

1

You can refer to following code. Please note that ReadString method requires a length of "code units" to read. This is the reason each string is preceded by its length when "on the wire". In your scenario, you could not make sure the data transmitted from the hardware in code unit. I'm not sure if showing the data in hex format in the TextBox is acceptable for you.

    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!";
            }
        }
    }
Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Thanks it works! I wanted the data to be displayed in Hex. I will work on the transmit next. Thanks. – mylim Jan 24 '19 at 04:01