-2

I'm trying to get UART-communication to work, and I used some example-code from ATMEL as a base for this (UART Example). The main change I want to make is that instead of using test_string I want to receive a string/char array using uart_getchar() and run a character check, I.E:

    data[i] = uart_getchar();
    //repeat

    switch (data[0]) {
        case: 'w':
        //do stuff 1
        break;

        case: 'r':
        //do stuff 2
        break;
    }

But I'm having difficulties properly understanding the UART example, and getting my new code to work.

Any tips?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
DoTheGenes
  • 197
  • 2
  • 4
  • 22
  • and what do you not understand, specifically? – The Paramagnetic Croissant Jul 30 '14 at 07:34
  • you cannot get a string using `uart_getchar()`, because its a character function. – askmish Jul 30 '14 at 07:34
  • 2
    How can you tell when the string received from UART ends? Newline? Anyway, you need a buffer, and then you need to read chars to the buffer until you detect end of string (beware buffer overrun, remember to add terminating `'\0'` char if you want it to be a C string). – hyde Jul 30 '14 at 07:54
  • 2
    Another thought: in case it is unclear to you, in C a "string" is a very different thing from a single "character", and handling them needs totally different code. Just saying this, in case you actually want to read individual chars (same as bytes, in C), and not entire strings (0..N chars of actual string content, *always* followed by `'\0'` byte marking the end of the string). – hyde Jul 30 '14 at 07:57
  • The example has expired, please update – Marco Ramirez Castro Mar 06 '20 at 21:51

1 Answers1

1

Instead of this line in the code:

Assert (data == test_string[cnt++]);

Put your switch case code.

switch (data[0]) {
        case: 'w':
        //do stuff 1
        break;

        case: 'r':
        //do stuff 2
        break;
    }

And maybe instead of switching on data[0], you wanted to use data[i].

askmish
  • 6,464
  • 23
  • 42