6

Does there exist (any user developed etc.) version of the Hardware UART serial library that has support for RTS/CTS based flow control? More interested in the Tx side (Arduino sends data, while the peripheral can back-pressure). The library should enable the Arduino to stop transmitting within 1 character of seeing a 'not ready' indication from the peripheral.

Thank you.

O.K.
  • 209
  • 1
  • 2
  • 8

3 Answers3

2

I am not aware of any such library. You could try to implement one yourself, and maybe share it with the community. :-)

If you take this route, you will have to copy the file HardwareSerial.cpp from the Arduino core, make your changes there, and have your version replace the one from the core. Here are some ideas that may help to get you started:

  • Wire the 'ready' signal from the device to an interrupt-capable pin of the Arduino.
  • Inside the interrupt handler:
    • when 'ready' is deasserted, disable the UDRE interrupt by issuing cbi(*_ucsrb, UDRIE0)
    • when ready is asserted, enable it again, but only if the output buffer is not empty: if (_tx_buffer_head != _tx_buffer_tail) sbi(*_ucsrb, UDRIE0);
  • Patch HardwareSerial::write() to enable the interrupt only if 'ready' is asserted.
  • Note that the UDRE IRQ can be explicitly called by HardwareSerial::flush() and by HardwareSerial::write() if interrupts are globally disabled. You will have to condition this on the 'ready' signal.
  • Note also that HardwareSerial::write() can write directly to the UART if the buffer is empty. This also has to be conditioned on the 'ready' signal.
Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
0

I believe that CTS would be fairly easy to implement:

void loop() {
  uint8_t clear_to_send = digitalRead(CTS);
  volatile char data;
  if (clear_to_send) {
    Serial.write(data);
  }
}

I'm not sure about RTS.

woo2
  • 101
  • 4
0

I have I discovered that RTS/CTS may be implemented in genuine Arduinos, but is not supported by Arduinos that use the CH340 USB to serial chip. I found this when my JAVA program could talk with a genuine Uno ok, but didn't send any data to several Arduino compatibles that use the CH340 chip (but did receive from them). I had RTS/CTS set on the JAVA program. When I changed it to FLOWCONTROL_NONE it started working just fine.