Does a serial device know when it is being connected to or read from?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Does a serial device know when it is being connected to or read from?

Post by aivarannamaa » Thu Oct 31, 2019 7:40 am

I'm trying to get better understanding of serial communication. I think I understand the basics (https://learn.sparkfun.com/tutorials/se ... cation/all), but I haven't found an explanation about what happens when a program connects to a device.

When I'm connecting to some MicroPython devices with Pyserial and start reading (without writing anything), I get some garbage and then comes the REPL banner and prompt. When I disconnect and connect again, I get the same garbage again. Does the device know I'm reading or does Pyserial send some handshaking/initialization data when I'm connecting?

The garbage happens also when I'm connecting with Putty, but there is less of it. Does it mean Putty is connecting differently or it tries to hide the garbage? There are some screenshots and more details here: https://github.com/thonny/thonny/issues ... -547652188
Aivar Annamaa
https://thonny.org

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Does a serial device know when it is being connected to or read from?

Post by pythoncoder » Thu Oct 31, 2019 8:40 am

It depends on the physical connection. A USB link does "know" if a connection exists. A UART link may or may not "know" depending on the electrical characteristics of the interface. A UART transmitter adopts a high state when idle. If the receiver line is pulled up the receiver has no way of detecting a connected transmitter until the transmitter changes the state of the line. On the other hand if the receiver line is pulled down it can in principle detect a connection: the receiver will see a "break condition" until the transmitter connects.

A USB connected MicroPython device draws its power from the interface so when you initially make the USB connection the device starts running. As you've discovered, vendor code on ESP8266 devices issues a burst of data at 75Kbps before MicroPython starts. Then the baudrate changes to 115K and the REPL appears. Hence the "garbage" encountered.
Peter Hinch
Index to my micropython libraries.

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Does a serial device know when it is being connected to or read from?

Post by aivarannamaa » Thu Oct 31, 2019 8:48 am

Thank you for the reponse!
pythoncoder wrote:
Thu Oct 31, 2019 8:40 am
A USB connected MicroPython device draws its power from the interface so when you initially make the USB connection the device starts running.
The same garbage happens when I keep the cable connected and only disconnect and reconnect the software. Does this mean the device uses extra logic to "greet" the new connection?
Aivar Annamaa
https://thonny.org

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

Re: Does a serial device know when it is being connected to or read from?

Post by Roberthh » Thu Oct 31, 2019 10:08 am

There is no "greeting". The ESP8266 sends when reset a short boot log ata baud rate of 74880 baud. Reading that at 115200 baud looks like garbage. Putty may treat that garbage different from Tonny. On my ESP8266, the boot text is as below. Nothing exiting.

Code: Select all

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 31084, room 16
tail 12
chksum 0x5a
ho 0 tail 12 room 4
load 0x3ffe8000, len 1024, room 12
tail 4
chksum 0xb0
load 0x3ffe8400, len 824, room 4
tail 4
chksum 0x49
csum 0x49
rf cal sector: 1019
freq trace enable 0
rf[112] : 00
rf[113] : 00
rf[114] : 01

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: Does a serial device know when it is being connected to or read from?

Post by Christian Walther » Thu Oct 31, 2019 12:06 pm

What could be happening is that opening the device in pyserial does something with the hardware handshake lines that causes the microcontroller to reset, hence you get the boot output on every new connection. (I don’t know the details offhand about how to configure that in pyserial and how the usual reset circuitry on ESP8266 boards is wired.)

Post Reply