Hi everyone.I was hoping someone here can explain the time required to load the tx serial buffer on an Uno or Mega2560.A little backgroundI have a sketch which gets 32 bytes of data from 4 different I2C devices.Then I sort and package all that data up in an array with a few other things totaling up to 136 bytes. I then send that data out via serial to raspberry pi over usb. I have increased the tx buffer in hardwareserial.h to 256 bytes to accommodate for this large array.The sketch takes approximately 5.4 milliseconds (ms) to execute. 3.6ms of that is the 128 bytes over I2C and the rest (1.8ms) is the time to Serial.write 136 bytes at 460800 baud. My goal is to never overrun the tx buffer so 460800 baud is the minimum I can use (4bits=2.9ms).The problem comes in that I thought Serial.write would return almost instantly as long as the buffer had enough room for what was being sent. Meaning if I was sending 50 bytes and the buffer was 64 bytes (and the buffer was clear) then Serial.write would return immediately, the uart would take care of serial comms and the sketch would continue (the essence of non - blocking no?).
![]() ![]()
After everyone persuades him, Bamakhyapa changes his decision and promises to visit Kashi. But Ma Tara loses her temper and threatens Bamakhyapa with. Bamakhepa, goddess Tara's ardent devotee lived near the temple and meditated in the cremation grounds. He was a contemporary of another famous Bengali saint Ramakrishna.At a young age, he left his house and came under the tutelage of a saint named Swami Makshadananda, who lived in a village name Dakshingram, in Birbhum district.
![]()
But my experience is not the case. I get a linear increase in time based on the amount of data sending.data(bytes) time to Serial.write(ms)25 0.23250 0.4675 0.69100 0.928150 1.492200 1.94250 2.396but then also peculiarly I get a difference in time to return depending on what baudrate I am using (the below is sending 50 bytes).baud time to Serial.write(ms)115200 0.46230400 0.536460800 0.712921600 0.5So I guess it boils down to 2 main questions (with a few sub questions )1. Why does Serial.write take so long to return if the buffer has room for what is being sent?-Can you explain the timing?-Can it be faster?2. Why is Serial.write return time baud dependent?Below is some code that I have been using to benchmark times. Thanks PaulS for the reply.I am aware that serial.write etc block until there is room in the buffer. The whole goal of what I am trying to do is speed up the system. I do not want serial to block.
That is why I increased the buffer size. I could alternatively have made several smaller serial.write calls throughout the program but I didn't choose to do that.Regarding micros overflow, I definitely see overflow and it is dealt with accordingly. I never said I don't care. I have large Matlab post processing scripts to parse through the data so overflow in the timestamp is dealt with there.Regarding the code I posted, it runs the same whether you declare longs or ints. I just tried it.
I know it does not return instantly - it has work to do. But if the buffer is empty and the data is less than the buffer size the write duration is not affected by the baud rate.Conversely, if you send data that is longer than the buffer size the duration is affected by the baud rate. IIRC at 500,000 baud the buffer was being emptied faster than it could fill.And I forgot to mention that my tests were done with the standard 64 byte buffer.It is probably possible to write directly to the buffer bypassing Serial.It is certainly possible to write your own code to send Serial data without using the Serial library or its bufffer at all. That way you could also eliminate the 'cost' of copying the data from your array to the serial buffer.R.
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |