Pages: [1]   Go Down
Author Topic: Library programming and Serial  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody!

Hope this is the right forum to post my problem. If it's not, I apologize.

For a project of mine (for my Master's thesis, university rules forbid me from telling you any details...that sucks) I have to create a custom library.

I did that and everything worked fine. Then, without any reason (at least not any I noticed), the following happened:

The library code stopped working and so I fired up the Serial Monitor to see the output of my code (some debug messages to Serial) to check what went wrong. I noticed something odd. My code has to read from a Client instance, byte by byte until a newline character, then examine the line and continue until no more data is coming in. I am using a char[] to store the incoming bytes one by one (that might not be the smartest idea, I guess - any suggestions?). I tried to print the whole buffer to Serial after each byte read and found the data I expected. But then, some odd characters appeared on the screen, the execution seemed to freeze and the setup()-method was called and executed! That happened over and over again. As you can imagine, this is absolutely not what I want to do smiley-confuse

So here's my question: is it possible that either the usage of Serial.print[ln] or the usage of a char[] (I use 1024 bytes at once to buffer data) could interfere with Arduino's whole program? I mean...calling the setup()-method over and over again seems...just wrong. The funny thing is, that when I added some more Serial.printlns to my code, it started working again! That is just...odd...(Serial is using 9600 baud).

As I mentioned before, I can't give you the whole code, but here's a snippet from it, maybe it will help:

Code:
        //_line_length is 1024
        char buffer[_line_length];
       
        //Read the whole answer
        while((c = client.read()) != -1)
        {
            //Serial.print(c);
            buffer[offset] = c;
            offset++;
            //Serial.println(buffer);     

            //To keep the end-of-string all the time!       
            buffer[offset] = '\0';
                       
            //c contains the current byte read
            if(c == '\n' || offset == _line_length-1)
            {
                Serial.println("NEW LINE");
                //New line starts here (or buffer is full)
                     
                //Examine line
                //Extract status code
                if(startsWith(buffer, "HTTP/1.1 200")) { statuscode = 200; }
                if(startsWith(buffer, "HTTP/1.1 404")) { statuscode = 404; }
                if(startsWith(buffer, "HTTP/1.1 500")) { statuscode = 500; }
           
                //Reset buffer and offset
                // I tried using memset, but that seemed to make things worse
                //memset(buffer, '\0', sizeof(buffer));
               
                //TEST - did not work either
                //free(buffer);

                offset = 0;
            }   
        }

The above snippet should give you the gist of what I am doing. After each newline / when the buffer is full, I examine the line just read.

I suspect that my code might be overriding some bytes of the Arduino's own code, thus leading it into an unstable state, thus causing it to reset over and over again. But why can this be fixed by some additional Serial.printlns? I do not get it.

Has anyone ever noticed something like this in his/her own code and found a fix or workaround!? I don't dare to remove the Serial stuff from my code right now as it is finally working and I have some testing to do. But in the final version of the code, I want to remove the printing, so I need to know the root of my problem.

Any help is highly appreciated!


Greets and THX - steps
Logged

Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 887
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        //_line_length is 1024
        char buffer[_line_length];

That's half your memory right there.  The other code suggests you might be doing something with a WIZNET ethernet whose library is also pretty RAM hungry.  My bet is that you are clobbering your stack.  Try it with _line_length == 64.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Man, I feel so stupid right now!

Of course I am using most/all of the RAM available! I always thought something like "well, there are ~30 KB of memory, so let's just use 1 KB, that should work".  smiley-red

Thanks a lot for the hint, I will try to change the code and get back at you here.

Greets and THX (imagine this THX in a VERY huge and bold font, please smiley-razz) - steps

[edit]
Alright, I changed the size to 128 bytes (some lines I get are quite long, have to reduce them, I guess) and everything seems fine!

Thank you so much! I did not see the wood for the trees here, I guess smiley-grin
[/edit]
« Last Edit: August 19, 2011, 03:35:37 pm by Steps » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Man, I feel so stupid right now!

Of course I am using most/all of the RAM available! I always thought something like "well, there are ~30 KB of memory, so let's just use 1 KB, that should work".  smiley-red

Thanks a lot for the hint, I will try to change the code and get back at you here.

Greets and THX (imagine this THX in a VERY huge and bold font, please smiley-razz) - steps

Yes, the C/C++ language doesn't help make one very aware of the big difference of Harvard architecture micro-controllers, have separate program and data memory spaces and the big differences in their relative sizes.

Lefty
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7234
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you receive a message that is longer than 1024 bytes?
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you receive a message that is longer than 1024 bytes?

I don't think so, no. Guess the longest message I got was around 100 bytes. Why?

Greets - steps
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7234
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your array is 1024 long and you don't have a mechanism to check out of bound.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I included
Code:
if(c == '\n' || offset == _line_length-1)
to avoid out of bounds issues. But as I control the data sent to the Client, I won't have that problem smiley

Thanks anyway!

Greets - steps
Logged

Pages: [1]   Go Up
Jump to: