View previous topic :: View next topic |
Author |
Message |
Honken
Joined: 03 Mar 2004 Posts: 15
|
printf sometime doesn't output correct data?? |
Posted: Thu Jan 17, 2008 6:32 am |
|
|
I am running v4.066 with a dsPIC30F6010A and noticed that sometimes after I added or removed some code the printf function outputs more data after the NULL termination.
I looks like it does something wrong when it using its pointer so it wont see the null termination.
It's like if I have odd numbers of char in the string then it's OK but with even I get the string and some junk data on my RS232 line.
Is it possible to add some sort off pre-processor statement so the code alines to even or odd memory addresses?? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jan 17, 2008 8:03 am |
|
|
Please post sample code. |
|
|
Honken
Joined: 03 Mar 2004 Posts: 15
|
|
Posted: Thu Jan 17, 2008 10:40 am |
|
|
boolean loadCANfile(char filenname[], int8 handle, struct file_handle * hFile)
{
// This fprintf will output more than "test.txt" it will add 2 more byte from memory
fprintf(RS232, "\r\nO %cR>%s\r\n", handle, filenname);
}
main
{
loadCANfile("test.txt", 1, &hFiles_0);
} |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jan 18, 2008 2:48 am |
|
|
Your problem may be related to an error I can see.
you pass the value 1 as handle.
You print this using %c.
%c displays the character of the value so an '1' is 49 or 0x31. the character represented by a 1 is SOH (Start Of Heading) this will most likely be displayed as garbage or you may not see anything at all.
You need to use %d to display the actual value of handle.
See if that helps. |
|
|
Honken
Joined: 03 Mar 2004 Posts: 15
|
|
Posted: Fri Jan 18, 2008 4:29 am |
|
|
I tested that but then I get "0 49R>test.txt1 2"
with %c I get "0 1R>test.txt1 2"
And I want "0 1R>test.txt"
Really strange |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jan 18, 2008 7:14 am |
|
|
Do you have more code before the fprintf in that function ?
try using char *filename in the header instead of char filename[].
The %c %d issue is very strange. This implies that handle does actually contain the ascii value of '1'.
So unless you are modifying filename and handle before calling fprintf then something very strange is happening.
It also looks like there are 3 extra chars 1[sp]2
Going back to your first post. Doe this work sometimes depending on adding more code ? or lines (blank) ? |
|
|
kamputty Guest
|
|
Posted: Fri Jan 18, 2008 1:25 pm |
|
|
Hi all!
What happens if you use this line
fprintf(RS232, "\r\nO %cR>%s\r\n", '0'+handle, filenname);
You will notice I added ---> '0'+handle
i'm assuming that 'handle' has NOT been adjusted to use ascii, but is a number 0~9. By using the '0'+handle, we change the value to ascii 0~9...but you should really change it to %d...
~Kam (^8* |
|
|
Honken
Joined: 03 Mar 2004 Posts: 15
|
|
Posted: Sat Jan 19, 2008 3:57 am |
|
|
I forgot to add the code above the fprintf function.
Yes I convert the handle to ASCII before I use the fprintf,
// Make it a char
handle = handle + 0x30;
wonder if it has to be something with the address of the handle?
I have noticed if handle is located on even address the problem ocurs?!?! |
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Sat Jan 19, 2008 10:36 am |
|
|
Do you declare 'handle' as an 'int'? If so then I assume that its a 16 bit value. And your function expects an 8 bit value. The whole problem looks like a misalignment with the parameters passed to the function call - and in some cases the string terminator is missed with the result that you get the extra characters printed. |
|
|
|