cbytepayload

Split payload data from sensor into separate bytes length


Please I'm having an issue where I need to split a payload coming from an energy meter into chunks of 5-bytes each that represent different values of line parameters. The payload length is 35 bytes as shown below.

{"01000008c502000008e90300000913040000001305000000310600000013070000001b0800000022090000006d0a000000040b000000940c000000290d0000006d0e0000002b0f00000096100000033511000003e6120000006513000003d91400000713150000137d1600000005";}

I would like it printed out this way after running the code.

phase A voltage: 01000008c5
phase B voltage: 02000008e9
phase C voltage: 0300000913 
phase A current: 0400000013 
phase B current: 0500000031 
phase C current: 0600000013
powerfactor: 070000001b
active power A: 0800000022
active power B: 090000006d
active power C: 0a00000004
Apparent power A :0b00000094
Apparent power B: 0c00000029
Apparent power C: 0d0000006d

If anyone can assist with a sample code or point me in the right direction on how i can split this packet coming from a sensor into 5-bytes each and print it on a compiler. I just want to copy the packet from the sensor, paste it into the program and run. Using an array will require adding commas manually to separate the bytes which I don't want to do.


    #include <stdio.h>
    #include <string.h>
    
    void split_payload(const char *payload_hex, size_t chunk_bytes) 
    {
        // Each byte is represented by two hexadecimal characters.
        size_t chunk_chars = chunk_bytes * 2;
        size_t payload_len = strlen(payload_hex);
        
        printf("Splitting the payload into %zu-byte chunks (chunks of %zu characters):\n\n", chunk_bytes, chunk_chars);
    
        for (size_t i = 0; i < payload_len; i += chunk_chars) 
        {
            // Use a loop to print each character of the chunk
            for (size_t j = 0; j < chunk_chars; ++j) 
            {
                if (i + j < payload_len) 
                {
                    printf("%c", payload_hex[i + j]);
                }
            }
            printf("\n");
        }
    }
    int main() 
    {
        const char *payload_hex = "01000008c502000008e90300000913040000001305000000310600000013070000001b0800000022090000006d0a000000040b000000940c000000290d0000006d0e0000002b0f00000096100000033511000003e6120000006513000003d91400000713150000137d1600000005";
        size_t bytes_per_chunk = 5;
        split_payload(payload_hex, bytes_per_chunk);
        return 0;
    }

Solution

  • You want this:

    ...
    const char* labels[] =
    { "phase A voltage:", "phase B voltage:", "phase C voltage:",
      "phase A current:", "phase B current:", "phase C current:",
      "active power A:", "active power B:", "active power C:",
      "Apparent power A:", "Apparent power B:", "Apparent power C:",
      "Reactive power A:", "Reactive power B:", "Reactive power C:",
      "Whatever 1:", "Whatever 2:", "Whatever 3:",
      "Whatever 4:", "Whatever 5:", "Whatever 6:", "Whatever 7:"
    };
    ...
    
     ...
     for (size_t i = 0; i < payload_len; i += chunk_chars)
      {
        printf("%s ", labels[i / chunk_chars]);  // <<< add this to your code
        // Use a loop to print each character of the chunk
        ...
    

    The value i / chunk_chars varies from 0 to payload_len / chunk_chars by increments of 1.

    So the first the loop is executed, it is 0, the second time it's 1 etc.

    const char* labels[] = ... declares an array containing (roughly speaking) the different labels. labels[0] is the first label, labels[1] is the second label etc.