Go Down

Topic: Processing serial.write command slow; any ideas? (Read 2 times) previous topic - next topic

A._Square

I need to send data to the Arduino quickly, and my messages weren't getting processed as fast as I expected. I assumed the problem was in my Arduino firmware, but, after much debugging, I realized that the problem was on the sending end. It turns out that Processing takes a full millisecond in between serial write commands, regardless of baud (at least on my Mac 10.5.2 2.33Ghz machine). Since the messages are received correctly, the actual message is being sent at the correct speed; it's just that the overhead of the command seems to be ridiculously high.

Does anyone have any ideas how to fix this? If not, what's a good alternative for my computer-side software? I fear I may need to switch over to C, which will make my life a little more difficult, but I suppose learning to do Serial I/O in C would come in handy eventually.

bloodline

I have a similar problem! I need to be able to send data very quickly. I profiled the Serial.write() function and found it takes 600microseconds to return (and thus continue program execution) after sending 4bytes of data. The speed of this function is dependent upon the baud rate, I assumed that the pins1/2 serial connection was handled by a UART and thus should be asynchronous to the program execution.

Does anyone have any ideas to speed this up?

Grumpy_Mike

Quote
It turns out that Processing takes a full millisecond in between serial write commands, regardless of baud


It's not processing it is the USB protocol. USB devices are polled every millisecond so that is where the delay is. I am not sure you can do anything about it.
Data transfer from the USB to the serial bridge is always done at the same rate regardless of the actual serial baud rate. If the baud rate is too slow the data just backs up in the buffer in the serial bridge chip.

bloodline

Hi, as I only sent 4bytes to test the Serial.write function, I was not limited by the transmit buffer and the USB polling... Is there any explanation for my slowness? Is the source code to the Serial object available?

Grumpy_Mike

Quote
I was not limited by the transmit buffer and the USB polling

Yes you were the USB device is only contacted every millisecond so no matter how few bytes you have there is always the potential for a 1mS delay.

bloodline

Hi Mike, appologies I don't think I wa clear in my original post. I did not check the transmission time from the Arduino to the computer. I timed how long the actual Serial.write() function took to execute on the board itself, at 115200 I was getting ~600microseconds at double the baud rate I was getting ~300 microseconds.

AFAIK, the AVR should transmit to the Serial-USB bridge at a constant rate and that should be handled by a hardware UART

Go Up