I have a system where I am doing some lab sample tests with the help of Arduino-based hardware. I define tests (parameters) with the help of a C# based application and pass data through serial communication. Arduino, send the test results back to pc after the test through serial communication. One test information is 2 data. [test_id, test_duration]. Arduino sends back two pieces of information [test_id, result].
But I want to define new tests even one test is going on and put them in the queue. Ardunio should able to fetch new test data (serially) when the active test is over and sending the result back to pc.
Can Arduino handle this kind of continuous two-way serial communication?
How should I do it. ?
The Arduino can do that, if you use hardware serial ports.
The data transfer to the computer is the fastest if the Arduino board does not have a separate USB-serial chip, but has the USB-serial in the microcontroller itself (Arduino Leonardo, Arduino MKR boards with SAMD processor, and more).
The Serial library has buffers of 64 bytes for receiving and transmitting.
If you have, for example, a long calculation in the sketch, then meanwhile data is received and transmitted in interrupts with those buffers.
How big should the queue be ? If you need a lot of RAM, then you should look for a board with more memory.
In the sketch, grab data as soon as it arrives and put it in a buffer.
@Koepel
some of my tests can take up to 15 minutes. Also, Arduino should send the result back to the PC. So if a test is running, and the user tries to send new data, don't you think that there will be data loss? or how do I inform the pc that Arduino is busy with a test?
With millis() you can do everything at the same time, independent of each other.
There are other options:
The Schedular for the SAMD processors works very well. You have to be careful how to assign enough stack space for each task and you have to keep in mind when using common variables or devices that others tasks could use them as well.
The ESP32 runs FreeRTOS, that is a full multitasking environment.
Using millis() requires a certain way of thinking. A existing sketch needs often to be rewritten all over. Andreas Spiess says it is like wearing your shirt inside out: