C# - 从串口缓冲区读取
我正在尝试从 RS-232 端口读取数据。有谁有一个例子说明我如何从端口/缓冲区获取数据并确保我拥有所有数据,因为它可以是多行数据。
我只是简单地读如下吗?
string Rxstring = port.ReadLine();
Console.WriteLine(Rxstring);
I am trying to read data from an RS-232 port. Does anyone have an example of how I get the data from the port/buffer and make sure that I have all the data as it can be multiline data.
Do I simply read it as follows ?
string Rxstring = port.ReadLine();
Console.WriteLine(Rxstring);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问:如何从端口/缓冲区获取日期,或从连接的设备输入数据。并确保您拥有所有数据。
答:我广泛使用 .net 串行端口类驱动程序,负责创建可靠、健壮的代码。这意味着被测连接设备必须在很长一段时间内运行并且不会出现故障。串行端口可能并且确实会丢失数据!别忘了这一点。
这应该足以帮助您入门。这是多年来用 C# 创建定制终端模拟器的结果。还有其他事情可以做,特别是如果您有大量 I/O 数据,您需要与设备建立握手。您必须让设备以设备满意的速度进行处理。在必须传输较大数据的情况下,请考虑设置一个简单的数据包传递协议和命令信号量构造 - 或使用控制器/设备设计使用的协议。
Q: how to get the date from the port/buffer, or input data from your connected device. AND make sure that you have all the data.
A: i have worked extensively with .net serial port class drivers where i was tasked to create reliable, robust code. this means that a connected device under test has to run and NOT fail over a LONG period of time. Serial port can AND does lose data! don't forget that.
this should be plenty to get you started. this is result of years of creating customized terminal emulators in c#. there are other things that can be done, particularly if you have large amount of i/o data you need to set up handshaking with device. you have to let the device handle at a rate that the device is happy with. in cases where larger data has to be transferred consider setting up a simple packet passing protocol and command semaphore construct - or use a protocol as defined that the controller / device is designed to work with.
试试这个:
详细信息可以在 Coad 代码。
Try this:
Details can be found at Coad's Code.