Extra bytes received at RX buffer of UART

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Extra bytes received at RX buffer of UART

2,334件の閲覧回数
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

ラベル(1)
0 件の賞賛
返信
4 返答(返信)

1,472件の閲覧回数
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 件の賞賛
返信

1,472件の閲覧回数
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 件の賞賛
返信

1,472件の閲覧回数
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 件の賞賛
返信

1,472件の閲覧回数
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 件の賞賛
返信