views:

212

answers:

3

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?

A: 

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() ?

Carl Smotricz
A: 

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.

Carl Smotricz
+1  A: 

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.

Carl Smotricz
"Sorry about all these answers": No problem :-). You do realize that you can just edit your existing answer, don't you?
sleske
I would certainly hope so! But these are all different and distinct answers, so I feel they should be presented that way. I know it's unusual to offer multiple answers, but the system allows it so I figure it must be a valid thing to do if I consider it appropriate.
Carl Smotricz