Search code examples
c#serial-portgoogle-coral

C# Serial Port: Read output data from an embedded Linux device


I am assigned to write a C# WPF application to connect to a Google Coral Development Board through the Debug Port and Stream all the data that the Board outputs to a Windows Screen (e.g. TextBlock).

I follow this Microsoft referemce for the reading events. For now, after I establish the connection to the board, I see the outputs in my Textblock but it is not in the readable text format.

What I expected:

U-Boot SPL 2019.04.1 (Jan 11 2021 - 20:43:00 +0000)
power_bd71837_init
Board id: 0
DDRINFO: start DRAM init
DDRINFO:ddrphy calibration done
DDRINFO: ddrmix config done
Normal Boot
Trying to boot from MMC1
hdr read sector 300, count=1


U-Boot 2019.04.1 (Jan 11 2021 - 20:43:00 +0000), Build: jenkins-enterprise.uboot                                                                                                                                                             -imx-1

CPU:   Freescale i.MX8MQ rev2.1 1500 MHz (running at 1000 MHz)
CPU:   Commercial temperature grade (0C to 95C) at 37C
Reset cause: POR
Model: Freescale i.MX8MQ Phanbell
DRAM:  4 GiB
MMC:   FSL_SDHC: 0, FSL_SDHC: 1
Loading Environment from MMC... *** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial

 BuildInfo:
  - ATF
  - U-Boot 2019.04.1

flash target is MMC:0
Net:
Warning: ethernet@30be0000 using MAC address from ROM
eth0: ethernet@30be0000
Fastboot: Normal
Normal Boot
Hit any key to stop autoboot:  0
2065 bytes read in 6 ms (335.9 KiB/s)

What I actually see from the TextBlock: enter image description here

I check the BaudRate and other settings, nothing seems to be incorrect. I know that UART protocol sends data as byte, can someone spot if my output if wrong data type? If so, do I have to read receiving byte and convert to readable text?

Adding My Code

//Port setting
_serialPort.PortName = comPort.Name;
_serialPort.BaudRate = 115200;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;         
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;

_serialPort.DtrEnable = true;
_serialPort.RtsEnable = true;

// Port Data Receiving method
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
_serialPort.Open();

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            this.Dispatcher.Invoke(() =>
            {
                TextBlock_Output.Text += indata; 
            });
        };

Hardware Connection I connect the Debug Port of the Google Coral Board to my Windows Computer through a micro-USB(Coral Board) to USB(Windows Computer). I normally use this connection for debugging through COM Port via Putty as well.

Thank you!


Solution

  • From the documentation, on all platforms they indicate using a terminal emulator like screen (linux/MacOS) or PuTTy (windows).

    A terminal emulator is more than simply a text box and the data stream will be more than simply readable ANSI characters. In addition to the text, the data stream will also include a variety of control characters, or escape sequences, that are used to indicate how to lay out and render the text-based UI to the user in a remote terminal window. You cannot use a plain serial port class to reproduce this behaviour - you require a fully featured terminal emulator. As a sender, you must also produce the correct control character outputs to be able to move the cursor in the terminal window, to send copy/paste/cancel or other commands, and perform other user interaction with the remote screen.

    A number of libraries exist that provide this functionality for C#, but recommending one is out of scope for answers here. You could also write your own terminal emulator, of course, processing the VT100 control sequences yourself, but this would be a considerable exercise. In any case, the data coming into the serial port needs to be parsed and interpreted before you can render the text to the screen. Simply printing the incoming characters to a text box will not work.