Multiple Serial Use, Problem in Arduino Mega an a large project

Hello!

I'm Working in a relatively large project in an Arduino Mega, it uses several Sensors, 3 of them through Serial.

When I try to measure it, the project fails, some of the others sensors show unexpected values or an "oxf" or "eDj".

The project

this is the project:

It uses a library that I created to read the Serial Sensors

What I tried

I tried changing the Hardware Serial for Software Serial but the problem get worse.

I tried diabling another function callings in the loop() function, but I doesn't work

I tried looking for the Available ram but I didn't get any problems with it. ( the minimum was 5900 b)

So, What could be happening?

thanks in advance :smiley: :smiley: ;D

Your first link was messed up, it points to the library instead of the sketch. Here's the right link:

What exactly do you mean by "the project fails"?

I'd get each sensor working individually first before combining them.

Hello @pert, thanks, I corrected the link

About the explanation, I will try to do it well :slight_smile:

So, the setup() works as expected
Then the loop() begins, it run well until

void loop() {
pmRead();
mq131Read();
meteorologyRead();
winsenRead(CO); <-------- Here
winsenRead(NO2);
winsenRead(SO2);
getDate(DS1307_ADDRESS);
tableWrite();
if(wifi){request();}
}

If I comment the next two lines, the program appear to run correctly

if not

After that, the loop fails. it gets the date correctly but doesn't save the data on the SD, if the wifi is enabled, the Arduino resets.
If not, the next loop will fail and throws strange values (negative or ovf or eDj)

I'm afraid I see a huge re-write coming. Your library is using Serial.readBytes() which is a blocking function that will hold everything up until it is satisfied.

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

You can easily have separate copies of my functions to read data from different serial ports.

Also note that it is essential to call those functions frequently so that they can remove the data from the serial input buffer before it can overflow. That almost certainly means that the way your main program calls the library will have to change also.

...R

Hello @Robin2, Well I don't really need to use non-blocking ways to receive data, except if it avoids the problem of overflow.

Also, the Serial port Only receives data when I Send a signal, after that, the sensor will not perform any kind of communication.

Do you think that handle it in a nonblocking way will solve the problems?

Thanks

Fega:
Also, the Serial port Only receives data when I Send a signal, after that, the sensor will not perform any kind of communication.

If you can be certain that data is only sent to the Arduino when you request it and if your program can then wait until it all arrives then a blocking system would work.

I had assumed that data would be arriving asynchronously at three different serial ports.


And I have just realized that your .INO program does not seem to use your library at all - I was wasting my time looking at the wrong thing. Please be clearer in future.

This looks very strange

if (Serial1.available() > 0) {
        Serial1.readBytes(measure,9);
        if (measure[0]==0xff && measure[1]==0x78){
          Serial1.readBytes(measure,9);
        }

and seems to me to have many ways to fail. What happens if 9 bytes are not sent? And you are trying to get 18 bytes (in 2 groups of 9) but you don't seem to do anything to request the second group.

My feeling is that you have let your program grow far too big before sorting out the problems you are now facing. At the very least I suggest you create separate functions to work with each sensor as that will allow you to test them separately with a lot of confidence that any time you call a function "it just works".

...R

Hello @Robin2
I'm very sorry for what happened
My sincere apologies, I didn't merge branch where I implemented the library.

This is basically the same code but clean and reusable, I'm really really grieved about it, is not my intention waste your time in a meaningless way.

About the program grow, I actually made a test for every single component and all of the functions were tested alone, I also save some of the code that I use in the same repository and all work as expected when is used isolated.

About the snippet of code, I really didn't remember why I did it in that way (yeah, I should comment it :frowning: ), I guess that the sensor re-sends my initial petition before the answer, but I'm not sure about it. but as I mentioned, it works when I tested it before.

I will rewrite the function from zero again, and then post the results

Well, I changed the code a little bit and solved a lot of issues in the measure, Now I'm sure that the Sensors work as expected,

But the program is still having overflow problems :confused:

Now the code es simpler

_s->write(petition,sizeof(petition));
delay(1500);
// read
if (_s->available() > 0) {
_s->readBytes(measure,9);
}

Fega:
Now the code es simpler

If you still need help with something you must post a complete program together with a description of what it actually does and what it should do.

And please put code inside code tags using the code button </>

...R

Hello Robin, yeah I still need help. I wrote this library:

in this

it works as expected, the problem comes when I try to use it with another devices, the program go crazy and began to work in unexpected ways

I still believe that is an OVF problem but I didn't get what is wrong with it

I solve it changing both

SERIAL_TX_BUFFER_SIZE to 8
SERIAL_RX_BUFFER_SIZE to 8

but what it means? it means that the problem is the SRAM? or could be another thing?

After my previous experience I am not going to take any chances. Please post your program in this Forum - as an attachment if it is too big to include directly.

...R