Read/Write sequence of bytes to UART

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

Read/Write sequence of bytes to UART

Jump to solution
1,320 Views
Paleantrop
Contributor III

Hi,

 

I am trying to write and read a sequence of bytes (e.g. : FF FF FF FF) to/from UART using M52259. Basically I have managed to write a program which sends characters and receive. I want to develop this one but, no success using integers. I have attached source files from the project with characters. Maybe someone can give me a hint (I am e beginner ).

 

thank you!

0 Kudos
1 Solution
566 Views
ralu
Contributor III

To send integer you might set

 

 

  response->DATA[0] = 'F';
  response->DATA[1] = 'F';
  response->DATA[2] = 'F';
  response->DATA[3] = 'F'; 

 

 

as integers and on the other side you deserialise this response

 

so

 

int myint=1234567;

for(i=0;i<4;i++)

{

   response->DATA[i]=((myint & (0xFF << (i*8)))>>(i*8));

}

 

 

and on other side when you deserialise

 

 

int myint=0;

for(i=0;i<4;i++)

{

 myint |= (response->DATA[i])<<(i*8);

}

 

 

 

This way code is endian dependless and it only depends on fact that reciver has integers of size 32 bits. 

 

If you know that both sides uses same endianess you can apply union when you serialise/deserialise.

 

http://en.wikipedia.org/wiki/Endianness

 

http://www.wellho.net/resources/ex.php4?item=c209/union.c


View solution in original post

0 Kudos
2 Replies
567 Views
ralu
Contributor III

To send integer you might set

 

 

  response->DATA[0] = 'F';
  response->DATA[1] = 'F';
  response->DATA[2] = 'F';
  response->DATA[3] = 'F'; 

 

 

as integers and on the other side you deserialise this response

 

so

 

int myint=1234567;

for(i=0;i<4;i++)

{

   response->DATA[i]=((myint & (0xFF << (i*8)))>>(i*8));

}

 

 

and on other side when you deserialise

 

 

int myint=0;

for(i=0;i<4;i++)

{

 myint |= (response->DATA[i])<<(i*8);

}

 

 

 

This way code is endian dependless and it only depends on fact that reciver has integers of size 32 bits. 

 

If you know that both sides uses same endianess you can apply union when you serialise/deserialise.

 

http://en.wikipedia.org/wiki/Endianness

 

http://www.wellho.net/resources/ex.php4?item=c209/union.c


0 Kudos
566 Views
Paleantrop
Contributor III

Thank you for the useful information!

0 Kudos