我试图从串口清除数据,运气不佳...

问题描述:

大家好,

我在使用串口读取时遇到了一些麻烦。我正在使用ReadExisting()读取数据但是一旦读取它,它似乎保留在端口上,直到使用以下单独的按钮单击清除



Hi All,
I am having a bit of trouble with some serial port reading. I am reading the data using ReadExisting() but once it is read it appears to remain on the Port until cleared using the following in a seperate button click

ComPort.DiscardInBuffer();
 ComPort.DiscardOutBuffer();

  rtbIncomingData.Text = "";



如果我手动点击按钮,它会清除相关缓冲区和富文本框并工作。我的想法是这有点奇怪,但得到了一个解决方案我从读取端口函数调用了单击例程这没有用。所以我将代码复制出函数并重试,仍然无法正常工作。我的想法是,这个数据太快了。如果我使用工具箱中的标准Windows计时器,有一个1秒的时间,这似乎可以解决问题,但我之前被这些计时器咬过,并且已经将代码发送给在我的机器上运行的客户端但是目前不是客户,我试图避免这种情况。任何一个任何想法,建议,请?


If I manually click the button it clears the relavent buffers and rich textboxes and works. My thinking was "thats a bit odd, but got a get around" and I called the click routine from the read port function this did not work. so I copied the code out of the function and tried again, still not working. My thinking was this was too quick for the data to be there. If I use a standard Windows timer from the tool box, with a 1 Sec period this appears to ''cure'' the issue but I have been bitten by these timers before and have sent code to clients that "Works on my Machine" but not the customers at the moment I am trying to avoid this. Any one any ideas, suggestions, please??





的确,那是有点奇。读取完成后应自动清理缓冲区。



无论如何,请在阅读后尝试Thread.Sleep(100)。我认为100ms就足够了。



问候。
Hi,

Indeed, that is bit odd. Buffers should be cleaned automatically when reading is done.

Anyway, try Thread.Sleep(100) after reading. I think 100ms should be enough.

Regards.




谢谢你我我想我现在已经解决了原因。这是一个半双工系统,我需要让我的软件发送并从中接收。我没想到。任何想法....

Glenn
Hi,
Thanks for that I think I have now worked out why. It''s a Half Duplex System, I need to get my software to send and recieve from it. I wasn''t expecting that. Any ideas....
Glenn


如果你阅读ReadExisting函数的文档,你会看到有一个警告,字节可能会离开因为函数的工作方式,在内部缓冲区中。 MSDN Library Link here [ ^ ]



如果使用CR或CR / LF终止进入的数据,则可以使用ReadLine ...它将阻塞直到它在输入流上看到CR字符。大多数设备将自动终止与CR的通信。如果您可以容忍可能的延迟或处理您可能无法获得CR的情况(通过指定该函数的超时并处理异常),那么这是最好的函数之一(我认为无论如何)。它基本上从设备返回一条消息,而不用担心检测CR /长度等。



或者,您可以定期查询串口通过检查BytesToRead属性来查找要出现的字符。如果大于0,请使用ReadByte,ReadChar或ReadTo,具体取决于您的编码和需求。



老实说,我不确定为什么ReadExisting甚至可用。我想对于你可能需要缓冲区内容和底层流的高速应用程序,你可能需要这样的东西,但对于一般的串口工作,你应该避免它。其他读取函数仅适用于缓冲区,这对于几乎所有内容都应该足够,并且避免了处理缓冲区和流的复杂性。



我'多年来,我们做了很多串口工作。如果您还有其他问题,请给我发消息,我会尽力给您一些帮助。



Jason
If you read the documentation on the ReadExisting function, you''ll see there is a warning that bytes could get left in the internal buffer because of the way that function works. MSDN Library Link Here[^]

If the data coming in is terminated with a CR or CR/LF, you can use ReadLine instead... which will block until it sees the CR character on the input stream. Most devices will terminate their communications with a CR automatically. If you can tolerate a possible delay or handle a situation where you may not get a CR (by specifying a timeout on that function and handling the exception) then this is one of the best functions to use (in my opinion anyway). It returns essentially a single "message" from the device without worrying about detecting the CR/Length/etc.

Alternately, you can query the serial port on a regular basis looking for characters to be present by checking the BytesToRead property. If that is greater than 0, use ReadByte, ReadChar, or ReadTo depending on your encoding and needs.

Honestly, I''m not sure why ReadExisting is even available. I guess for high-speed applications where you might need the contents of the buffer and the underlying stream, you might need something like this, but for general serial port work, you should probably avoid it. The other "read" functions work only on the buffer which should be sufficient for nearly everything and avoids the complexity of dealing with both the buffer and the stream.

I''ve done a lot of serial port work over the years. If you have any more questions, shoot me a message and I''ll try to give you some help.

Jason