SerialPort - 清除现有数据
我使用 .NET SerialPort 类与仪表进行通信。每次按下仪表上的按钮时,都会通过端口发送读数值。
我遇到的问题是,如果在程序关闭时按下仪表按钮,那么当程序启动时,它会触发现有数据的数据接收事件。我不想要这种行为——我只想收集端口打开后完成的读数。
我目前有一个使用 Thread.Sleep 的解决方法,但它看起来很老套,我想知道是否有人知道更好的解决方案。
port = New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
port.Open()
port.DtrEnable = True
System.Threading.Thread.Sleep(100)
port.ReadExisting()
AddHandler port.DataReceived, AddressOf PortDataReceived
如果我没有 Thread.Sleep,则会针对先前的仪表数据触发 DataReceived 事件。我猜这是因为 ReadExisting 不会阻塞,所以当它在打开后立即调用时,它什么也不读取。
谢谢
所以,如果您在打开其零后立即调用“port.BytesToRead()”。如果你在打开后100ms调用它,它的非零值。我想这是有道理的——打开后需要一段时间才能读取现有数据。
另一种选择是调用 read 并设置读取超时,但这似乎与 Thread.Sleep 没有什么不同,除非你必须捕获异常......
I'm using the .NET SerialPort class to communicate with gauges. Every time a button on a gauge is pressed a reading value is sent over port.
The problem I have is that if the gauge button is pressed while the program is off then when the program starts it fires the data received event for that existing data. I don't want this behavior -- I only want to collect readings done after the port is opened.
I have a current workaround using a Thread.Sleep but it seems hacky and I'm wondering if anyone knows of a better solution.
port = New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
port.Open()
port.DtrEnable = True
System.Threading.Thread.Sleep(100)
port.ReadExisting()
AddHandler port.DataReceived, AddressOf PortDataReceived
If I don't have the Thread.Sleep then the DataReceived event is fired for the previous gauge data. I'm guessing this is because the ReadExisting doesn't block and so when its called immediately after an open it just reads nothing.
Thanks
So if you call 'port.BytesToRead()' immediately after open its zero. If you call it 100ms after open its non-zero. I guess this makes sense -- it takes time after open to read the existing data.
Another option is to call read and set the read timeout but that seems no different than Thread.Sleep except you have to catch an exception...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会产生副作用。仪表可能会上下跳动,急于发送读数。毕竟,有人按下按钮告诉它可以发送了。但其传输例程正在遵守握手协议,数据终端就绪未打开。跳啊跳啊!它已经打开了。发送。这需要一段时间,取决于波特率。
那么,为什么即使计算机还没有准备好,有人还是按下了按钮呢?为什么它总是准备就绪?回答这个问题,然后你就可以删除Sleep()。还要了解当有人在计算机准备就绪之前按下按钮一百次时会发生什么。你的 Sleep() 可能太短了。
That could have a side-effect. The gauge might be jumping up and down, eager to send the reading. After all, somebody pressed the button to tell it that it's okay to send. But its transmit routine is observing the handshake protocol, Data Terminal Ready is not on. Jump, jump, ah! it's on. Send. That takes a while, depends on the baudrate.
Well, why did somebody press the button even though the computer wasn't ready? Why isn't it always ready? Answer that question, then you can delete Sleep(). Also find out what happens when that somebody presses the button a hundred times before the computer is ready. Your Sleep() might well be too short.
如果您不需要收集串行端口输出缓冲区中等待的内容,请尝试调用
serialPort.DiscardWriteBuffer()serialPort.DiscardOutBuffer() ,它应该清除那里收集的所有内容。感谢乍得纠正我的错误。If you don't need to collect what is waiting in the out buffer of the serialport try calling the
serialPort.DiscardWriteBuffer()serialPort.DiscardOutBuffer() which should clear out whatever has collected there. Thanks to Chad for correcting my error.