Better/faster way to read from Serial1 in Arduino Lonardo?

Hello all,

I would be interested on knowing the faster or better way to read the reply to a command that I send towards one of my serial interfaces (Serial1 in Lonardo platform). So far I have been using as beneath, is there any other better way to read the output from that Serial1?

Thanks in advance,

regards

Serial1.print(dataToBeTx);
Serial.print(text + dataToBeTx);
delay(4000);
while (Serial1.available()) Serial.write(Serial1.read());

It amazes me the number of people that want faster code and have delay()s in it. It would be faster without the delay().

Hi PaulS,

I set the delay in order to see what was coming from the ouput of Serial1, otherwise if I don't set it and I do asign some variable to the output of such a serial is empty...

ndarkness:
Hi PaulS,

I set the delay in order to see what was coming from the ouput of Serial1, otherwise if I don't set it and I do asign some variable to the output of such a serial is empty...

So, you are stuffing your head in the sand and pretending that asynchronous communication is synchronous if you just wait long enough.

Hmmm...

PaulS:
So, you are stuffing your head in the sand and pretending that asynchronous communication is synchronous if you just wait long enough.

Hmmm...

So I should implement some sort of interruption mechanism? Do you mean that?

Do you mean that?

No. It appears that you are sending something to a device, via the serial port, and then waiting around for a response, though it is hard to tell just from a snippet and your comments.

Serial data is meant to be sent and forgotten. Arriving serial data is meant to be read and handled without any context. That's what asynchronous means.

If you are expecting serial data that arrives to be in response to some specific data sent, that is synchronous communication.

You have three choices. One, you can keep track of what you have sent, and when a response arrives, pair the response up to the oldest command sent, to determine if the reply was appropriate/what to send next.

Or, you can twiddle your thumbs until a (complete) response has been received, and know that it is in response to the last data/command sent.

Or, you can stick your head in the sand for some period of time, and then hope that:

  1. there is a response
  2. it is complete
  3. it is in response to the last data/command sent.

The choices listed are in order of speed. Send and forget is fast. Send, and wait just long enough for a (complete) response is slow, but not nearly as slow as just sticking your head in the sand for a fixed internal and then hoping that a (complete) response has arrived.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data without blocking.

...R

PaulS:
Serial data is meant to be sent and forgotten. Arriving serial data is meant to be read and handled without any context. That's what asynchronous means.

If you are expecting serial data that arrives to be in response to some specific data sent, that is synchronous communication.

Err...No(!)

Synchronous / Asynchronous refer to the low level transmission of bits and bytes (a little like the data link layer in the OSI model). In synchronous transmission a clock line physically joins the clocks at the near and far end. With the clocks tied together, time can be inferred as a constant at both ends. With asynchronous transmission, the near and far end clocks are not joined, hence time is not a constant at both ends.

Context is imposed higher up, when you have transmitted enough bits and bytes to form messages. The Hayes AT protocol, would be an example of a protocol which uses context and may be sent using asynchronous or synchronous transmission.

Apart from that, I agree with you :smiley:

Thanks PaulS, Robin2

Indeed I am sending a command to my device through the serial port, thus I would like to know when it has issued the data, since in that case it returns certain value that I would like to capture to check that data was sent properly. Thus I was implementing something similar to what Robin2 has in his examples, I will take a better look into them.

Thanks again.

Synchronous / Asynchronous refer to the low level transmission of bits and bytes

That's one definition for Synchronous / Asynchronous.