Shop OBEX P1 Docs P2 Docs Learn Events
Some more C help needed — Parallax Forums

Some more C help needed

ThricThric Posts: 109
edited 2011-11-28 14:25 in General Discussion
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,
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

  • Martin_HMartin_H Posts: 4,051
    edited 2011-11-26 17:07
    The temp variable is not an array type, so temp[0] is going to try take the address temp contains, add zero, and dereference. This is broken on a couple levels. But if you want a character array use char temp[sizeConstant] to allocate space for multiple characters.

    Also the %c prints a single character, you use %s for a sequence of characters ending in 0 (null).
  • ThricThric Posts: 109
    edited 2011-11-26 17:18
    thanks for the help! I did what you suggested, now the code looks like this:
    int test(){
        int x=0;
        char temp;
        temp="$";
        OpenComport(wp,wbr);
        while(x==0){
           cprintf(wp,"%s",temp);
         }
    }
    

    Still doesn't want to work :-/. If my thinking is correct would cprintf(wp,"%s",temp); follow this progression?
    cprintf(wp,"%s",temp);
    cprintf(wp,"%s","$");
    cprintf(wp,"$");
    
    At least thats how I make sense of it
  • RossHRossH Posts: 5,519
    edited 2011-11-26 17:20
    Thric wrote: »
    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]);
         }
    }
    

    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):
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        int x=0;
        char temp[] = "this is a string\n";
    
        // one way to print the string temp:
        for (x=0; x < strlen(temp); x++) {
           printf("%c",temp[x]);
        }
    
        // an easier way print the string temp:
        printf("%s", temp);
    
    }
    

    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.
  • bill190bill190 Posts: 769
    edited 2011-11-26 17:47
    Some of these terms can be a bit intimidating to say the least!

    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
  • ThricThric Posts: 109
    edited 2011-11-26 18:52
    Still trying to rack my brain for a solution :( but i've gotten rid of the array part. Maybe adding the link for the definition of csprintf() would help: http://www.teuniz.net/RS-232/

    I've also changed the csprintf to just printf and that works fine with what I have:
    //What I have now. Still not working
    int test(){
        int x=0;
        char temp;
        temp="$";
        OpenComport(wp,wbr);
        while(x==0){
           cprintf(wp,"%c",temp);
         }
    }
    
    //What I've tried with just printf, works fine.
    int test(){
        int x=0;
        char temp;
        temp="$";
        OpenComport(wp,wbr);
        while(x==0){
           printf("%c",temp);
         }
    }
    
    
  • ThricThric Posts: 109
    edited 2011-11-26 21:25
    Alright, made some more progress. Although I can't get the $ working I have been able to get integers working. The propeller uses the pst function decin to wait until a certain number is received and then lights the led.
    int test(){
        int x=0,n=0;
        x=1;
        OpenComport(wp,wbr);
        while(n==0){
           SendByte(wp,"%d",x);
            printf("%d",x);              //Just to see whats happening
         }
    }
    

    I tried the same idea for the $ but still no luck
    int test(){
        int n=0;
        char temp;
        temp='$';
        OpenComport(wp,wbr);
        while(n==0){
           SendByte(wp,"%c",temp);
            printf("%c",temp);
         }
    }
    
  • RossHRossH Posts: 5,519
    edited 2011-11-26 21:43
    Thric wrote: »
    int test(){
        int n=0;
        char temp;
        temp='$';
        OpenComport(wp,wbr);
        while(n==0){
           SendByte(wp,"%c",temp);
            printf("%c",temp);
         }
    }
    

    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.
  • ThricThric Posts: 109
    edited 2011-11-27 10:04
    Hmm well things seems to be running a little funky. I'm re-installing Code Blocks and MinGW. Maybe that'll help clear somethings up.
  • bill190bill190 Posts: 769
    edited 2011-11-27 10:11
    When I was first learning C, I thought that what you could do with one command, you could also do with other commands...

    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!
  • RossHRossH Posts: 5,519
    edited 2011-11-27 14:02
    Thric wrote: »
    Hmm well things seems to be running a little funky. I'm re-installing Code Blocks and MinGW. Maybe that'll help clear somethings up.

    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:
    #include <stdio.h>
    #include <rs232.h>
    

    Ross.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-11-27 17:05
    > 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.

    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.
  • RossHRossH Posts: 5,519
    edited 2011-11-27 17:15
    Kevin Wood wrote: »
    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.
  • TorTor Posts: 2,010
    edited 2011-11-28 01:25
    @Thric:

    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:
    /* (FIle is zz.c): */
    int test()
    {
            return 0;
    }
    
    int main (void)
    {
            test (4);
            return 0;
    }
    
    $ cc -Wall -o zz zz.c
    $
    
    v.s.
    /* (FIle is zz.c): */
    int test([b]void[/b])
    {
            return 0;
    }
    
    int main (void)
    {
            test (4);
            return 0;
    }
    
    $ cc -Wall -o zz zz.c
    zz.c: In function 'main':
    zz.c:8:2: error: too many arguments to function 'test'
    zz.c:1:5: note: declared here
    $
    

    -Tor
  • ThricThric Posts: 109
    edited 2011-11-28 14:25
    Alright, So i've re-installed and worked my way through to back to were I was. There is so much information gahhhh!! I'll have to sift through and try all your suggestions and I'll try to report back my progress :). Thanks for all the tips so far.
Sign In or Register to comment.