SerialPortEventListener 实现
我正在使用 Firmata 协议,它允许您通过串行协议控制 Arduino。我正在使用 SerialPortEventListener 监听 DATA_AVAILABLE 事件来读取传感器值。但我注意到有很多延迟,我的应用程序注册更新的传感器值需要一秒钟,协议以 57600 的波特率运行。我的问题是事件侦听器是否在单独的线程上运行,或者我的应用程序和侦听器在同一线程中运行,并且我的应用程序减慢了速度。
编辑:为了让我自己清楚,我只是在理论上问在单独的线程中读取串行传输或使用事件侦听器会更快吗?
I am playing with the Firmata protocol which allows you to control Arduino through a serial protocol. I am reading sensor values using SerialPortEventListener listening for DATA_AVAILABLE event. But i notice a lot of latency it takes a second for the updated sensor values to be registered by my application, protocol runs at a baud rate of 57600. My question is does the event listener run on a separate thread or does both my application and listener run in the same thread and my application slow things down.
EDIT: To make my self clear, i am just asking in theory would it be faster to read serial transmission in a separate thread or using the event listener?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对所有这些答案感到抱歉,我正在认真考虑您的问题。
除了从错误的线程更新 GUI 的问题之外,线程的问题可能是无关紧要的。由于从串行端口获取更新可能不会消耗大量 CPU 功率(无论如何,它不应该消耗大量 CPU 功率,除非它是通过紧密的轮询循环完成的),因此线程之间不会出现任何明显的 CPU 资源争用,并且因此线程不应该“互相减慢速度”。我几乎不认为这种可能性。但是您可以查看任务管理器 (Windows) 或系统监视器 (Linux),看看您的 CPU 是否非常繁忙;这可能会改变事情。
Sorry about all these answers, I'm thinking hard about your problem.
Apart from the problem of updating your GUI from the wrong thread, the issue of threads is probably pretty irrelevant. Since getting an update from your serial port probably doesn't consume a lot of CPU power (it shouldn't, anyway, unless it's done with a tight polling loop) there will not be any noticeable contention for CPU resources between your threads, and so there should be no possibility for threads to be "slowing each other down." I'd pretty much discount this possibility. But you could look in TaskManager (Windows) or System Monitor (Linux) to see if your CPUs are being kept really busy; that might change things.
没有看到你的应用程序,我不得不猜测;但是您是否可能尝试在 GUI 中显示这些事件,并从事件调度线程以外的线程更新 GUI?
当您简单执行
System.out.println()
时,延迟如何?Without seeing your app I have to guess; but is it possible you're trying to display these events in a GUI, and updating the GUI from a thread other than the Event Dispatch Thread?
How's the latency when you simply do
System.out.println()
?另一个猜测:当您收到
DATA_AVAILABLE
事件时,您是否还会获得有关可用数据量的一些信息?您可能尝试读取比缓冲区中更多的数据,并且在读取操作超时之前您无法看到所获得的数据。这表明超时设置为 1 秒。
Another guess: When you get your
DATA_AVAILABLE
event, do you also get some information about how much data is available?You could be trying to read more data than is in the buffer, and you don't get to see what you got until your read operation times out. This would indicate the timeout is set to 1 second.