Serial comms is always more difficult than people think once you take the possibility of non-ideal circumstances happening - like your TERMINAL_MODE_PROMPT_SHELL character being corrupted in transmission. probably resulting in a permanent hang in your code as written!
I've spent days working on this now.
The problem:
I send a command over serial to a
UWB module.
The serial output should contain either ": ok" or ": failed".
But often the serial output just echoes part of the command which was sent and then everything hangs.
I then noticed that any command which contains a space or any command more than 6 characters in total length fails and hangs.
I suspected a character encoding issue, where my text command converted to bytes which are written to the serial port and not correctly encoded or decoded.
This was not the problem.
I then suspected my OutputStream which writes the bytes to the serial port was not flushing all bytes to the serial port, so only part of a command was being sent to the UWB device.
This was not the problem.
I then suspected an underlying problem with the serial port library I'm using - this library is built into the custom android device's firmware, I have no documentation for it.
This was not the problem.
By now I'd googled endlessly for help, but then found this page:
https://forum.qorvo.com/t/issue-with-uart-shell-mode/3696
I am unable to send any commands with a space to the DWM1001
And the response:
shell is intended to be used with human and not with software. So in some previous thread is recommended to add cca 10ms delay between characters.
That is: introduce a small delay between sending every byte of the command.
The text serial mode of the UWB module is intended to handle human input, not streams of many bytes sent (rapidly) by code.
So I updated my code to send a byte, wait 10 millis then send next byte etc.
Success all of my commands now work as expected.