When is a serialport finished?

I’m trying to write a serial driver for an ATmega328P.

I can make it work. I give it a TAG I check that while it reads and a timeout so it does not continue forever.

The question is, what if I do not know what the response (TAG) should be? When is the message received?

Here is some of my code.

Capture

2

Here is an example of a device I communicate with (ESP-01)

3

I can see the TX line are hold high, when it reads. Is the solution to check when it goes low and then the message is received?

The stream of data incoming and outgoing are not linked together. They share the same baudrate and bit settings, but that’s all.

Both TX and RX signals are idle high. The TX level should not drop just like that.
If you send 0x00, then it will be low for a short time, but it must get high after the start-bit and the 8 data-bits, because the stop-bit is high.
The TX is high during receiving because that is what you do in your code.

The hardware of a Arduino Uno (ATmega328P) has only a one byte buffer. If you wait to read that UDR0 register, then you can not do other things.

The Arduino Serial library uses interrupts to receive and to send data and has buffers. That is how it is supposed to work.

To know when data starts or stops, a protocol is required. Probably also a certain timeout has to be specified. That can be made with the Arduino Serial library.

When is the message received?

at least in UNIX, the default mode of operations is for a read of a string from for example a keyboard is to return data after receiving the line feed.

of course there are settings to return a character at a time or return more than one character after a specified pause where there is no received data

When the line goes Active and stays that way it’s called a ‘break’. The receiving side detects a ‘break’ when the line is still Active when it should be Idle: during the ‘stop’ bit.

From the datasheet:

19.7.5 Parity Checker. The USART Receiver has three Error Flags: Frame Error (FEn), Data OverRun (DORn) and Parity Error (UPEn). All can be accessed by reading UCSRnA. Common for the Error Flags is that they are located in the receive buffer together with the frame for which they indicate the error status. Due to the buffering of the Error Flags, the UCSRnA must be read before the receive buffer (UDRn), since reading the UDRn I/O location changes the buffer read location.

So, before you read UDR0, read UCSR0A and look for the ‘FE0’ bit (Frame Error). You can also check for the DOR0 bit to see if you lost any characters because you are reading them slower than they are arriving and the UPE0 bit to see if the character had a parity error.

1 Like