I'm playing ESP32 board.It has a on-board USB-COM port which is used by REPL.
My application is designed to use UART communication.
But I would prefer to use REPL port, so I don't need to wire other Pins to use UART.Just use the REPL port like a normal UART?
I don't know how to get data from REPL port in my program.
Thanks for any help!
how to get data through REPL port in program?
Re: how to get data through REPL port in program?
You can use sys.stdin.read() or sys.stdin.readinto() or sys.stdin.readline() to get data from the REPL port. A few aspects to consider:
- the sys.stdin.read...() calls are blocking.
- The stream is not fully transparent. Ctrl-C must be disabled using micropython.kbd_intr(-1), otherwise a Ctrl-C on the input will stop your code. Using the sys.stdin.buffer.ŕead..() calls may be preferred.
sys.stdout.write() or sys.stdout.buffer.write() can be used to write.
- the sys.stdin.read...() calls are blocking.
- The stream is not fully transparent. Ctrl-C must be disabled using micropython.kbd_intr(-1), otherwise a Ctrl-C on the input will stop your code. Using the sys.stdin.buffer.ŕead..() calls may be preferred.
sys.stdout.write() or sys.stdout.buffer.write() can be used to write.
Re: how to get data through REPL port in program?
Thanks,it works.
I have a new question.
I create a thread dedicated to sys.stdin.read(),without set micropython.kbd_intr(-1).I find that CTRL+C (or 0x03) cann't stop the code.
If I play sys.stdin.read() not in a thread, CTRL+C is working.
It seems that CTRL+C is only working in MAIN thread(or UI thread as in other languages).Is this true?
I have a new question.
I create a thread dedicated to sys.stdin.read(),without set micropython.kbd_intr(-1).I find that CTRL+C (or 0x03) cann't stop the code.
If I play sys.stdin.read() not in a thread, CTRL+C is working.
It seems that CTRL+C is only working in MAIN thread(or UI thread as in other languages).Is this true?
Re: how to get data through REPL port in program?
That seems to be the case, whether intentional or not.t seems that CTRL+C is only working in MAIN thread(or UI thread as in other languages).
Re: how to get data through REPL port in program?
Hi,
I was about to ask the same question re serial comms using usb, so thank you to Roberthh for your answer.
@mmpy1st: Would it be possible to see an example of how to run this code in a thread?
Also, is it possible to generate an interrupt when serial data is received over usb?
Sorry if these seem simple questions, but my knowledge of MicroPython is limited at the moment.
I was about to ask the same question re serial comms using usb, so thank you to Roberthh for your answer.
@mmpy1st: Would it be possible to see an example of how to run this code in a thread?
Also, is it possible to generate an interrupt when serial data is received over usb?
Sorry if these seem simple questions, but my knowledge of MicroPython is limited at the moment.
Re: how to get data through REPL port in program?
code example demo
def recv():
while True:
rx = sys.stdin.readline()
....
t1 = _thread.start_new_thread(recv,0)
So thread t1 is not main thread,so it won't repond to CTRL+C interruption.
It is a little bit confused to understand how uart data be handle when they go into the chip. My thinking is that micropython enable the UART communication and received any data passed in. APP is working over micropython,and it can only get the data from micropython.
If micropython finds that a data belongs to it(like CTRL+C),it won't pass it to APP,instead it will execute itself.
And micropython only terminated main thread on CTRL+C.
I'm not sure whether I'm right.
def recv():
while True:
rx = sys.stdin.readline()
....
t1 = _thread.start_new_thread(recv,0)
So thread t1 is not main thread,so it won't repond to CTRL+C interruption.
It is a little bit confused to understand how uart data be handle when they go into the chip. My thinking is that micropython enable the UART communication and received any data passed in. APP is working over micropython,and it can only get the data from micropython.
If micropython finds that a data belongs to it(like CTRL+C),it won't pass it to APP,instead it will execute itself.
And micropython only terminated main thread on CTRL+C.
I'm not sure whether I'm right.