how to get data through REPL port in program?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mmpy1st
Posts: 4
Joined: Sat Nov 13, 2021 6:07 am

how to get data through REPL port in program?

Post by mmpy1st » Sat Nov 13, 2021 6:20 am

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!

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: how to get data through REPL port in program?

Post by Roberthh » Sat Nov 13, 2021 7:16 am

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.

mmpy1st
Posts: 4
Joined: Sat Nov 13, 2021 6:07 am

Re: how to get data through REPL port in program?

Post by mmpy1st » Sun Nov 14, 2021 1:18 pm

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?

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: how to get data through REPL port in program?

Post by Roberthh » Sun Nov 14, 2021 1:30 pm

t seems that CTRL+C is only working in MAIN thread(or UI thread as in other languages).
That seems to be the case, whether intentional or not.

tenspot
Posts: 2
Joined: Sat Aug 27, 2016 10:11 am

Re: how to get data through REPL port in program?

Post by tenspot » Mon Nov 15, 2021 5:14 pm

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.

mmpy1st
Posts: 4
Joined: Sat Nov 13, 2021 6:07 am

Re: how to get data through REPL port in program?

Post by mmpy1st » Sun Nov 21, 2021 3:17 am

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.

mmpy1st
Posts: 4
Joined: Sat Nov 13, 2021 6:07 am

Re: how to get data through REPL port in program?

Post by mmpy1st » Sun Nov 21, 2021 3:19 am

Roberthh wrote:
Sun Nov 14, 2021 1:30 pm
t seems that CTRL+C is only working in MAIN thread(or UI thread as in other languages).
That seems to be the case, whether intentional or not.
Hey Roberthh, could u please check my last post and comment,thanks.

Post Reply