Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing data into struct

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.

like image 323
JavaCake Avatar asked Jun 11 '26 00:06

JavaCake


2 Answers

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:

// We want to copy raw data to this structure
// but the short will cause it to be unaligned
struct _parsed_structure
{
    int a;
    int b;
    short c;
    int d;
} parsed_structure;

template<typename T>
void read_and_update_offset (int & offset, char * buffer, T & var)
{
    T * pInt = (T*)(buffer + offset);
    var = *pInt;
    offset += sizeof(T);
};

int _tmain(int argc, _TCHAR* argv[])
{
    // Here's a buffer which we know contains ints and shorts, we could just cast it to our structure
    // but this will cause errors because the structure will not be aligned properly.
    char buffer[] = { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0 };

    // Read the first int from the buffer into the structure
    int offset = 0;
    read_and_update_offset(offset, buffer, parsed_structure.a);
    read_and_update_offset(offset, buffer, parsed_structure.b);
    read_and_update_offset(offset, buffer, parsed_structure.c);
    read_and_update_offset(offset, buffer, parsed_structure.d);

    // Print the values
    std::cout << 
        parsed_structure.a << " " <<
        parsed_structure.b << " " <<
        parsed_structure.c << " " <<
        parsed_structure.d << " " << std::endl;

    // Look the size of our structure is different than the size of our buffer due to alignment
    std::cout <<
        "sizeof(buffer)" << "==" << sizeof(buffer) << " " <<
        "sizeof(parsed_structure)" << "==" << sizeof(parsed_structure) << std::endl;

    return 0;
}
like image 133
Benj Avatar answered Jun 13 '26 12:06

Benj


The usual approach to something like this is to write methods nextInt, nextDouble, etc that will read bytes from the stream (in proper "endian" order) and return a value of the indicated type, updating a pointer or index into the array as they do. This is much more manageable than trying to in-line the conversions, and can be quite efficient. (You can make the methods C++ vs Objective-C for efficiency.)

like image 29
Hot Licks Avatar answered Jun 13 '26 14:06

Hot Licks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!