I have an issue where I am trying to perform a number of functionality tests on some devices. My “test jig” communicates with the DUT (Device Under Test) via UART, using the scanf function.
Assuming the devices work as intended, there are no issues. However, one part of the test puts the DUT into sleep mode and my jig wakes it back up. If the DUT has a hardware problem and it doesn’t wake out of sleep, then my program hangs when performing the scanf, as it just sits and waits to receive data that isn’t coming.
I have tried to do an if(DUT.readable()), but as it retains info from the previous string that was sent, it sees the device as readable (this is what I believe is happening at least).
I have tried to clear the buffer using the following that I found online:
while (DUT.readable()) {
DUT.getc();
}
but this still doesn’t work and it still attempts to read a string that will never be coming. As far as I can see, there is no way to do a timeout on the scanf, so I wanted to do a check to see if anything was available, then skip if not, but I cannot seem to clear the buffer properly. The incoming string ends with \r\n if this is useful information.
Does anyone have a way to reliably clear this buffer, or perhaps any other solutions?
For reference, my microcontroller is the STM32F091RC, and I am using the online compiler