Matas Senkus
Matas Senkus

Reputation: 29

Trouble trying to receive data from a serial port

Hi I have a port connected to my pc which is sending data constantly and my objective is to receive data from it and print it out. The problem is I don't really know what type of data it's sending and how should I read it...

private void Button_Receive_Data_Click(object sender, EventArgs e)
    {
        GroupBox_Serial_Transmit.Enabled = false;
        #region
        string Port_Name = ComboBox_Available_SerialPorts.SelectedItem.ToString();
        int Baud_Rate = Convert.ToInt32(ComboBox_Standard_Baundrates.SelectedItem);


        COMport = new SerialPort(Port_Name, Baud_Rate);
        #endregion
        COMport.ReadTimeout = 3500; //3.5 sekundes



        try
        {
            COMport.Open();
            if (COMport.IsOpen == true)
            {
                TextBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                ReceivedData = COMport.ReadExisting();
                this.Invoke(new EventHandler(ShowData));

                GroupBox_Serial_Transmit.Enabled = true;

            }
            else
            {
                MessageBox.Show("Unable to Write to COM port ");
                GroupBox_Serial_Transmit.Enabled = true;
                COMport.Close();
            }
        }
        catch (TimeoutException SerialTimeOutException)
        {
            MessageBox.Show(SerialTimeOutException.ToString());
            COMport.Close();

            GroupBox_Serial_Transmit.Enabled = true;
        }
    }

    private void ShowData(object sender, EventArgs e)
    {
        TextBox_System_Log.Text = ReceivedData;
    }

Here's my code which tries to open the port and read the data. I tried debugging it and the ReadExising function returns a string of symbols which I can't understand. I tried a couple of ways but none of the worked and right now I'm desperate. I'm sorry there might be some unnescesarry lines of code cause I'm a complete beginner in SerialPort communications.

Upvotes: 1

Views: 1057

Answers (1)

Attersson
Attersson

Reputation: 4866

All you can hope is the serial sends readable text. In which case your primary goal is to figure out the baud rate. Since you are a beginner, just install docklight (or maybe picocom or whatever working tool you have available). Try every baud rate starting from the highest (115200) [check notes below] and 8 data, going downwards and for every baud rate you check this way, mess around with stop bits and parity.

Alternatively (Thanks to Trevor for inspiration) you could run a tool to check for repeating 1s and 0s. For example if your stream is 110011001100000011110011 a good candidate is 115200/2. With 111000111000111000000111000111111111 a good candidate is 115200/3 etc

For the sake of completeness, the above method might be inconclusive, since the data "might" actually be 1100110011001100. Which would make little sense as this is a non efficient encoding, but coherently with the original problem, "we don't know" what the serial sends.

Now your hope is that you finally stumble upon the correct configuration and actually get readable text. If you do, you can move back to C# and set the correct configuration and resume your debug.

If you don't find any, this means the data is in binary format and we can have no certainty about the correct transmission configuration and the data content.

Your next bet would be saving a long enough log and open it with a hex editor.


Notes:

Even though you may receive some device info (e.g. baud rate 9600) the above steps are still recommended. Infact OP stumbled across wrong info whereas 9600 did not work, 115200 did.

115200 is considered the max for UART. But with proper interface you can reach 230400 and 460800... and possibly more. To make this answer future-proof, please verify what your interface maximum is as supported by both your hardware and serial driver.

Upvotes: 1

Related Questions