Extra bytes received at RX buffer of UART

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Extra bytes received at RX buffer of UART

2,337 Views
netra
Contributor IV

Hi,

We have mcf52259 Controller based hardware board and MQX 3.5 OS, which is communicating with external device on RS 485 (MODBUS RTU Protocol).

We have established communication between this device.

we found during reception of data from the external device

one or two extra bytes are getting added to the beginning of the frame sent by that device.

We had debugged and concluded that the bytes are getting added inside UART.

We used Scope and concluded that the additional bytes are not added till the RX pin of the Controller.

Please do find the packets send and received ( Bold ones are the extra bytes).

Note: This behavior is not consistent and sometimes we are getting correct response continuously.

    

TX:- 01 03 08 34 00 01 c7 a4

RX:-  01 03 02 00 c8 b9 d2

TX:-01 03 03 e8 00 06 45 b8

RX:-  5c 01 03 0c 00 40 00 00 00 00 a5 a5 a5 a5 2c ec bd ff

TX:- 01 03 03 e8 00 06 45 b8

RX:-  01 03 0c 00 40 00 00 00 00 a5 a5 a5 a5 2c ec bd ff

TX:- 01 03 04 b0 00 01 84 dd

RX:-  fb 01 03 02 00 03 f8 45

TX:- 01 03 04 b0 00 01 84 dd

RX:-  01 03 02 00 03 f8 45

Thanks in advance ,Please reply

Nisha

Labels (1)
0 Kudos
Reply
4 Replies

1,475 Views
chandrasekarkan
Contributor III

Hi Nisha

It looks like 5c is a part of " 01 03 02 00 c8 b9 d2", same way is the byte fb. Use interrupt mode ("ittyx:") and wait till a timeout(say 50 or 100ms or more) to

get expected number of bytes. You can use "fflush" before sending the next query.

0 Kudos
Reply

1,475 Views
netra
Contributor IV

Hi,

Thanks for your reply

It is already in interrupt mode.

I have attached flushing code for  receive buffer 

   bool  bytesPresent = TRUE ;    /* To avoid blocking */

    char    pBuffer;

    unsigned int length = 0;

   

  

        while(bytesPresent)

        {

            if( MQX_OK == ioctl(comPort->handle, IO_IOCTL_CHAR_AVAIL ,&bytesPresent))

            {

                if( bytesPresent )

                {

                    length += fread(&pBuffer, SINGLE_BYTE_ELEMENTS, 1, comPort->handle);

          

                }

                       

            }

            else

            {

                bytesPresent = FALSE;

            }

        }

   

   

   

memset(&pbuffer,0,lenght);

    return(length);

I am using fflush also before sending query still  i am getting that extra bit.

Thanks in advance,

Nisha

0 Kudos
Reply

1,475 Views
chandrasekarkan
Contributor III

Hi

Try this code:

//global variable

char receiveBuffer[256];

// Function call

readResponse( receiveBuffer, 10 );

int readResponse( portHandle *comPort,  char *buffer, int expectedNumOfBytes )

{

  int    retries=0, bytesRead=0, ret=0;

  retries = 3;

  bytesRead=0;

  while( retries != 0 )

  {

          if(  (ret=fread(&buffer[bytesRead], SINGLE_BYTE_ELEMENTS, 256, comPort->handle) ) > 0  )

          {

               bytesRead += ret;

               if( bytesRead >= 256 )    // to take care of any memory violation

                     return bytesRead;

               if( bytesRead >= expectedNumOfBytes )

               {

                    break;

               }

          }

          else

          {

               _time_delay(100);     // in ms

               --retries;

          }

  }

   if( retries == 0 )

          return -1;     // No Rx bytes till the timeout of 300ms

     return bytesRead;

}

Increase or decrease the time delay or retries...

0 Kudos
Reply

1,475 Views
stevejanisch
Contributor IV

I'm no expert -- and not using your device or your version of MQX...

But I also noted odd extra bytes before I changed the default MQX serial from polled to interrupt mode.

0 Kudos
Reply