I’m working on a project where I need to communicate with a device via the TCP/IP-protocol.
The device sends an large amount of data which I somehow want to parse into some objects/structs.
Datapackage example (in the TCP buffer[]):
[64] [1] [78] [244] [77] [189] [249] [149] hcurrent
[64] [1] [78] [247] [89] [95] [104] [85] htarget
[0] [0] [0] [0] [0] [0] [0] [0] qcurrent
[188] [220] [97] [3] [66] [62] [0] [0] kcurrent
[66] [0] [102] [103] [66] [99][153] [154] mcurrent
[253] [191] [246] [74] [170] [216] [242] [29] fmode
[102] [191] [246] [74] [178] [44] [92] [72] tmil
[137] mode
Now this package frame is identified as:
double hcurrent
double htarget
double qcurrent
float kcurrent
float mcurrent
float fmode
float tmil
unsigned char mode
My idea was that I somehow could parse the data directly into a struct which had the same structure as above.
Of course it will be neccessary to identify some key values to determine which kind of data it is.
How can this be done?
Since I’m coding for an iOS device it has to be objective-C or C(++).
EDIT (method tested for copying each part of the datagram into struct):
Small Java implementation where i try to read the first 4 bytes [0] [0] [1] [5]:
byte[] read = new byte[4];
int length = 0;
while (length < read.length) {
len = iStream.read(read, len, read.length);
}
int ByteLength = (int)unsignedIntToLong(read);
ByteLength = ByteLength-5;
state = 1; // Continue and work with next data.
And the bit manipulation method:
public long unsignedIntToLong(byte[] b)
{
long l = 0;
l |= b[0] & 0xFF;
l <<= 8;
l |= b[1] & 0xFF;
l <<= 8;
l |= b[2] & 0xFF;
l <<= 8;
l |= b[3] & 0xFF;
return l;
}
So i fetch the first 4 bytes i mentioned earlier which determines something specific, and in the end an find the length of 465. My plan is to repeat this process with all other parts of the received data.
The biggest problem you’re going to have is that structs don’t store data in a completely contiguous form, they align data acording to word boundaries
This means that you can’t simply define a struct and then cast your buffer[] to it if the buffer didn’t contain a struct to begin with. Instead what you probably need to do is declare a struct and then memcpy each part of the buffer[] in one field at a time using apointer offset into the buffer[].
If this approach is too cumbersome, it’s often possible to turn off structure alignment so that a structure can represent completely packed data. MSVC allows the use of #pragma pack to do this. This approach does however slow memory access to the structure.
EDIT: Here’s an example which shows how you can use a template function to read any type from a buffer and then update an offset into that buffer. You can use this method to safely parse any number of types into a structure one by one: