Electronics > Microcontrollers
sending data to UART in C
Tony R:
ok, I never had to do this before and i want to know what the best way to do this is.
i have a struct like
--- Code: ---struct status
{
uint16_t heading;
uint8_t speed;
uint8_t battery;
uint8_t event;
uint8_t headlights;
};
--- End code ---
what I want to do is use a UART connection to send this data. first two bytes heading and the remaining bytes speed battery event and headlights in that order.
The function prototype for the UART is this:
--- Code: ---void UARTSend( uint32_t portNum, uint8_t *BufferPtr, uint32_t Length );
--- End code ---
now I would like a simple way of just sending the struct, i know i will have to use pointers but i am not sure how to use them in this case.
instead i have done the following.
--- Code: ---void transmit(struct status stat)
{
uint8_t data[6];
data[0] = stat.heading & 0x00FF;
data[1] = (stat.heading & 0xFF00)>>8;
data[2] = stat.speed;
data[3] = stat.battery;
data[3] = stat.event;
data[4] = stat.headlights;
UARTSend( 0, data, sizeof(data));
}
--- End code ---
Does anyone know if a cleaner way to do this?
enz:
Hello Tony,
just skip the skip the unnecessary copy action and add a cast to a pointer to uint8_t for the address of the the struct:
void transmit(struct status stat)
{
UARTSend( 0, (uint8_t *) stat, sizeof(struct status));
}
Regards, Martin
Tony R:
--- Quote from: enz on November 16, 2011, 04:31:53 pm ---Hello Tony,
just skip the skip the unnecessary copy action and add a cast to a pointer to uint8_t for the address of the the struct:
void transmit(struct status stat)
{
UARTSend( 0, (uint8_t *) stat, sizeof(struct status));
}
Regards, Martin
--- End quote ---
Tried that but it says "invalid type conversion" error number 171
I am using the Keil IDE for the ARM M3 LPC1768
Tony R:
--- Quote from: Tony R on November 16, 2011, 04:40:11 pm ---
--- Quote from: enz on November 16, 2011, 04:31:53 pm ---Hello Tony,
just skip the skip the unnecessary copy action and add a cast to a pointer to uint8_t for the address of the the struct:
void transmit(struct status stat)
{
UARTSend( 0, (uint8_t *) stat, sizeof(struct status));
}
Regards, Martin
--- End quote ---
Tried that but it says "invalid type conversion" error number 171
I am using the Keil IDE for the ARM M3 LPC1768
--- End quote ---
but this seems to work:
--- Code: ---void transmit(struct status *stat)
{
UARTSend( 0, (uint8_t *) stat, sizeof(struct status));
}
--- End code ---
Thanks for your help!
baljemmett:
--- Quote from: Tony R on November 16, 2011, 04:40:11 pm ---
--- Quote from: enz on November 16, 2011, 04:31:53 pm --- UARTSend( 0, (uint8_t *) stat, sizeof(struct status));
--- End quote ---
Tried that but it says "invalid type conversion" error number 171
--- End quote ---
You need to take a pointer to the structure and cast that; you can't cast the struct itself:
--- Code: ---void transmit(struct status stat)
{
UARTSend( 0, (uint8_t *) &stat, sizeof(stat));
}
--- End code ---
Although do be aware that this doesn't guarantee the data format is the same as the routine you had originally. The two main things to look out are endianness (your two-byte heading field might get transmitted high-byte-first or low-byte-first depending on architecture) and structure padding (junk bytes between structure elements used to keep things in alignment, although I believe in this case you'll be OK since the natural alignment of the elements shouldn't require any padding).
In general it could be considered better practice to serialise the data into a known format for transmission (as you are currently doing), so that you don't need to worry about the wire protocol being tied to the struct's exact memory layout. At least, that's the case in the world of full-blown computers and software; in the embedded space it's probably less of a worry, but I'm a bit of a neophyte there so could be wrong.
Navigation
[0] Message Index
[#] Next page
Go to full version