13 Replies Latest reply on Oct 4, 2015 8:19 AM by valskibuda_1466811

    SwitchcaseUart

    valskibuda_1466811

      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. 

        • 1. Re: SwitchcaseUart
          user_14586677

          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.

          • 2. Re: SwitchcaseUart
            valskibuda_1466811

            Hi

               

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

               

            Best regards 

            • 3. Re: SwitchcaseUart
              user_14586677

              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.

              • 4. Re: SwitchcaseUart
                valskibuda_1466811

                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 

                • 5. Re: SwitchcaseUart
                  user_1377889

                  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

                  • 6. Re: SwitchcaseUart
                    user_14586677

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

                    1 of 1 people found this helpful
                    • 7. Re: SwitchcaseUart
                      valskibuda_1466811

                      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 

                      • 8. Re: SwitchcaseUart
                        user_14586677

                        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.

                        • 9. Re: SwitchcaseUart
                          valskibuda_1466811

                          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. 

                          • 10. Re: SwitchcaseUart
                            user_14586677

                            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.

                            1 of 1 people found this helpful
                            • 11. Re: SwitchcaseUart
                              valskibuda_1466811

                              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. 

                              • 12. Re: SwitchcaseUart
                                user_14586677

                                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.

                                • 13. Re: SwitchcaseUart
                                  valskibuda_1466811

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