You are here

SwitchcaseUart | Cypress Semiconductor

SwitchcaseUart

Summary: 13 Replies, Latest post by NewPSoc on 04 Oct 2015 08:19 AM PDT
Verified Answers: 3
Last post
Log in to post new comments.
NewPSoc's picture
User
46 posts

Hi 

I have some errors in my code.

I am doing a switch case,  after receiving data from UART.  The compiler isn't so happy doing a muliti character after case like: case 10<EOM>. What alternative could I do.

I have attached my code.

Thanks. 

user_14586677's picture
User
7648 posts

The CASE statement switches on a constant or a single literal.

 

http://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm

 

So you need to encode the literal expression you have either to a single literal

or a numeric value.

 

Regards, Dana.

NewPSoc's picture
User
46 posts

Hi

I have encoded the statement to a single literal.  Though, I still have some errors. I have attached the new code.

Best regards 

user_14586677's picture
User
7648 posts

You called handl() on line 71 w/o supplying a parameter.

 

After compile look at Notice List tab at bottom of screen for the Compiler output,

read the error, and double click it to highlight the line of offending code.

 

 

Regards, Dana.

NewPSoc's picture
User
46 posts

Hi

I do no longer have errors in my code.  But when calling plccom it does not send the request further to my handl function, which results in nothing.  Can anybody see what's wrong with my code?

My program is attached. 

Best regards 

user_1377889's picture
User
10803 posts

Is the sequence "<EOM>" really consisting of the characters '<','E','O','M','>' or is it ment to be the ascii control character for end-of-message which is 0x19. I would suggest you to use the debugger to verify which characters are received.

Keep in mind that when you have got once a correct message you will have to reset your logic for a new message.

 

Bob

user_14586677's picture
User
7648 posts

Not sure, but some minor changes -

 

void handl(uint8 Rx) {
    
   // delay
 
    switch( Rx ) {
    
        case 'A' :
        
            Pin_1_Write(1);                     // set pin high ready to send;
            CyDelay(5);                         // delay
            UART_1_PutString("Tyv");
            CyDelay(5);                         // delay
            Pin_1_Write(0);                     // set pin low, ready to receive again.
            break;
            
        case 'B' :
            
            UART_1_PutString("Validation");
            UART_1_PutString("2");
            break;
            
        case 'C' :
            
            Pin_2_Write(1);                      // Set Led hight
            UART_1_PutString("Contact Server");
            UART_1_PutString("3");
            break;
    }
}

void plccom( ) {
    
    if(UART_1_GetRxBufferSize()  !=  0 ) {

        Rxbuffer[i++] = UART_1_GetChar();
        //UART_1_PutString(Rxbuffer);
    }
    
    Rx = ' ';
    
    if (strstr(Rxbuffer, "10<EOM>") != NULL)  Rx = 'A';

    if (strstr(Rxbuffer, "1OK<EOM>") != NULL) Rx = 'B';

    if (strstr(Rxbuffer, "1OK<EOM>") != NULL) Rx = 'C';

    if (strstr(Rxbuffer, "1OK<EOM>") != NULL) Rx = 'D';  
 
    if ( Rx != ' ' ) handl( Rx );
}

NewPSoc's picture
User
46 posts

Hi again

I got it to work thanks for the advise  (In my way ;)).

 I want one more application to work.  If entering Case A (receiving 10<EOM>) I want to listen again to the UART, and then if the statement is true (receiving 10k<EOM>) enter case B.

The code is attached.  

Best regards 

user_14586677's picture
User
7648 posts

You change the handl() to return a flag if EOM was invoked, set the flag in the EOM CASE,

and call the function again with Rx of 'B' if that is true after the first call to handl(). Clear the

flag after you execute the second CASE.

 

Just a little advice, indent your code, its difficult to read. Indents

occur on test statements, take a look at the attached file in Creator

as an example.

 

Regards, Dana.

Attachments: 
NewPSoc's picture
User
46 posts

 

Hi Ok.

After  receiving 10<EOM>. I want to read the UART again  and if receiving 1OK<EOM>  enter case B.

How can I do that?

Best regards. 

 

 

user_14586677's picture
User
7648 posts

Do you not want to execute CASE A if 10<EOM> received ?

 

Or do you want to execute CASE A when 10<EOM> is received, then

if immediately following you get a 10K<EOM> execute CASE B ? Which

is the way the code works now.

 

List all possible conditions for executing CASE A and B.

 

The way to see this is to draw a flow diagram to work out the logic, then write the

code.

 

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=20&cad=rja&uact=8&ved=0CGIQtwIwE2oVChMI_crQzcuoyAIVgfI-Ch2G3wYW&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIkZsQN3Dkxc&usg=AFQjCNHAich35vk94vwCEDf6-ktarjAB5w

 

Regards, Dana.

Log in to post new comments.