SwitchcaseUart

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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. 

0 Likes
1 Solution
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

View solution in original post

0 Likes
13 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi

   

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

   

Best regards 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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 );
}

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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 

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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. 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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=0CGIQtwIwE2oVChM...

   

 

   

Regards, Dana.

Anonymous
Not applicable

Exactly, I want to execute case A, when 10<EOM> is received, and then I am waiting on a confirm (1OK<EOM>) and when I receive this confirm, I should execute CASE B:

   

Pseudocode:

   

If received 10<EOM> execute CASE A,

   

wait for 1OK<EOM>, If  1OK<EOM> is received execute CASE B:

   

But at the moment that's not what the code is doing. When receiving 10<EOM> it correctly executes CASE A, but when I send 10K<EOM> , it does not execute CASE B, but continuing executing CASE A.  

   

Best regards. 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Set a breakpoint in the CASE statement code, say at line 25, and look at

   

Rx and the Rxbuffer, for the case where you think the buffer has "1OK<EOM>"

   

in it.

   

 

   

Note you typed

   

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

   

 

   

is "1OK<EOM>" supposed to be typed as "10K<EOM>",

   

eg the second letter is a zero, not the letter O ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Ok.

   

I have now tried to debug both using the PsoC3 debug tool and the printf (UART_PutString).

   

It seems that Rxbuffer doesn't get all the characters, when sending 1OK<EOM>. What I can see is that it cuts off the first byte,  so this is whats inside the buffer OK<EOM>.-

   

Yes. it is 1OK<EOM> , I have changed that in the code.    

   

 Thank you 😉

0 Likes