[英]How to read bytes from SerialPort.BaseStream without Length
I want to use the stream class to read/write data to/from a serial port. 我想使用流类从串行端口读取/写入数据。 I use the BaseStream to get the stream (link below) but the Length property doesn't work. 我使用BaseStream获取流(下面的链接),但是Length属性不起作用。 Does anyone know how can I read the full buffer without knowing how many bytes there are? 有谁知道我如何在不知道有多少字节的情况下读取完整的缓冲区?
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.basestream.aspx http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.basestream.aspx
You can't. 你不能 That is, you can't guarantee that you've received everything if all you have is the BaseStream
. 也就是说,如果您拥有的只是BaseStream
,则不能保证已收到一切。
There are two ways you can know if you've received everything: 您可以通过两种方式知道是否已收到一切:
Or, depending on your application, you can do some kind of timing. 或者,根据您的应用程序,您可以执行某种计时。 That is, if you haven't received anything new for X number of seconds (or milliseconds?), you assume that you've received everything. 也就是说,如果您在X秒(或毫秒?)内没有收到任何新消息,则认为您已经收到了所有消息。 That has the obvious drawback of not working well if the sender is especially slow. 如果发件人特别慢,那显然有不能正常工作的缺点。
也许您可以尝试SerialPort.BytesToRead属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.