Some more C help needed
Thric
Posts: 109
So last time I asked for a little bit of C help I got alot of help so I thought I might as well ask another problem that has stumped me for a good 5 hours.
The basics of the program is to send a character to a serial connection which will just light an led for now. Currently I have a setup so that on COM 3 i have a XBEE and on my propeller theres the corresponding xbee. I've narrowed the problem from my Spin program (as the led works fine with tera term or the PST) and figured that the problem lies with my C program.
so here is a C program that actually works,
Now the problem with this is that I want to be able to send different characters, not just a $. So I tried this which doesn't work:
In the end i want to be able to send a string of characters, but only one at a time. With this in mind I was thinking that I would have a string of characters named temp and then use temp[x] in a loop, where x would increase by one every iteration.
thanks
The basics of the program is to send a character to a serial connection which will just light an led for now. Currently I have a setup so that on COM 3 i have a XBEE and on my propeller theres the corresponding xbee. I've narrowed the problem from my Spin program (as the led works fine with tera term or the PST) and figured that the problem lies with my C program.
so here is a C program that actually works,
int test(){ int x=0; OpenComport(wp,wbr); while(x==0){ cprintf(wp,"$"); } }
Now the problem with this is that I want to be able to send different characters, not just a $. So I tried this which doesn't work:
int test(){ int x=0; char temp; temp="$"; OpenComport(wp,wbr); while(x==0){ cprintf(wp,"%c",temp[0]); } }
In the end i want to be able to send a string of characters, but only one at a time. With this in mind I was thinking that I would have a string of characters named temp and then use temp[x] in a loop, where x would increase by one every iteration.
thanks
Comments
Also the %c prints a single character, you use %s for a sequence of characters ending in 0 (null).
Still doesn't want to work :-/. If my thinking is correct would cprintf(wp,"%s",temp); follow this progression? At least thats how I make sense of it
I'm not surprised. That program shouldn't even compile! For a start you are declaring temp as a char, but then treating it as if it was an array.
Here is a similar program that will do what you probably expect (not sure what C compiler you are using, but this program is ANSI C, and will compile with both GCC and Catalina):
Note how temp is declared as an array of characters, initialized to a string value. Also note that printing character by character and usinfg %c is the same as printing the whole string at once using %s - provided you don;t want to actually transmit any NUL characters.
Ross.
But you "save" or "reserve" memory for something. And when you do this, you say how much space you will need.
You might just be able to store one letter like "A".
Or maybe two letters like "AA"
Or a whole bunch of letters like "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"!
An "array" is different in that it is sort of like a filing cabinet with many files in it. You need to specify *which* file [in that cabinet] you are storing or retrieving information from.
Here is a web page I found which shows a graphic of an array. That might make this a bit easier to understand...
http://einstein.drexel.edu/courses/Comp_Phys/General/C_basics/#arrays
I've also changed the csprintf to just printf and that works fine with what I have:
I tried the same idea for the $ but still no luck
Thric, that code should not compile either. If it does, you need to think about changing C compilers!
You are using SendByte incorrectly - check the definition in rs232.h
Ross.
Then I got a couple of books on C and learned this was not so!
Each command in C is like a different program on a PC. For example on a PC there are not many options with the text editor "Notepad". But there are all sorts of options for Microsoft Word. They are two separate things. And to learn about Notepad, you read the help file or a book which covers Notepad. For Word, the help file or a book which covers Word. What you can do with Word does not apply to Notepad. And what you can do with Notepad does not apply to Word!
Same with commands in C.
To learn what you can do with printf, read about printf. And what you can do with printf does not necessarily apply to other commands.
I searched google.com for...
printf ansi c
And found this which is just on printf...
http://www.thinkage.ca/english/gcos/expl/c/lib/printf.html
Then I searched google.com for...
putc ansi c
And found this which is just on putc...
http://www.thinkage.ca/english/gcos/expl/c/lib/putc.html
Notice there are not many options for putc!
Hi Thric,
After my last post, it occurred to me that you may not be including the appropriate C header files - in which case the C compiler will not alert you to any invalid function usage.
You should be including at least the following at the top of your C program file:
Ross.
I'm not sure that you can even get that to compile without main(), so I think that he might just be posting the function that gives the error, and not the complete program.
Hi Kevin,
Yes, I realized what was posted was not the whole program, since several of the variables used are also undefined - this is why it only occurred to me sometime later that Thric might in fact be leaving out the inclusion of the header files.
This is a very common mistake for C newcomers, and would also explain why the compiler didn't warn about the invalid usage of the SendByte function.
Ross.
Try compiling your code with the -Wall parameter ("turn on all warnngs") passed to gcc. (I'm assuming that's already supported by propgcc - it's one of the most prominent features of GCC).
There's a lot of problems with your code, as pointed out by previous posters. Most if not all of them would have been caught by the compiler if you use the -Wall option.
In addition to the 'temp' declaration problem I would also recommend that you don't declare empty parameter lists for functions, i.e. don't use 'int test()', use 'int test (void)'. This is because with '()' the compiler falls back to pre-ANSI behavior and won't do its normal prototype check: It won't detect it if you call the function incorrectly: v.s.
-Tor