Your original code should work.
In cases like this, I typically use a low-level function to isolate the user code from the actual protocol, like this:
//send a byte with a protocol
void send_byte(unsigned char dat) {
//implement your protocol here
}
//send a word with send_byte()
void send_word(unsigned short wrd) {
unsigned char i=sizeof(wrd);
unsigned char *ptr=(unsigned char *) &wrd;
while (i--) send_byte(*ptr++);
}
//send a float with send_byte()
void send_float(float flt) {
unsigned char i=sizeof(flt);
unsigned char *ptr = (unsigned char *) &flt;
while (i--) send_byte(*ptr++);
}
You can pretty much expand it to cover any other data types.
It has the advantage of providing some stability to your code: if you wish to change the protocol through which the data is sent, you just change send_byte(), as everything else is built on top of it.