SerialPortEventListener Implementation
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 lat开发者_运维技巧ency 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?
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.
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()
?
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.
精彩评论