20

I'm automating a configuration process for an embedded board. To enter the setup screen I need to send "Ctrl-C" command.

This is NOT to interrupt a process I'm running locally, KeyboardInterrupt will not work. I need to send a value that will be interpreted by the bootloader as Ctrl-C.

What is the value I need to send?

Thank you

5 Answers 5

29

IIRC, Ctrl-C is etx. Thus send \x03.

Sign up to request clarification or add additional context in comments.

1 Comment

See also, the wikipedia pages on ASCII and ETX.
11

You should send a character with the ASCII code 3:

serial.write('\x03')

Comments

5
\x03

Which means 'end of text' or 'break' is what Ctrl+C sends.

1 Comment

any idea how to use it in a python3 script?
4

Python doesn't take the ASCII code as a string, it needs to be encoded as bytes. So just add b before the code.

serial.write(b'\x03')

I've used here and saved and saved my life a couple times.

Comments

0
serial.write(b'\x03')

This may not work (or not work all the time) as it depends on the default encoding. If this is NOT utf-8 then pyserial will complain with the message :

TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))

To avoid this you have to encode your string as utf-8, so to send an escape sequence (e.g. ESC [ z ) you can do:

ESC_CHAR = chr(27)
text=f"{ESC_CHAR}[z".encode("utf-8"
serial.write(text)

You can of course compress this to one line or a variable for convenience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.