Hello,
it works in devcpp (as c project) but not working in Keil. What is my fault. output must be "12345" but it is not.
typedef union { u8 Reg8[5]; struct { u8 Select; u32 Value32; }Regs; }CreditLoadRegs_t; const char MyArray[]={"12345"}; void MyFonc(char *Buf) { CreditLoadRegs_t *fPtr = (CreditLoadRegs_t *)Buf; printf("%s",fPtr->Reg8); } int main(void) { MyFonc(MyArray); return 0; }
A common way of serializing data is to write code that takes a uint8_t pointer and inserts or retrieves different data types byte-by-byte in a well-defined order.
So if serializing a int32_t, the serialize function may emit it from high-to-low byte. And the deserialize function would extract and join from high-to-low. That means that the byte stream transmitted will be compatible even if the sender and receiver have different byte order.
And it also means that it doesn't matter if the sender and receiver have different rules for padding between fields in a struct.
So you might do:
p = transmit_buffer; p = pack_u32(p,my_struct.my_u32); p = pack_u32(p,my_struct.other_u32); p = pack_u16(p,my_struct.some_u16); p = pack_string(p,my_struct.string,sizeof(my_struct.string)); ...
and on the other side do something like:
p = receive_buffer; p = extract_u32(p,&my_struct.my_u32); p = extract_u32(p,&my_struct.other_u32); p = extract_u16(p,&my_struct.some_u16); p = extract_string(p,my_struct.string,sizeof(my_struct.string)); ...
Or maybe switch parameter/return value and do:
p = receive_buffer; my_struct.my_u32 = extract_u32(&p); my_struct.other_u32 = extract_u16(&p); ...
This is a situation where C++ is very handy, since it can keep track of the size of the transmit buffer and fail if trying to insert more than what fits. Or keep track of the number of available bytes in the receive buffer and fail if the code tries to extract more than was available.
With C, you need additional parameters or global variables to keep track of all the state information when packing/unpacking.
But with explicit serializing, you can get very robust code that works with different architectures, compilers, optimization levels, computer languages etc. The two sides can completely change order and data types of the internal data as long as they both agree on the order/size/type of the individual values stored in the serialized data.
Thank you a lot for sample and information,
I had already known serializing but I hadn't known its technical name as serializing.
I'm using it but usually for big string/data sending/receiving, if I'll send or receive little size data which is also combinations of difference variable type, I'm using packed structs as rx and tx buffer. (Because serializing process is slowing for parsing process) But if there are big data size, I'm using serializing process.
__packed typedef struct{ u8 Reg8; u32 Reg32; float Flt; u16 Reg16; ..... }Comm_t; // sender side Comm_t SenderCommRegs; UartCanUsbSenderFonc(Comm_t *cPtr); UartCanUsbSenderFonc(&SenderCommRegs); // receiver side Comm_t RecvCommRegs; UartCanUsbRecvFonc(Comm_t *cPtr); UartCanUsbRecvFonc(&RecvCommRegs);
in fact, I'm a little confused.
I can test it but if I use __packed keyword also, Can the keil compiler add byte/bytes for padding?
if it is not add, The union must cover struct regs. isn't it?
typedef union{ u8 Areg8[5]; // 5 bytes u8 array __packed struct{ u8 Temp8; // 1 byte u16 Low16; // 2 bytes u16 Hi16; // 2 bytes }Regs; }x_t;
One could presumably use sizeof() and offsetof() to arrive at a conclusion.
I would be careful as it is very easy to get caught with an alignment fault on ARM devices if you are not paying attention.
"Because serializing process is slowing for parsing process"
You'd be surprised at how fast serializing can be. When your web browser retrieves a web page, it gets serialized data. When it processes a JPG image, it's decoding serialized data. When you view a BluRay movie, the player decodes serialized data.
A "file format" is a protocol for how the data should be serialized into the file so writer and reader will be able to both understand the content. All networking happens with serialized data. It's just that in some situations, one layer of serialized data may contain "blobs" of unknown payload, that a different protocol layer will be able to work on.
In the end, you should avoid having transfers or data sharing contain data blobs where the content is raw memory dumps unless the data is known to just be a byte stream. It's a short-term cost/time-saving that tends to give lots of interesting problems.
Ok, I understand, Thanks,