1 2 Previous Next 21 Replies Latest reply on Mar 25, 2013 6:57 PM by riko.husin

    How can I get byte_length from Rx port ?

    riko.husin

      Guys,

         

      How can I get byte_length from Rx port ?

         

      Thanks

        • 1. Re: How can I get byte_length from Rx port ?
          riko.husin

          Is it posibble doing this :

             

          byte_length=c;
                   for (i=0;i<byte_length;i++)
                         {
                           RxdataBuff[i] = c;    // Get a character from UART RX data register
                         }   

          • 2. Re: How can I get byte_length from Rx port ?
            arvi

            It is possible to write a string of characters (multiple bytes) into a TX. But not the same with RX.\

               

            The RX has a buffer that can hold only 1 byte of information, and can read only one byte at a time.

               

             

               

            If you want to get the byte count, you have to keep reading the data from the buffer, one byte at at time.

               

             

               

            You can give this a try:

               

             

               

             

               

             

               

            while (UART_bReadRxStatus() & UART_RX_REG_FULL)) // keeps looping as long as there is data in the RX buffer

               

            {

               

            if (UART_cReadChar()) RXcount++;  //_cReadChar reads one character from buffer, returns 0 if there's an error or no data

               

            }

            • 3. Re: How can I get byte_length from Rx port ?
              riko.husin

              How can I detect Rx buffer with content :

                 

              AB 04 01 04 00

                 

              ??

              • 4. Re: How can I get byte_length from Rx port ?
                riko.husin

                can I do :

                   

                byte_length=UART_1_cGetChar();
                                       
                                        for (i=0;i<byte_length;i++)
                                            {
                                                                    RxdataBuff[i] = UART_1_cGetChar();    // Get a character from UART RX data register
                                            }
                                           
                                                    LCD_1_Position(0,0);
                                                    LCD_1_PrCString("SN: ");
                                                    LCD_1_Position(1,0);

                                                    for (i=0;i<10;i++)
                                                    {           
                                                        LCD_1_PrHexByte(RxdataBuff[i]); // Print serial number of the card detected
                                                    }       

                • 5. Re: How can I get byte_length from Rx port ?
                  riko.husin

                  while (UART_bReadRxStatus() & UART_RX_REG_FULL)) // keeps looping as long as there is data in the RX buffer

                     

                  {

                     

                  if (UART_cReadChar()) RXcount++;  //_cReadChar reads one character from buffer, returns 0 if there's an error or no data

                     

                  }

                     

                   

                     

                  It's not a valid working code......

                  • 6. Re: How can I get byte_length from Rx port ?
                    user_27941315

                    You need to enable the command buffer and define the command terminator.

                       

                    Then using the code below it should work

                       

                    if(!UART_bCmdCheck() ){

                       
                            byte_length = UART_bCmdLength();    
                       
                        }   
                       
                            
                       
                        Regards   
                    • 7. Re: How can I get byte_length from Rx port ?
                      riko.husin

                      So it'll be like this :

                         

                                                                  if(!UART_bCmdCheck() ){
                                                                     byte_length = UART_bCmdLength();
                                                                  }

                                          
                                              for (i=0;i<byte_length;i++)
                                                  {
                                                                          RxdataBuff[i] = UART_1_cGetChar();    // Get a character from UART RX data register
                                                  }
                                                
                                                          LCD_1_Position(0,0);
                                                          LCD_1_PrCString("SN: ");
                                                          LCD_1_Position(1,0);

                                                          for (i=0;i<10;i++)
                                                          {          
                                                              LCD_1_PrHexByte(RxdataBuff[i]); // Print serial number of the card detected
                                                          } 

                      • 8. Re: How can I get byte_length from Rx port ?
                        riko.husin

                        How can I enable the command buffer and define the command terminator ?

                           

                        Can you give me example ? thank you

                        • 9. Re: How can I get byte_length from Rx port ?
                          riko.husin

                          I can't see command terminator since it's a reply from device......for 5 to 10 bytes.

                             

                          How can I create it ?

                             

                          Please find my manual attached, and it's on "Basic Command Description"

                             

                          Thanks a lot

                          • 10. Re: How can I get byte_length from Rx port ?
                            riko.husin

                            The logic :

                               

                            I gave command to device and it replied with AB 04 01 04 00 on Rx port, how can I parse it so I can display them into LCD ?
                            thank you

                            • 11. Re: How can I get byte_length from Rx port ?
                              user_27941315

                               To enable the command buffer, there is a parameter that you define to enable.

                                 

                              Could you re-attached the manual?

                                 

                               

                                 

                              The reply must formatted in some way, like an address and a data byte plus a checksum or anything else. If the reply sequence always end with a different value then after a pre-difined time has elapsed retrieve the data from the command buffer.

                                 

                               

                                 

                              Regards

                              • 12. Re: How can I get byte_length from Rx port ?
                                arvi

                                You can define the command terminator in the UART user module parameters window, and enable the RX command buffer option. RX interrupts must be enabled.

                                   

                                 

                                   

                                From the datasheet-

                                   

                                 

                                   

                                 

                                   
                                    RxCmdBuffer   
                                   
                                    

                                This parameter enables the receive command buffer and firmware used for command processing. The UART RX interrupt must be enabled for the command buffer to operate.

                                   
                                   
                                    RxBufferSize   
                                   
                                    

                                This parameter determines the number of RAM locations that are reserved for the receive buffer. The largest command that can be received is one less than the buffer size selected, because the string must be null terminated. This parameter is only valid when the RxCmdBuffer is enabled and the UART RX interrupt is enabled.

                                   
                                   
                                    CommandTerminator   
                                   
                                    

                                This parameter selects the character that signals the end of a command. When received, a flag is set signaling that a complete command has been received. After this flag is set, additional characters are not accepted until the cmdReset() function is called.

                                    

                                 

                                    

                                 

                                    

                                Make sure the input stream always contains the cmd terminator to get the count. If the command terminator is not received, in this code, byte_length will not be set-

                                    

                                if(!UART_bCmdCheck() )

                                    

                                {
                                           byte_length = UART_bCmdLength(); 
                                }

                                    

                                 

                                    

                                The other issue is that the RX cmd buffer has a size limitation (32 or 64 bytes, pls check)

                                    

                                 

                                    

                                 

                                    

                                If all you want to do is display the RX string on an LCD, the best solution is to use interrupts (you can get an interrupt every time one byte is received) and display the current byte in the RX buffer on LCD. Additionally use the RX status flags to determine if all data has been displayed.

                                   
                                • 13. Re: How can I get byte_length from Rx port ?
                                  riko.husin

                                  To enable the command buffer, there is a parameter that you define to enable.

                                     

                                  Could you re-attached the manual?

                                     

                                   

                                     

                                  The reply must formatted in some way, like an address and a data byte plus a checksum or anything else. If the reply sequence always end with a different value then after a pre-difined time has elapsed retrieve the data from the command buffer.

                                     

                                   

                                     

                                  Regards

                                  ==============   

                                   

                                     

                                  Yes sure, I'll attach the manual again.

                                     

                                  if you're talking about checksum, make more sense for me since I read about it on the manual but don't know how to use it yet.

                                     

                                  I need to check the end of the reply (Rx), but from I can see it's always changing.....

                                     

                                  I can print out Rx, but still random. What I want, is if I give command AB 02 01,

                                     

                                  device will reply AB 04 01 04 00, please see page 5 on the manual.

                                  • 14. Re: How can I get byte_length from Rx port ?
                                    riko.husin

                                    Can I do like this :

                                       

                                    while(1)
                                        {
                                           do {

                                                                    UART_1_CmdReset();                // Reset Command Buffer
                                                for (i=0;i<2;i++) //repeat command 15 times
                                                  {
                                                    for (i=0;i<3;i++)   
                                                    {
                                                        UART_1_PutChar(sel_card[i]); // Send a character to UART TX port
                                                    }
                                                      Delay50uTimes(1);

                                                 }
                                                   
                                                    c = UART_1_cGetChar();
                                               
                                             }while (c == 0);
                                                byte_length = UART_1_cGetChar();
                                             for (i=0;i<byte_length;i++)
                                                {
                                                    RxdataBuff[i] = UART_1_cGetChar();    // Get a character from UART RX data register
                                                }   
                                            if (RxdataBuff[0] == 0xAB)//check the first character
                                            {
                                             
                                             switch(RxdataBuff[1])            // Check status byte
                                                {
                                                    case 0x04:            // If 0x04 : Operation success
                                                        LCD_clr_line(0); //clear LCD screen
                                                          LCD_clr_line(1);

                                                        LCD_1_PrCString("Card Selected");    // Print "Card selected" on the LCD  ;
                                                        LCD_1_Position(1,0);
                                                        LCD_1_PrCString("SN: ");
                                                        for (i=2;i<6;i++)
                                                        {           
                                                            LCD_1_PrHexByte(RxdataBuff[i]); // Print serial number of the card detected
                                                        }                   
                                                        break;
                                                   
                                                    case 0x02:            // If 0x02 : Operation fail
                                                        LCD_clr_line(0); //clear LCD screen
                                                          LCD_clr_line(1);

                                                        LCD_1_PrCString("Operation failed");    // Print "Card selected" on the LCD  ;
                                                               
                                                        break;
                                       
                                                 }//end switch
                                            }//end if
                                        }
                                     } 

                                    1 2 Previous Next