I need non blocking stdio character reading through USB-Uart0 port on esp32 development board.
As I understand there is NO non-blocking stdio reading available.
So I would like to disable REPL from USB-Uart0 port and use the same port with standard Uart functionality including non-blocking read.
To disable REPL.
Have tried redirecting uos.dupterm(None, 1), throws ValueError: invalid dupterm index.
Can anyone suggest a solution.
Thanks
Trying to disable REPL from Uart0 on ESP32
Re: Trying to disable REPL from Uart0 on ESP32
Even if you can detach the REPL using dupterm, you won't be able to access the UART with the current firmware.
From machine_uart.c
I don't know the history of these "fun errors" unfortunately.
However, the feature you need (non-blocking stdio read) might be a very simple change:
https://github.com/micropython/micropython/issues/4849
implemented for ESP8266 in https://github.com/micropython/micropython/pull/4859
Maybe ESP32 is very simple to implement?
From machine_uart.c
Code: Select all
// Attempts to use UART0 from Python has resulted in all sorts of fun errors.
// FIXME: UART0 is disabled for now.
if (uart_num == UART_NUM_0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) is disabled (dedicated to REPL)", uart_num));
}
However, the feature you need (non-blocking stdio read) might be a very simple change:
https://github.com/micropython/micropython/issues/4849
implemented for ESP8266 in https://github.com/micropython/micropython/pull/4859
Maybe ESP32 is very simple to implement?
Re: Trying to disable REPL from Uart0 on ESP32
Actually, ESP32 was already implemented a few days later. So if you're running the latest nightly build (e.g. esp32-20190916-v1.11-312-g22099ab88.bin) then you should be able to do this.
I used
as an equivalent of "any()".
According to that PR, you should be able to use sys.stdin.buffer.read(1) as the non-blocking version of sys.stdin.read(1) but this didn't work for me.
You can also use the select.poll API. Weirdly the class is called "poll" not "Poll"?
I used
Code: Select all
select.select([sys.stdin], [], [], 0)
According to that PR, you should be able to use sys.stdin.buffer.read(1) as the non-blocking version of sys.stdin.read(1) but this didn't work for me.
You can also use the select.poll API. Weirdly the class is called "poll" not "Poll"?
Code: Select all
p = select.poll()
p.register(sys.stdin)
p.poll(0) # returns empty list if no data ready
Re: Trying to disable REPL from Uart0 on ESP32
Thanks jimmo,
poll works for me, as well as code below.
poll works for me, as well as code below.
Code: Select all
import sys
import select
import time
while 1:
time.sleep(0.1)
while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
ch = sys.stdin.read(1)
print(ch)
print("prove it doesn't block")
-
- Posts: 61
- Joined: Fri Feb 15, 2019 9:22 am
Re: Trying to disable REPL from Uart0 on ESP32
I have also encountered similar problems, which can be solved in the following ways through exploration.
1. Directly define other UART ports to overwrite repl on gpio1 and gpio3, but it will cause complete failure of repl, including ctrl-c. if the automatic operation of main.py is set, the board cannot be connected again.
uart = machine.UART(1, tx=1, rx=3 ,baudrate=115200)
2. By setting a main.py identification to restrict the operation of the application program, it can be realized that when the specific pin is pulled down, the automatic running program will not be started to avoid the loss of repl forever.
main.py
from machine import Pin
key_1=Pin(27,Pin.IN,Pin.PULL_UP)
key_2=Pin(25,Pin.IN,Pin.PULL_UP)
key_3=Pin(32,Pin.IN,Pin.PULL_UP)
print("if pin 25,27.32 any pin is low , exit app")
if key_1.value()==1 and key_2.value()==1 and key_3.value()==1:
print("Start App.....")
exec(open('./app.py').read(),globals())
1. Directly define other UART ports to overwrite repl on gpio1 and gpio3, but it will cause complete failure of repl, including ctrl-c. if the automatic operation of main.py is set, the board cannot be connected again.
uart = machine.UART(1, tx=1, rx=3 ,baudrate=115200)
2. By setting a main.py identification to restrict the operation of the application program, it can be realized that when the specific pin is pulled down, the automatic running program will not be started to avoid the loss of repl forever.
main.py
from machine import Pin
key_1=Pin(27,Pin.IN,Pin.PULL_UP)
key_2=Pin(25,Pin.IN,Pin.PULL_UP)
key_3=Pin(32,Pin.IN,Pin.PULL_UP)
print("if pin 25,27.32 any pin is low , exit app")
if key_1.value()==1 and key_2.value()==1 and key_3.value()==1:
print("Start App.....")
exec(open('./app.py').read(),globals())