Remove and Replace Printed characters in serial terminal

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Remove and Replace Printed characters in serial terminal

Post by palivoda » Sat Feb 15, 2020 9:33 am

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

User avatar
jimmo
Posts: 1440
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Remove and Replace Printed characters in serial terminal

Post by jimmo » Sat Feb 15, 2020 9:50 am

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?

palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Re: Remove and Replace Printed characters in serial terminal

Post by palivoda » Sat Feb 15, 2020 1:03 pm

I use "mpfshell -n -c 'open ttyUSB0;repl"

User avatar
jimmo
Posts: 1440
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Remove and Replace Printed characters in serial terminal

Post by jimmo » Sat Feb 15, 2020 1:14 pm

Can you try using screen?

screen /dev/ttyUSB0 115200

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

cgglzpy
Posts: 33
Joined: Thu Jul 18, 2019 4:20 pm

Re: Remove and Replace Printed characters in serial terminal

Post by cgglzpy » Sat Feb 15, 2020 5:49 pm

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

palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Re: Remove and Replace Printed characters in serial terminal

Post by palivoda » Sun Feb 16, 2020 4:34 pm

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

Post Reply