I am using an Arduino Due to collect a large amount of data from an encoder (about 1kb). Afterwards I need to send the collected data to a C# application I wrote over the serial port. I serialized my tx data into a byte buffer and simply used this code to transmit:

void sendEncoderData()
  byte buffer[1024];

  /*** some code that populates buffer ***/

  int bytesToSend = 1024;
  Serial.write(buffer, bytesToSend);

When I run the code, nothing seems to get received by the C# application. In fact, I never see the Tx LED light up. However, if I change bytesToSend to a value smaller than 64 bytes everything works. Is this a limitation of Serial.write() or is it something else? What are ways I can work around this limitation?

More information: sendEncoderData() is called by an interrupt service routine. However prior to the call, I made sure to 'detachInterrupt()` on all pins including the pin associated with said ISR.

  • Have you tried sending in 64-byte chunks? Aug 21, 2015 at 8:37
  • That's a snippet, isn't it? snippets-r-us.com
    – Nick Gammon
    Aug 21, 2015 at 9:23
  • @NickGammon, I would be a snippet if Serial.write() is supposed to be capable of sending more than 64 bytes at a time. If that's the case: 1. My assumption was wrong and perhaps you could have pointed it out. 2. I will post more code and details.
    – user11933
    Aug 21, 2015 at 9:30
  • This only seems to work - define "work". It hangs? Only 64 bytes are transmitted? If so, which? The first 64? The last 64?
    – Nick Gammon
    Aug 21, 2015 at 9:36
  • 1
    I(t) would be a snippet if ... - OK, let's say you turned interrupts off. You may have, you may not have. With them off things would be different than if they were on. It may not seem important to you, but to us, it is. You are the one asking the question. It is up to you to supply as much information as you can to get a good answer.
    – Nick Gammon
    Aug 21, 2015 at 9:44

1 Answer 1


Here's the problem:

sendEncoderData() is called by an interrupt service routine.

There is an internal 64 byte serial transmit buffer that is filled by Serial.write(). That buffer is empted by sending the data a byte at a time using an interrupt.

If you are in an interrupt (at a higher priority than the serial interrupt) then the interrupt that sends the data can never happen, so the buffer just fills up and stops when it gets to 64 bytes. It will hang until there is room for more bytes in the buffer, and that will never happen because you're not allowing the data to be sent.

The answer (as I and others have said many times in the past): Never use Serial in an interrupt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.