Go Down

Topic: Half a string becomes garbage chars for mysterious reasons... (Read 452 times) previous topic - next topic


Hi guys and girls

I'm currently busy with a project that receives phone calls, logs them, and sends reports to admin users via SMS. Admin users can also reset the values that are being logged via an SMS. I use a Seeeduino Stalker v2.3 and a GPRS Shield v1.4.

My problem is as follows:
Function sendTextMessage( char[] number, char[] messg ) sends an SMS containing messg as body to number. I use AT commands to achieve all this:

in: AT+CMGF=1\r                   <- "Please send a text message."
out: OK                               <- "Sure thing!"
in: AT + CMGS = "[number]"\r  <- "Here's the number."
out: OK                               <- "Thanks!"
in: [messg] ctrl-z \r               <- "The body..."
out: > [some gibberish] OK     <- "Your message is sent"

There is no question of whether or not this works. I've been using it successfully for quite a while now. The problem is: the first 6 characters of number come out as gibberish chars when I run my sketch. In previous versions of this project I used an identical version of sendTextMessage() with no problems. In fact, I made no huge changes in the programs' overall structure between the two versions, which are both attached. I also attached a sample output, containing the gibberish in line 52. I manually added line numbers afterward.

Having encountered a similar problem before, I immediately assumed the problem was related to memory, so I made the standard changes: Moved any non-changing Strings to PROGMEM, put all misc strings in F(). I also got a little memory measurement function off of the forums, and I call it at places of interest. Finally I added Serial printouts at the beginnings and ends of all relevant functions, to look for crashes or weird behavior. (">functionName + date/time = beginnig of funtion, <functionName + date/time = end of function")

According to the memory function, I never get close to filling up the RAM. You'll see those numbers on the sample output(lines 31, 34, 38, 40, 41, 43, 45). Furthermore, all of the functions seem to be behaving themselves.
Which sucks.

Please take a look. I know my code isn't super awesome. I am quite new to Arduino. Please tell me what steps I can take to further investigate and/or solve this problem. If it's a memory issue, please tell how you knew.

Please note that the previous version is quite stripped down, having no Serial outputs. For some reason I thought removing them would save memory. But the part of the program in question works perfectly, although there are other unrelated errors I'm fixing. The current version is also not perfected yet, with a few possible unrelated bugs.

Previous version: RemoteCallLoggerV1e1safe.ino
Current version: RemoteCallLoggerV1e2safe.ino


Nick Gammon

This is pretty hard to read:

Code: [Select]

        //+CMTI: "SM",3
          if( idleExcept( &smsIncoming ) &&
            length >= e + 20 &&
            char( incoming[e + 1] ) == 'C' &&
            char( incoming[e + 2] ) == 'M' &&
            char( incoming[e + 3] ) == 'G' &&
            char( incoming[e + 4] ) == 'R' &&
            char( incoming[e + 5] ) == ':' &&
            char( incoming[e + 6] ) == ' ' &&
            char( incoming[e + 7] ) == char( 34 ) &&
            char( incoming[e + 8] ) == 'R' &&
            char( incoming[e + 9] ) == 'E' &&
            char( incoming[e + 10] ) == 'C' &&
            char( incoming[e + 11] ) == ' ' &&
            char( incoming[e + 12] ) == 'U' &&
            char( incoming[e + 13] ) == 'N' &&
            char( incoming[e + 14] ) == 'R' &&
            char( incoming[e + 15] ) == 'E' &&
            char( incoming[e + 16] ) == 'A' &&
            char( incoming[e + 17] ) == 'D' &&
            char( incoming[e + 18] ) == char( 34 ) &&
            char( incoming[e + 19] ) == ',' &&

How about looking up "strcmp"?


I have not looked at your code yet but the problem has all the hallmarks of an array overrunning its bounds and impinging on another variable.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


OK. So far so good. I'll check out the arrays. And use strcmp in the very near future...


So I tried a few things on v1e2, but to no avail. After that I just abandoned all that, and built a new version based on the very first version: v1e0. Now it seems to be working just fine.


At least now I know strcmp  XD


Go Up