Unable to call flush from inside the library
Hello, I am using version 1.0.5 and trying to use the flush() function from inside the library to avoid any user induced loop delay so that I can know faster when transmission was complete. But the UART stops responding when I try implementing this way. I implemented a custom function to send the data as below
void NeoHWSerial::write_bytes(uint8_t *buffer,uint8_t datalen){ // actually writes //active_object = this; basically active_object was assigned "this" instance while initializing for(int i=0;i<datalen;i++){ dbgSerial.print(buffer[i],HEX); active_object->write(buffer[i]); } active_object->flush(); // calling flush here to block until transmission is complete
}
trying to use the flush() function from inside the library to avoid any user induced loop delay so that I can know faster when transmission was complete.
Yeah, don't do that.
Instead, upgrade to v1.6.5 and use availableForWrite. When that value is SERIAL_TX_BUFFER_SIZE-1, you know the last byte has been given to the UART. The last character will have gone out over the wire 10 bit times after that. Is that close enough?
If you really need to know when the last byte has been transmitted, you could wait one more character time (polling micros() in loop) or you could watch the UDRE register (UART Data Register Empty), either by polling (check the spec to make sure reading the register doesn't clear important flags or pending interrupts) or by adding code to the ISR.