C#从串口读取数据
我尝试过使用 Readline() 并且数据被丢弃,我尝试过使用 Read() 但我不确定如何有一种防错方法来做到这一点,因为我可能会一个接一个地收到多个数据包,但我无法知道将会有另一个数据包进入。在数据包之间,BytesToRead 为 0,所以我无法使用它。 当将数据读取到缓冲区时,您是否有一个计时器或让线程进入睡眠状态以允许所有数据包到达?
我搞不清楚了。 不知道接下来要尝试什么。
我应该提到的是,我不能保证来自串行端口的字符串将以 \n 或 \r 或 \r\n 结尾。 我只需要一种万无一失的方法来读取当用户按下“打印”时来自秤的所有数据包。
有人在这里回答了我喜欢的想法 - 等待所有数据包一定的时间,但他们删除了他们的答案。 您有机会重新发布吗?
I have tried using Readline() and data gets dropped, I tried using Read() but I am not sure how to have an error proof method of doing it, since I may get several packets one after another and I have no way of knowing that there is going to be another packet coming in. In between packets BytesToRead is 0, so I can't use it. When reading data to the buffer to you have a timer or put the thread to sleep to allow for all the packets to arrive?
I am lost. Don't know what to try next.
I should mention that I get no guarantee that the string coming off the serial port will be ended with \n or \r or \r\n. I simply need a fool proof way to read ALL the packets that will come from the scale when the user presses PRINT on it.
Someone answered here with the idea I liked - waiting for a certain amount of time for all the packets, but they erased their answer. ANy chance you could re-post it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过收听
DataRecieved<
的 /code>
事件SerialPort
类?Have you tried listening to the
DataRecieved
event of theSerialPort
class?不久前我们也经历过同样的过程。
读取“数据包”的唯一方法是了解它们在流中的开始和结束位置。
来自 msdn :
我们使用后台线程(您可以使用BackgroundWorker)将数据读入缓冲区。
如果您无法使用 SerialPort.Newline 属性可靠地设置终止字符(因为它会有所不同!),那么您将需要实现自己的数据包检测系统,因为您将无法使用阻塞 SerialPort.Readline()方法。
您可以使用 SerialPort.Read()(或使用 SerialPort.ReadExisting() 方法读取字符串)读取字节缓冲区,并在检测到数据中的有效数据包时生成事件。 请注意, Read() (我假设 ReadExisting() )清空了 SerialPort 的缓冲区,因此您需要将数据保存在其他地方。
如果设置 SerialPort.ReadTimeout,则可以处理 TimeoutException,并有一种简单的方法来处理设备不传输的情况。 如果您使用固定字节数或其他非终止方案,这是重置数据包检测的好方法。 (如果不需要部分数据包,请在超时时使用 SerialPort.DiscardInBuffer() )。
祝你好运
We went through the same process a while ago.
The only way to read 'packets' is to have some concept of where the start and end of them are in a stream.
From msdn:
We used a backround thread (you could use a BackgroundWorker) to read the data into a buffer.
If you can't reliably set the terminating character using the SerialPort.Newline property (because, say, it varies!) then you will need to implement your own packet detection system because you wont be able to use the blocking SerialPort.Readline() method.
You could just read into a byte buffer using SerialPort.Read() (or string using the SerialPort.ReadExisting() method) and generate an event when you detect a valid packet in the data. Note that Read() (and I assume ReadExisting() ) empty the SerialPort's buffer, so you'll need to keep the data somewhere else.
If you set the SerialPort.ReadTimeout, you can handle the TimeoutException and have an easy way to handle conditions where your device is not transmitting. This is a good way to reset your packet detection if you are using a fixed number of bytes, or some other non-terminated scheme. (use SerialPort.DiscardInBuffer() on a timeout if you don't need partial packets).
Good luck
由于字节可能随时传入,因此缓冲传入数据至关重要。 所以你应该
我只是想知道你是否仍然有串行端口的问题。 如果是这样,我用 C# 开发了一种串行端口编程语言,我相信它可以解决几乎每个人遇到的所有问题。
请您看一下并尝试一下好吗? 例如; 您可以像下面这样缓冲来自串行端口的传入数据,并轻松执行字符串操作。
该项目可在 sourceforge 上免费获取,如果您有任何疑问,请随时询问。
项目主页
下载链接
Since bytes may come in at any time, buffering incoming data is critical. So you should
I am just wondering if you are still having problems with the serial port. If so, I developed a serial port programming language in C# and I believe it solves nearly all of the problems that everybody encounters with.
Would you please take a look and try it ? For example; you can buffer incoming data from the serial port like the following and do string operations easily.
Project is freely available on sourceforge and if you have any questions, please feel free to ask.
Project Homepage
Download Link