Colin Green wrote:
Hi this is for Serial Com.. On the 'send side'. I will be sending, for
example "how are you?". It will be then coupled with the Header
Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
what is actually sent is 34 486F772061726520796F753F 0C.
My questions are to write them in C
1) How to strip the Header and Checksum from the actual message?
2) How to convert the 486F772061726520796F753F into ASCII again? (that
means on the receiving end I should see "how are you?"
I need to store them on the buffer
Any code that woudl help will be appreciated
yours
Green
Try using a union:
union
{
unsigned char data[13];
struct
{
unsigned char header;
char message[11];
unsigned char checksum;
} values;
} packet;
On the send end:
packet.values.header = 0x34;
packet.values.checksum = 0x0C;
strncpy(packet.values.message, "how are you?", 11);
Then send the packet something like:
write(serialPortHandle, packet.data, 13);
On the receive side, just read like:
read(serialPortHandle, packet.data, 13);
Then extract the values from the union.
char receivedMessage[11];
header = packet.values.header;
header = packet.values.checksum;
strncpy(receivedMessage, packet.values.message, 11);
If you're going to use the receivedMessage as a string, you
need to remember to add a terminating null, with will require
that receivedMessage be at least 1 byte longer than the maximum
size of the message you expect.