Re: sending /receiving data through UART

Started by November 26, 2009

You are experiencing the endian problem between the two processors.
Do not send a structure. Define fields and "copy: each field into the message and out of the message. Another problem may be due to the way your compiler may actually allocate structure s_x. It may not pack them as expected.


>I need to send / receive binary data to / from PC through UART.
>The problem is that the data is a struct containing asci (char) information as well as "short int" "long int" ...
>for example
>struct s_x
> char x[6];
> short y;
> long z;
>char * x_msg = &s_x;
>for (int i=0;i < sizeof(s_x);i++)
> send(s_x[i]); // this routine send byte through uart.
>There are three problems:
>1. The size of the message is not the same as in PC.
>2. The "short" here is 1 byte, and in PC it is 2 bytes.
>3. The long z is aligned so ther is a hole in the middle (which on PC I solve with pragma pack)
>can anyone tell me how to solve this problem? I can solve it for each message apart but is there a general solution ?