SerialPort.ReadTo永远不会返回 [英] SerialPort.ReadTo never returns, ever

查看:529
本文介绍了SerialPort.ReadTo永远不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReadExisting可以很好地工作,但ReadTo不能.这是代码,请告诉我是否有问题...

ReadExisting works great but ReadTo does not.  Here is the code, please tell me if something sint right...

            SerialPort mPort;
mPort = new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One);
            mPort.Open();





 while (true)
            {
                lData += mPort.ReadTo(Environment.NewLine);
                Thread.Sleep(5);
            }

如果我使用read exiting,则可以正常工作...只是不使用ReadTo或ReadLine

If i use read exiting, it works perfectly fine... just not ReadTo or ReadLine

推荐答案

我唯一的解释是您的源未发送Environment.NewLine(即"\ r \ n"),但还有其他一些行分隔符.由于您说ReadLine也不起作用,并且因为默认分隔符为\ n,所以我猜您的源使用的是单个\ r,但是您最好检查一下数据来验证实际发送的内容使用ReadTo.

完成此操作后,您可以使用正确的分隔符调用ReadTo,也可以使用该分隔符设置SerialPort的NewLine属性,然后使用ReadLine.

HTH
--mc

The only explanation I have is that your source is not sending Environment.NewLine (i.e. "\r\n"), but some other line separator. Since you say that ReadLine is not working either, and since the default separator for that is \n, I'd guess your source is using a single \r, but you had better verify what is actually being sent by inspecting the data you are getting with ReadTo.

Once you do that, you can either call ReadTo with the correct separator, or you can set the NewLine property of your SerialPort with that separator and then use ReadLine.

HTH
--mc


这篇关于SerialPort.ReadTo永远不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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