串口检索? (问号) [英] Serialport retrieving ? (QUESTION MARKS)

查看:109
本文介绍了串口检索? (问号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用带有COM线的serialport从医疗KERATOMETER设备中检索数据,一切正常,但我正在检索??????? (问号)

我必须使用(Encoding.GetEncoding)

谢谢



我是什么尝试过:



Hi i am using serialport with COM cable to retrieve a data from a medical KERATOMETER device , everything OK but i am retrieving ??????? (QUESTION MARKS)
must i use (Encoding.GetEncoding)
thanks

What I have tried:

private void btnOpenPort_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen) { serialPort1.Close(); }
                txtRead.Text = "";
                currentCOM = LstCom.SelectedItem.ToString();
                serialPort1.PortName = currentCOM;
                serialPort1.Open();
                txtRead.Text = string.Format("{0} :   {1}", "CONNECTED!!!", currentCOM);
                txtRead.Text += Environment.NewLine;
                txtRead.Text += Environment.NewLine;
                txtRead.Text += Environment.NewLine;

                WriteCom.Properties.Settings.Default.COM = currentCOM;
                WriteCom.Properties.Settings.Default.BaudRae = (int)LstBaudRate.SelectedItem;
                WriteCom.Properties.Settings.Default.Save();

            }
            catch (Exception ex)
            {
                txtRead.Text = ex.Message;
            }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            txtRead.Text += serialPort1.ReadExisting();
        }

推荐答案

问号可能意味着这不是可显示的字符。

从通信手册开始,使用Hyperterminal或类似设备,确保使用正确的通信设置(波特率,BPC,奇偶校验,停止位)与正确的设备进行通信,并确保您收到的是协议手册告诉你你应该。

当看起来正确时,转移到代码,而不是之前!

然后,停止这样做!

如果您阅读文档 [ ^ ],DataRecived事件在非UI线程上处理 始终 - 因此您无法直接访问文本框:您必须使用Invoke 。

修复它时,不要使用ReadExisting - 使用Read方法将数据作为字节数组读取,并(用于测试)将这些数据转换为显示的十六进制值,而不是将它们视为字符。这样,您将能够更清楚地看到您正在接收的内容,以及它与协议手册所说的内容的比较。手册应该告诉你它发送的数据是什么字符集,你不能只假设它是可读的Unicode:它可能是打包的数据,它可能是ASCII格式。
A Question mark probably means "this isn't a displayable character".
Start with the manual for the communications, and use Hyperterminal or similar to ensure you are communicating with the right device, using the right communication settings (baud rate, BPC, parity, stop bits) and that you receive exactly what the protocol manual tells you that you should.
When that looks right, move to code, and not before!
Then, stop doing it like that!
If you read the documentation[^], the DataRecived event is always handled on a non-UI thread - so you cannot access the textbox directly: you must use Invoke.
When you fix that, don't use ReadExisting - use the Read method to read the data as an array of bytes, and (for testing) convert those to hex values fro display instead of treating them as characters. That way, you will be able to see a lot more clearly exactly what you are receiving, and how it compares to what the protocol manual says you should get. The manual should tell you what character set any data it sends is in, you can't just assume it's going to be readable Unicode: it's likely to be "packaged" data and it may be in ASCII instead.


这篇关于串口检索? (问号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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