Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 1 > How can I get byte_length from Rx port ?

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



How can I get byte_length from Rx port ?
Moderator:
ARVI

Post Reply
Follow this topic



How can I get byte_length from Rx port ?

bianchi posted on 19 Mar 2013 6:47 PM PST
Top Contributor
168 Forum Posts

Guys,

How can I get byte_length from Rx port ?

Thanks

 




Re: How can I get byte_length from Rx port ?

bianchi posted on 19 Mar 2013 07:08 PM PST
Top Contributor
168 Forum Posts

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
               }   

 



Re: How can I get byte_length from Rx port ?

arvi posted on 19 Mar 2013 09:35 PM PST
Cypress Employee
119 Forum Posts

 

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

}



Re: How can I get byte_length from Rx port ?

bianchi posted on 19 Mar 2013 09:54 PM PST
Top Contributor
168 Forum Posts

How can I detect Rx buffer with content :

AB 04 01 04 00

??



Re: How can I get byte_length from Rx port ?

bianchi posted on 20 Mar 2013 10:00 PM PST
Top Contributor
168 Forum Posts

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
                                    }       



Re: How can I get byte_length from Rx port ?

bianchi posted on 20 Mar 2013 11:36 PM PST
Top Contributor
168 Forum Posts

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......



Re: How can I get byte_length from Rx port ?

EricS posted on 20 Mar 2013 09:27 AM PST
Top Contributor
46 Forum Posts

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


Re: How can I get byte_length from Rx port ?

bianchi posted on 20 Mar 2013 04:07 PM PST
Top Contributor
168 Forum Posts

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
                                    } 



Re: How can I get byte_length from Rx port ?

bianchi posted on 20 Mar 2013 04:09 PM PST
Top Contributor
168 Forum Posts

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

Can you give me example ? thank you

 



Re: How can I get byte_length from Rx port ?

bianchi posted on 20 Mar 2013 06:16 PM PST
Top Contributor
168 Forum Posts

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



Re: How can I get byte_length from Rx port ?

bianchi posted on 20 Mar 2013 06:29 PM PST
Top Contributor
168 Forum Posts

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



Re: How can I get byte_length from Rx port ?

EricS posted on 20 Mar 2013 08:05 PM PST
Top Contributor
46 Forum Posts

 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



Re: How can I get byte_length from Rx port ?

arvi posted on 20 Mar 2013 08:49 PM PST
Cypress Employee
119 Forum Posts

 

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.



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 03:38 PM PST
Top Contributor
168 Forum Posts

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.

 

 



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 05:40 PM PST
Top Contributor
168 Forum Posts

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



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 07:31 PM PST
Top Contributor
168 Forum Posts

from this code,

it's responding to AB 02 01 perfecty,

when I can not display in orderly manner on hex byte ( still raw ), because I can't determine how many bytes to be read

=============
    while(1)
    {
       do {

                                UART_1_CmdReset();                // Reset Command Buffer
           
             
                for (i=0;i<15;i++)   
                {
                    UART_1_PutChar(0x01); // Send a character to UART TX port
                }
                  Delay50uTimes(1);

             
               
                c = UART_1_cGetChar();
           
         }while (c == 0);
           
                        
        if (c == 0x01)//check the card available
        {
         LCD_clr_line(0);
         LCD_clr_line(1);
         LCD_1_Position(0,0);
         LCD_1_PrCString("Card in ");
         UART_1_CmdReset();                // Reset Command Buffer
           for (i=0;i<3;i++)   
                {
                    UART_1_PutChar(sel_card[i]); // Send a character to UART TX port
                }
            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
            }

// Please help on how to read the byte length and parse it ?
            */

                    LCD_1_Position(1,0);
                    LCD_1_PrCString("SN: ");
                    for (i=2;i<6;i++)
                    {           
                        LCD_1_PrHexByte(byte_length); // Print serial number of the card detected
                    }                   
         
        }//end if 0x01
    }//end while



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 07:38 PM PST
Top Contributor
168 Forum Posts

I try this one and returning some bytes, but not sure if it's right or not

while(1)
    {
       do {

                                UART_1_CmdReset();                // Reset Command Buffer
           
             
                for (i=0;i<15;i++)   
                {
                    UART_1_PutChar(0x01); // Send a character to UART TX port
                }
                  Delay50uTimes(1);

             
               
                c = UART_1_cGetChar();
           
         }while (c == 0);
           
                        
        if (c == 0x01)//check the card available
        {
         LCD_clr_line(0);
         LCD_clr_line(1);
         LCD_1_Position(0,0);
         LCD_1_PrCString("Card in ");
         UART_1_CmdReset();                // Reset Command Buffer
           for (i=0;i<3;i++)   
                {
                    UART_1_PutChar(sel_card[i]); // Send a character to UART TX port
                }
               
                  if(!UART_1_bCmdCheck() )
                  {
                      byte_length = UART_1_bCmdLength();
                  }

           
           
            for (i=0;i<byte_length;i++)
            {
                RxdataBuff[i] = UART_1_cGetChar();    // Get a character from UART RX data register
            }
           
                    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
                    }                   
         
        }//end if 0x01
    }//end while
 }//end main



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 08:02 PM PST
Top Contributor
168 Forum Posts

what shoud I put for command terminator ? I can't see what's the end of the Rx...??

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.



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 08:12 PM PST
Top Contributor
168 Forum Posts

if the return is : AB 04 01 04 00  ......what's the command terminator ?

00 ??



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 08:41 PM PST
Top Contributor
168 Forum Posts

Please have a look on the video :

http://s129.photobucket.com/user/picture_77/media/RFID_zps0aef3a5d.mp4.html

I used :

      byte_length = UART_1_cReadChar();
            

The code :

while(1)
    {
       do {

                                UART_1_CmdReset();                // Reset Command Buffer
           
             
                for (i=0;i<15;i++)   
                {
                    UART_1_PutChar(0x01); // Send a character to UART TX port
                }
                  Delay50uTimes(1);

             
               
                c = UART_1_cGetChar();
           
         }while (c == 0);
           
                        
        if (c == 0x01)//check the card available
        {
         LCD_clr_line(0);
         LCD_clr_line(1);
         LCD_1_Position(0,0);
         LCD_1_PrCString("Card in ");
         UART_1_CmdReset();                // Reset Command Buffer
           for (i=0;i<3;i++)   
                {
                    UART_1_PutChar(sel_card[i]); // Send a character to UART TX port
                }
                /*
                  if(!UART_1_bCmdCheck() )
                  {
                      byte_length = UART_1_bCmdLength();
                  }
                  */
                   byte_length = UART_1_cReadChar();
                 
           
           
            for (i=0;i<byte_length;i++)
            {
                RxdataBuff[i] = UART_1_cGetChar();    // Get a character from UART RX data register
            }
           
                    LCD_1_Position(1,0);
                    LCD_1_PrCString("SN: ");
                    for (i=0;i<4;i++)
                    {           
                        LCD_1_PrHexByte(RxdataBuff[i]); // Print serial number of the card detected
                    }                   
         
        }//end if 0x01
          else
         if (c == 0xFE)
         {
           LCD_clr_line(0);
           LCD_clr_line(1);
           LCD_1_Position(1,0);
           LCD_1_PrCString("No Card! ");

         }//end 0xFE
    }//end while
 }//end main



Re: How can I get byte_length from Rx port ?

bianchi posted on 21 Mar 2013 09:31 PM PST
Top Contributor
168 Forum Posts

The respond description :

 



Re: How can I get byte_length from Rx port ?

EricS posted on 22 Mar 2013 08:14 AM PST
Top Contributor
46 Forum Posts

In the protocol you use there is no command terminator, so you can't use the command buffer.



Re: How can I get byte_length from Rx port ?

bianchi posted on 25 Mar 2013 04:57 PM PST
Top Contributor
168 Forum Posts

I tested with serial port and RS232 in computer

I send AB 02 01 and the response is AB 04 01 04 00

but in cypress I get a different response it's not AB 04 01 04 00.....why is that ????????????????????????






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20