Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

how to ctrl-D from python?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

how to ctrl-D from python?

by bludin on Tue Jul 07, 2020 1:55 pm

not really a CP question but I want to control via USB some devices running CP from a python program running on a PC. This works perfectly except for the case when the device is 'caught' in the REPL. How do I perform/send the equivalent of ctrl-D/EOF from python (using PySerial). write(b'\x04') doesn't do the job. Seems like it should be easy but somehow...

bludin
 
Posts: 23
Joined: Thu Apr 16, 2020 8:57 am

Re: how to ctrl-D from python?

by tannewt on Tue Jul 07, 2020 2:25 pm

What is the device doing when you send it? Do you need to CTRL-C it first?

tannewt
 
Posts: 1716
Joined: Thu Oct 06, 2016 8:48 pm

Re: how to ctrl-D from python?

by bludin on Tue Jul 07, 2020 2:39 pm

it's just idling showing a happy >>> prompt and waiting to be ctrl-D'ed

maybe to make to more clear: I connect to the CP device using a serial terminal program (e.g. Putty). When I am at the >>> prompt and close the connection/terminal session and then re-open it, it's still in the same state. Now let's say I left it like that and the PC python program connect to it and wants it run its code.py. How do I get it to soft reboot via serial commands? (i.e. without power-cycling, hitting the reset button, or writing to the file system)

bludin
 
Posts: 23
Joined: Thu Apr 16, 2020 8:57 am

Re: how to ctrl-D from python?

by aivarannamaa on Wed Jul 08, 2020 9:52 am

write(b'\x04') is supposed to work, I've done it myself with PySerial...

What happen when you write(b"2+3\r\n")? Can you read the response?

aivarannamaa
 
Posts: 11
Joined: Tue Jan 30, 2018 8:30 am

Re: how to ctrl-D from python?

by bludin on Wed Jul 08, 2020 2:50 pm

Yes, I can read the response, no problem.
I have Win10 on one and and an itsybitsy M4 on the other.
What combination did you succeed on?

bludin
 
Posts: 23
Joined: Thu Apr 16, 2020 8:57 am

Re: how to ctrl-D from python?

by tannewt on Wed Jul 08, 2020 2:58 pm

`supervisor.reload()` might work. https://circuitpython.readthedocs.io/en ... sor.reload I don't think I've tried it from the REPL.

tannewt
 
Posts: 1716
Joined: Thu Oct 06, 2016 8:48 pm

Re: how to ctrl-D from python?

by bludin on Wed Jul 08, 2020 3:24 pm

thx, but it doesn't work from the REPL. Neither does
Code: Select all | TOGGLE FULL SIZE
exit()
, btw
I also read somewhere that ctrl-Z/return (i.e. b'\x1A\r\n) should "emulate" ctrl-D under Windows but it doesn't do the job in my case, either

bludin
 
Posts: 23
Joined: Thu Apr 16, 2020 8:57 am

Re: how to ctrl-D from python?

by aivarannamaa on Thu Jul 09, 2020 1:56 am

@bludin, I don't have a M4 device, but following works for me with Feather M0 Express, Windows 10 and CP 5.3.0:

Code: Select all | TOGGLE FULL SIZE
import serial

ser = serial.Serial('COM5', 115200)

ser.write(b"\x03") # Ctrl+C
ser.write(b"\x02") # Ctrl+B to ensure a prompt to be read
banner = ser.read_until(b">>> ")
print("got banner:", banner)

ser.write(b"\x04") # Ctrl+D

while True:
    print(ser.readline())

aivarannamaa
 
Posts: 11
Joined: Tue Jan 30, 2018 8:30 am

Re: how to ctrl-D from python?

by aivarannamaa on Thu Jul 09, 2020 2:03 am

Something to consider -- Ctrl+D only works if there is nothing else written on the prompt.

aivarannamaa
 
Posts: 11
Joined: Tue Jan 30, 2018 8:30 am

Re: how to ctrl-D from python?

by bludin on Thu Jul 09, 2020 6:21 am

aivarannamaa wrote:Something to consider -- Ctrl+D only works if there is nothing else written on the prompt.


That was it :D
Thanks a lot.

bludin
 
Posts: 23
Joined: Thu Apr 16, 2020 8:57 am

Re: how to ctrl-D from python?

by kevinjwalters on Tue Jul 14, 2020 7:42 am

May be worth considering use of something like https://pypi.org/project/pexpect/ , a descendant of Expect in Tcl, depending on what you're up to.

kevinjwalters
 
Posts: 662
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.