Arduino Forum

Using Arduino => Project Guidance => Topic started by: Fega on Mar 31, 2017, 09:33 pm

Title: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Fega on Mar 31, 2017, 09:33 pm
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:

https://github.com/fega/arduair-hardware/blob/master/arduair/arduair.ino (https://github.com/fega/arduair-hardware/blob/master/arduair/arduair.ino)

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

https://github.com/fega/winsen-ze03-arduino-library (https://github.com/fega/winsen-ze03-arduino-library)

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  :D  :D  ;D

Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: pert on Apr 01, 2017, 12:51 am
Your first link was messed up, it points to the library instead of the sketch. Here's the right link:
https://github.com/fega/arduair-hardware/blob/master/arduair/arduair.ino (https://github.com/fega/arduair-hardware/blob/master/arduair/arduair.ino)

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

I'd get each sensor working individually first before combining them.
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Fega on Apr 01, 2017, 01:24 am
Hello @pert, thanks, I corrected the link

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

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)
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Robin2 on Apr 01, 2017, 08:51 am
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 (http://forum.arduino.cc/index.php?topic=396450.0) - 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
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Fega on Apr 01, 2017, 10:38 pm
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
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Robin2 on Apr 02, 2017, 09:41 am
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
Code: [Select]
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
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Fega on Apr 02, 2017, 09:19 pm
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 :( ), 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
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Fega on Apr 03, 2017, 02:35 pm
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 :/

Now the code es simpler

_s->write(petition,sizeof(petition));
  delay(1500);
  // read
  if (_s->available() > 0) {
    _s->readBytes(measure,9);
  }
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Robin2 on Apr 03, 2017, 03:23 pm
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
Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Fega on Apr 05, 2017, 01:40 am
Hello Robin, yeah I still need help. I wrote this library:

https://github.com/fega/winsen-ze03-arduino-library (https://github.com/fega/winsen-ze03-arduino-library)
in this

https://github.com/fega/winsen-ze03-arduino-library/blob/master/examples/WinsenZE03_triple_example/WinsenZE03_triple_example.ino (https://github.com/fega/winsen-ze03-arduino-library/blob/master/examples/WinsenZE03_triple_example/WinsenZE03_triple_example.ino)

 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?

Title: Re: Multiple Serial Use, Problem in Arduino Mega an a large project
Post by: Robin2 on Apr 05, 2017, 10:13 am
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