If you can choose the packet format there is a fourth way, using one byte in the packet to signal the size of the packet itself. One packet format I use always I can is: ... - Marker can be a byte with any value you want, preferably one byte rare or not occurring in your data at all. I often use 0xab, 0xe3, etc. - Length may be one or two bytes, depending on your maximum packet length. - Checking code can be anything from a simple LRC (xor), a checksum or a good CRC32 and should be calculated over everything else in the packet. At first your receiving device is not synchronized, so it ignores any byte different to the marker. Once you receive the marker, you look for the length, and if it is a plausible value you start collecting the data. If not, just start over. You also start calculating your checking code. It is simpler to calculate it as each byte is received than doing it just in the end. When you receive the transmitted checking code and it agrees with the one you calculated, you know your packed was received OK. If the checking code is not OK, start over. Here a good inter-packet delay may be good to help synchronize, but it is not mandatory. Best regards, Isaac Em 02/02/2014 15:28, Jan-Erik Soderholm escreveu: > There are basicly three methods: > > 1. > You have some distinct char/pattern in the stream that signals end-of-dat= a. > > 2. > You have a timeout (no data on the line) that singnals end-of-data. > > 3. > Your data has always a fixed lenght. > > Pick what works for you. > > The timeout counter should be reset/zeroed in the UART-ISR and > the "counting" should be done elsewhere (in main or in another > timer-ISR). > > Jan-Erik. > > > > Andre Abelian wrote 2014-02-02 18:50: >> Hi All, >> >> I am having hard time to determine the length of uart RX data. the data= length is unknown it is between (100-125 byte) and it is less then 128 byt= es. >> in my routine RX ISR clears the RX flag and saves the byte came in and i= ncreases the counter. data_available is not used for now >> >> void __attribute__((interrupt, no_auto_psv)) _U2RXInterrupt(void) { >> >> uint8_t data_size_U2; >> >> IFS1bits.U2RXIF =3D 0; >> if (U2STAbits.URXDA =3D=3D 1) >> { >> U2_RX[new_counter] =3D U2RXREG; // save incoming byte >> >> counter++; // increment >> data_available =3D 1; >> GLED_ON; >> } >> >> } >> >> >> the counter contains total number of bytes but the question is how to ch= eck when no more data is coming in? to use the counter. >> I was testing with IDEL flag U2STAbits.RIDLE I can see when uart RX = is active the flag is set to 0. now the problem is >> even on first receive byte the flag will indicate as busy. so just using= when it is busy is not going to work. >> in my case I do not mind to deal with fixed 128 byte every time I alread= y know the data is some where between >> 100-125 bytes. the only issue is when there is no data coming in the int= errupt will not occur and counting extra is not going to work. >> the counter in ISR knows exact length of bytes came in but I do not k= now when to check the counter since I do not know the length of data. >> i am not sure if I explain clearly. the counter and U2_RX are global var= iables. >> >> any idea or suggestion will appreciate. >> >> thanks >> >> Andre >> --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .