I'm trying to connect both Serial and Serial1 to the computer in order to use Serial1 to transfer data to and from the computer continuously and use Serial to upload the sketch and then print calculation results to the Serial Monitor. I have a Arduino Mega 2560 and a Arduino USB2Serial Micro adapter. Is it possible to transfer continuously through Serial1 without any interruption from Serial when it is required to print to the Serial Monitor?
Is it possible to transfer continuously through Serial1 without any interruption from Serial when it is required to print to the Serial Monitor?
Can you stir pasta sauce and roll out bread at the same time? Of course not. The Arduino can't do two things at the same time, either. It can send data out the Serial port OR it can send data out the Serial1 port OR it can be doing something else, at any given point in time.
The key is to make sure that the Arduino has time to send data out both ports at the speed need.
Serial data is NOT sent continuously. Serial data consists of bytes that are discrete blobs of information, and are sent one at a time.
To follow what @PaulS has said, if you organize your program properly it will appear to use both Serial ports simultaneously because the Arduino is very much faster than even a fast serial data rate.
These links may be useful
Several Things at a Time
Serial Input Basics
Planning and Implementing a Program
...R
Thank you to both of you and particularly thank you for the links to follow
The Arduino can't do two things at the same time, either. It can send data out the Serial port OR it can send data out the Serial1 port OR it can be doing something else, at any given point in time.
This isn't true. The UARTs in a microcontroller can be thought of as tiny little special-purpose execution units, and they ARE capable of transmitting and receiving simultaneously. All 4 of them. As well as doing something else on the main CPU (and having other peripherals do their respective tiny little things.)
Can you stir pasta sauce and roll out bread at the same time?
Like having the sauce on the stove and the bread in the oven at the same time. No problem!
There are limits, of course. Once one of the serial ports has received or transmitted 2 bytes, it need the CPU to read the two received bytes (or it will lose the data) or provide additional data to transmit (or it will have pauses.) (Serial is slow. In the time that it takes the uart to send two bytes at 9600bps, the main CPU can execute 10000+ other instructions.) The Arduino already has "invisible" internal software that will do this part without you having to pay attention, up to (usually) 64 bytes in each direction. If you try to output more than 64bytes in a Serial.print() statement, or do consecutive prints that total more than 64 bytes in less time than it actually takes to send that data, then your sketch will stop there ("block") and wait for some output to occur.
So you have to be a bit careful (or a lot careful, depending on the exact circumstances), and of course there are things that are flat-out impossible (moving data continuously from a fast port to a slow port, for example.) But "Yes, you can use multiple serial ports on an Arduino Mega simultaneously" is a more accurate answer than "no, you can't."
Like having the sauce on the stove and the bread in the oven at the same time. No problem!
You are going to roll out the bread while it is in the oven?
westfw:
There are limits, of course. Once one of the serial ports has received or transmitted 2 bytes, it need the CPU to read the two received bytes (or it will lose the data) or provide additional data to transmit (or it will have pauses.) (Serial is slow. In the time that it takes the uart to send two bytes at 9600bps, the main CPU can execute 10000+ other instructions.) The Arduino already has "invisible" internal software that will do this part without you having to pay attention, up to (usually) 64 bytes in each direction. If you try to output more than 64bytes in a Serial.print() statement, or do consecutive prints that total more than 64 bytes in less time than it actually takes to send that data, then your sketch will stop there ("block") and wait for some output to occur.
Where can I read more about the sketch stopping at 64 bytes? And how can you work around this if you're sending a really long string?
adazol:
Where can I read more about the sketch stopping at 64 bytes? And how can you work around this if you're sending a really long string?
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
You can receive as many bytes as you want to.
...R
A better way to xfer data to and from the computer, without interfering with Serial and debugging at all would be to connect Serail1 to a HC-05 module and use the bluetooth on your laptop or PC if you have it.
You can then use RealTerm for example to xfer data over the bluetooth com connection between the arduino and the computer.
Where can I read more about the sketch stopping at 64 bytes? And how can you work around this if you're sending a really long string?
Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation.