Read/Write sequence of bytes to UART

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

Read/Write sequence of bytes to UART

ソリューションへジャンプ
2,401件の閲覧回数
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,647件の閲覧回数
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,648件の閲覧回数
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,647件の閲覧回数
Paleantrop
Contributor III

Thank you for the useful information!

0 件の賞賛
返信