尝试从串行端口接收数据时遇到问题 [英] Trouble trying to receive data from a serial port

查看:170
本文介绍了尝试从串行端口接收数据时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到我的PC的端口,该端口不断发送数据,我的目标是从中接收数据并打印出来.问题是我真的不知道它正在发送什么类型的数据以及我应该如何读取...

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;
    }

这是我的代码,试图打开端口并读取数据.我尝试调试它,ReadExising函数返回一串我听不懂的符号.我尝试了几种方法,但是都没有用,现在我很绝望.很抱歉,可能是一些不必要的代码行,因为我是SerialPort通信的完整入门者.

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.

推荐答案

您所希望的就是串行发送可读文本.在这种情况下,您的主要目标是弄清楚波特率.由于您是初学者,因此只需安装Docklight(或者可以安装picocom或任何可用的工作工具).尝试从最高(115200)[检查注意事项]和8个数据开始的每种波特率,然后向下尝试,对于这种方式检查的每种波特率,请尝试使用停止位和奇偶校验.

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.

或者(感谢Trevor的启发),您可以运行一个工具来检查重复的1和0.例如,如果您的流是110011001100000011110011,则好的候选对象是115200/2.拥有111000111000111000000111000111111111的优秀候选人是115200/3等

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

为了完整起见,上述方法可能没有定论,因为数据可能"实际上是1100110011001100.由于这是一种无效的编码,因此没有什么意义,但与原始问题我们不这样做"保持一致不知道串行发送的内容.

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.

现在,您的希望是,您最终会偶然发现正确的配置并真正获得可读的文本.如果这样做,则可以移回C#并设置正确的配置,然后继续进行调试.

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.

注释:

即使您可能会收到一些设备信息(例如波特率9600),也仍然建议您执行上述步骤. Infact OP偶然发现错误的信息,而9600无效,115200无效.

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被认为是UART的最大值.但是通过适当的接口,您可以达到230400和460800甚至更多.要使此答案永不过时,请验证硬件和串行驱动程序支持的最大接口.

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.

这篇关于尝试从串行端口接收数据时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆