Page 1 of 1

Remove and Replace Printed characters in serial terminal

Posted: Sat Feb 15, 2020 9:33 am
by palivoda
I'm looking for a way to replace printed text in serial.
Does MicroPython allows to send special characters?

In Python its:
sys.stdout.write("\033[F") # Cursor up one line
sys.stdout.write("\033[K") # Clear to the end of line

Re: Remove and Replace Printed characters in serial terminal

Posted: Sat Feb 15, 2020 9:50 am
by jimmo
It should be the same in MicroPython. What you're actually doing here is sending ANSI escape codes -- \033 puts the escape byte (i.e. it's octal for 27).

What's probably going wrong here is that whatever terminal you're using to view the output doesn't understand the escape sequences. What are you using to access the serial console?

Re: Remove and Replace Printed characters in serial terminal

Posted: Sat Feb 15, 2020 1:03 pm
by palivoda
I use "mpfshell -n -c 'open ttyUSB0;repl"

Re: Remove and Replace Printed characters in serial terminal

Posted: Sat Feb 15, 2020 1:14 pm
by jimmo
Can you try using screen?

screen /dev/ttyUSB0 115200

(If you're new to screen, use "Ctrl-A k y" to quit)

Re: Remove and Replace Printed characters in serial terminal

Posted: Sat Feb 15, 2020 5:49 pm
by cgglzpy
I don't know if this is the same, but using '\r' and "print(msg, end=' ')" should work. e.g.

Code: Select all

>> def print_and_clear(msg):
...     msg_list = ['This', 'is', 'a', 'message', 'that', 'replaces', 'itself']
...     print(msg, end='')
...     time.sleep(1)
...     print('\r'+' '*len(msg), end='\r') # This clears previous line
...     for ms in msg_list:
...         print(ms, end='')
...         time.sleep(1)
...         print('\r'+' '*len(ms), end='\r') # This clears previous line

Re: Remove and Replace Printed characters in serial terminal

Posted: Sun Feb 16, 2020 4:34 pm
by palivoda
Thank you for help, the carrige return "\r" works indeed.

I also found that mpfshell supports:
print("\033[XA"); // Move up X lines;
print("\033[XB"); // Move down X lines;
print("\033[XC"); // Move right X column;
print("\033[XD"); // Move left X column;
print("\033[2J"); // Clear screen