Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strings in C - help
10-05-2012, 09:24 PM, (This post was last modified: 10-05-2012, 09:33 PM by KiloOne.)
#1
Strings in C - help
Can someone tell me why this string gets garbled and the program execution halts?
   
Sorry about showing this as an image but it just shows it all.
Thanks,
Dale

Here is code too:
Code:
#include <delay.c>

void setup() {
    Delayms(3000);  //delay to start hyperterminal
    CDC.println("You should see this if you got hyperterminal going quick enough");
    }

void loop() {
    int i=0;
    char tmp[20]="";
    char sdl[400] = "";
    strcpy(sdl,"");
    for (i=1;i<=20;i++){
        sprintf(tmp,"%10i,",i);
        strcat(sdl,tmp);
        CDC.printf(sdl);
        CDC.println("");
        Delayms(1000);
    }
    Delayms(2000);
    }
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
10-05-2012, 10:00 PM,
#2
RE: Strings in C - help
Hi Dale,

it is possible that it is buffer overflow within Windows. I have been working on updating the SD library examples and due to having problems with my Ubuntu set-up I have been testing a couple of the programs on a fairly new Windows 7 laptop. I found that I was loosing characters and output lines until I slowed down the output by putting a few delay(1);s around the place.

You could test this by increasing the length of the first output line in your program till it as least 5 - 10 characters longer than the length of the string that is causing problems.

There is a comment in the wiki about the use of CDC.print with windows (wiki CDC.print( ) under Bugs) but I don't know if it is still current as I have never tried that approach. Also it seems to be more concerned with detecting the presence of a terminal rather than real flow control.

Have you tried changing the speed setting or the terminal/driver and seeing how that affects the output?

Regards
Board = PIC32-Pinguino-OTG Rev C
OS = Linux Unbuntu 11.10 till 26 Apr 2012
OS = Linux Unbuntu 12:04 from 27 Apr 2012
Reply
10-05-2012, 10:06 PM,
#3
RE: Strings in C - help
So I added a little more code to see where the error is and it appears to me that the data is being garbled by the strcat() function.

What am I doing that could cause this?
   

Dale

New code:
Code:
#include <delay.c>

void setup() {
    Delayms(3000);  //delay to start hyperterminal
    CDC.println("You should see this if you got hyperterminal going quick enough");
    }

void loop() {
    int i=0;
    int err=0;
    char tmp[25]="";
    char sdl[400] = "";
    strcpy(sdl,"");
    for (i=1;i<=10;i++){
        err = sprintf(tmp,"%20i,",i);
        if (err<0){
            CDC.println("Error");
        } else {
            CDC.printf(tmp);
            CDC.println("");
        }
        strcat(sdl,tmp);
        CDC.printf(sdl);
        CDC.println("");
        Delayms(1000);
    }
    Delayms(2000);
    }
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
10-05-2012, 10:18 PM,
#4
RE: Strings in C - help
#include <delay.c>

void setup() {
Delayms(3000); //delay to start hyperterminal
CDC.println("You should see this if you got hyperterminal going quick enough");
}

void loop() {
int i=0;
char tmp[20]="";
char sdl[400] = "";
strcpy(sdl,"");
for (i=1;i<=20;i++){
sprintf(tmp,"%d,",i);
strcat(sdl,tmp);
CDC.printf(sdl);
CDC.println("");
Delayms(1000);
}
Delayms(2000);
}

Be careful, max length for the CDC TX buffer is 64 bytes.


(10-05-2012, 09:24 PM)KiloOne Wrote: Can someone tell me why this string gets garbled and the program execution halts?

Sorry about showing this as an image but it just shows it all.
Thanks,
Dale

Here is code too:
Code:
#include <delay.c>

void setup() {
    Delayms(3000);  //delay to start hyperterminal
    CDC.println("You should see this if you got hyperterminal going quick enough");
    }

void loop() {
    int i=0;
    char tmp[20]="";
    char sdl[400] = "";
    strcpy(sdl,"");
    for (i=1;i<=20;i++){
        sprintf(tmp,"%10i,",i);
        strcat(sdl,tmp);
        CDC.printf(sdl);
        CDC.println("");
        Delayms(1000);
    }
    Delayms(2000);
    }
Reply
10-05-2012, 10:30 PM, (This post was last modified: 10-05-2012, 11:50 PM by KiloOne.)
#5
RE: Strings in C - help
Thanks Marks,

You got me checking into CDC.

And it does look like CDC is causing issues.

If I just do smoke signals with the yellow LED using this code, there is not freezing but I can not tell if the data is garbled, likely it is not.
Code:
#include <delay.c>
void setup() {
    }

void loop() {
    int i=0;
    int err=0;
    char tmp[125]="";
    char sdl[400] = "";
    strcpy(sdl,"");
    for (i=1;i<=20;i++){
        err = sprintf(tmp,"%10i,",i);
        if (err<0){
            pinMode(YELLOWLED, OUTPUT);            // Set YELLOWLED pin for output
            digitalWrite(YELLOWLED, HIGH);         // Turn YELLOWLED On
            Delayms(2000);                        
            digitalWrite(YELLOWLED, LOW);         // Turn YELLOWLED Off
        }
        strcat(sdl,tmp);
        Delayms(500);
        pinMode(YELLOWLED, OUTPUT);            // Set YELLOWLED pin for output
        digitalWrite(YELLOWLED, HIGH);         // Turn YELLOWLED On
        Delayms(500);                        
        digitalWrite(YELLOWLED, LOW);         // Turn YELLOWLED Off
    }
    Delayms(3000);
    }

I will test the CDC windows bug WIKI suggestion.

Dale

Thank-you jp, I'm sure that will be my problem.

Is there somewhere I can look for more limitations on CDC, this issues caused me a few hours of hair pulling.

Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
10-05-2012, 10:43 PM,
#6
RE: Strings in C - help
Hi again Dale,

AS I now have my Ubuntu system up and running again, I have tried your program on that and it behaves similarly it does not seem like a serial buffer problem. Having played around a bit further, the problems seem to always start occurring when the string you are generating gets to around 68 characters long so it may well be related to the CDC buffer as indicated by JP.

Regards
Board = PIC32-Pinguino-OTG Rev C
OS = Linux Unbuntu 11.10 till 26 Apr 2012
OS = Linux Unbuntu 12:04 from 27 Apr 2012
Reply
10-05-2012, 10:45 PM, (This post was last modified: 10-05-2012, 11:08 PM by KiloOne.)
#7
RE: Strings in C - help
Oh darnConfused

I will keep looking at this, nice to know you have issues too, maybe it is strcat() issues.

Dale

So, I removed the CDC usage in my main code and execution still halts in the section where I use strcat() to assemble a long string (130 bytes right now) of comma delimited values that I am trying to write to the SD every second.

If I limit the number of values I save so that the string is about 68 bytes or less the program functions and saves the data.

As you can see, I use sprintf to format a value and then strcat to add it to the string. Is there another way I can assemble my values into a string?

Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
10-05-2012, 11:30 PM,
#8
RE: Strings in C - help
You can do this manually to see if the problem is due to strcat:

- Use strlen to know the length of the source string ( or even scan the string until \0 is found )
- Move a pointer on the destination string until \0 is found
- transfer from source to destination ( byte by byte )
- write a \0 in the destination string when transfer is finished

\0 ( \zero ) is the end string delimiter for C langage.




(10-05-2012, 10:45 PM)KiloOne Wrote: Oh darnConfused

I will keep looking at this, nice to know you have issues too, maybe it is strcat() issues.

Dale

So, I removed the CDC usage in my main code and execution still halts in the section where I use strcat() to assemble a long string (130 bytes right now) of comma delimited values that I am trying to write to the SD every second.

If I limit the number of values I save so that the string is about 68 bytes or less the program functions and saves the data.

As you can see, I use sprintf to format a value and then strcat to add it to the string. Is there another way I can assemble my values into a string?

Dale
Reply
10-05-2012, 11:32 PM, (This post was last modified: 10-05-2012, 11:35 PM by mf01.)
#9
RE: Strings in C - help
Hi Dale,

you can have multiple parameters in either the sprintf() or Sprintf() functions. The syntax of the format string should be similar to that CDC.printf() or Serial.printf(). I haven't explored the maximum length string that can be assembled.

Note Sprintf() is the Puinguino version of the sprintf() function. It is more limited in which variable types it can handle but it also takes fewer resources.

Regards
Board = PIC32-Pinguino-OTG Rev C
OS = Linux Unbuntu 11.10 till 26 Apr 2012
OS = Linux Unbuntu 12:04 from 27 Apr 2012
Reply
11-05-2012, 06:59 AM,
#10
RE: Strings in C - help
Hi

Yes, now I remember, I found this problem in my first program .

Quote://NOTE

// CDC.printf("%s",Str); go crazy if the length of the string is more than 64 Bytes

// Serial.xxx are alias of serial1xxx Es. Serial.begin serial1init@include <pinguinoserial1.c>

// Serial.printf is a blocking function


The maximum length of the string can be 64 characters.
To solve the problem I splitted the frame in more subframes.


Bye Bye, Moreno
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)