Page 1 of 1

I don't need echo when using repl

Posted: Wed Jul 04, 2018 10:41 am
by Meekdai
Hi everyone ,

when I using REPL, I found that it has the function of echo.
This problem does not exist when I use secureCRT, but when I receive serial port data by Serial debugging assistant, echo is appeared.
I guess secureCRT should hide echo which I was send.

Is there any way to cancel it? :?: :?:

Re: I don't need echo when using repl

Posted: Thu Jul 05, 2018 9:35 am
by Damien
There's no way to turn off echo from the pyboard for the REPL because it's a fundamental part of how it works. Usually you will configure your serial terminal so it doesn't echo characters you type.

You might also like to try out raw REPL mode (ctrl-A at the prompt) which is a way to programmatically send code to the device.

Re: I don't need echo when using repl

Posted: Fri Jul 06, 2018 1:31 am
by Meekdai
Thank you Damien , I have a project that wants to communicate with the host computer through the REPL. But echo and ">>>" are not needed. I view the source code and made some modifications (pyexec.c readline.c) to implement my current functionality.
Before modification:
serial send:

Code: Select all

GetVersion()\r\n
serial receive:

Code: Select all

GetVersion()
REG08_V0.1
>>> 
After modification:
serial send:

Code: Select all

GetVersion()\r\n
serial receive:

Code: Select all

REG08_V0.1
I made a PC software to control the motion of the multiple stepper motors..
I know this should not be the best solution. Is there any other way to communicate between pyboard and PC software?

Re: I don't need echo when using repl

Posted: Fri Jul 06, 2018 3:59 am
by dhylands
If enter the raw REPL (see: http://docs.micropython.org/en/latest/p ... l#raw-mode) then the data which is sent to the pyboard is not echoed.

You could also use something like json-ipc which you can find over here:
https://github.com/dhylands/json-ipc

Re: I don't need echo when using repl

Posted: Fri Jul 06, 2018 6:35 am
by pythoncoder
Is there any other way to communicate between pyboard and PC software?
You might use a UART and an FTDI USB adaptor. That way you could use the REPL normally while having a separate channel for data.

Re: I don't need echo when using repl

Posted: Mon Jul 09, 2018 1:32 am
by Meekdai
dhylands wrote:
Fri Jul 06, 2018 3:59 am
If enter the raw REPL (see: http://docs.micropython.org/en/latest/p ... l#raw-mode) then the data which is sent to the pyboard is not echoed.

You could also use something like json-ipc which you can find over here:
https://github.com/dhylands/json-ipc
I rewrote a custom REPL with reference to raw REPL. It works well.
json-ipc is not what I want,Because I need the parse and compile function like REPL.
pythoncoder wrote:
Fri Jul 06, 2018 6:35 am
Is there any other way to communicate between pyboard and PC software?
You might use a UART and an FTDI USB adaptor. That way you could use the REPL normally while having a separate channel for data.
If using serial communication, I need to define the communication protocol myself. I can rely on it completely through REPL.
Is there a similar REPL ready-to-use protocol that can be used on UART?

Serial communications protocols

Posted: Mon Jul 09, 2018 7:54 am
by pythoncoder
I don't know your level of experience but designing communication protocols in Python is easy if your communications link is reliable. This is thanks to its object serialisation libraries ujson and pickle. Aim for a half-duplex protocol: one end is the master. The slave never sends unsolicited messages. Usually all that's needed is to exchange Python dictionaries. The master sends a master dictionary to the slave, which responds with a response dictionary. The following gives a glimpse of this. To try it on a Pyboard link pins X1 and X2:

Code: Select all

import ujson
u = pyb.UART(4, baudrate=9600)

d = {1:'one', 2:'two'}
line = ''.join((ujson.dumps(d), '\n'))
u.write(line)

l2 = u.readline()
drec = ujson.loads(l2)
print(drec)
The dictionaries can be arbitrarily complex without altering a line of code.

If your communications link is unreliable (e.g. over a radio link) then protocol design becomes decidedly involved, but this won't arise with an FTDI adaptor connected to the Pyboard with short wires.

Re: I don't need echo when using repl

Posted: Sun May 02, 2021 1:13 am
by toybuilder
As someone that's just recently started the climb up the learning curve on micropython and python, I can appreciate the original question of this topic, as I've spend some time looking into it myself and have landed here a few times during my Google searches.

The answer pythoncoder provides is the correct one -- if you want to read a line of text without echo, readline() will process incoming bytes until the new-line and then return that result in a string.

I think what may be a bit unclear for people new to the micropython world is the distinction between when you are interacting with the REPL and when you're interacting with (user-created) code running on the board. When the user's code is running (either through booting up the board and running main.py, or by calling a function from the REPL and thus running in the code), the input/outputs are with the code, and no longer involves the REPL until the program is exited (either through a keyboard interrupt or via a sys.exit() call) and control passes back to the REPL.

Related to this (at least for my own purposes), you might have a desire to read data from stdin when there is data, but not block while that is happening. The post at viewtopic.php?t=7325 is helpful -- but only talks about the case of reading one byte at a time with read(1). read(1) can be replaced with readline() for a blocking read to the newline once characters start arriving.