Read/Write sequence of bytes to UART

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Read/Write sequence of bytes to UART

跳至解决方案
2,398 次查看
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 项奖励
回复
1 解答
1,644 次查看
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 项奖励
回复
2 回复数
1,645 次查看
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 项奖励
回复
1,644 次查看
Paleantrop
Contributor III

Thank you for the useful information!

0 项奖励
回复