简体   繁体   中英

processing a raw hex communication data log into readable values

I have an embedded microcontroller system that is communicating to my computer via uart, and at this point of development is should be sending out valid data on its own. I have triggered a 'scan' of this device, its role then is to send out data it's read to the host device autonomously. I now have a text file with raw hex data values in there, ready to be processed. I'm sending out packets that start with 0x54 and end in 0x55 and they come in lots of 4 (4 packets per single 'read' of the instrument). A packet contains two 'identifier' bytes after the 0x54, then a bunch of 6 bytes for data, totaling 10 bytes per packet. depending on the packet identifiers, the data within could be a floating point number or an integer.

I basically want to to design a command line program that takes in a raw text file with all this data in it, and outputs a text file, comma seperated between packets, with the data converted to its readable decimal counterpart. a new line for every 4th converted packet would be very useful. I'd like to do this in C (I am relatively proficient in coding embedded C, and I can convert the hex values back and forth between types easily enough) but I do not have much experience in writing C executables. Any help with getting started on this little mini project would be awesome. I need to know how to create a command line controllable executable, read in a data file, manipulate the data (which I think I can do now) and then export the data to another file. I've installed netbeans c/c++. Pointers in the right direction are all I require (no pun intended =] )

Here's the raw data file:

http://pastebin.com/dx4HetT0

There is not much variation in the data to deduce the bytes with certainty, but the following should get the OP started.

void db_parse(FILE *outf, const unsigned char *s) {
  fprintf(outf, "%c", *s++);
  fprintf(outf, " %3u", *((uint8_t *) s));
  s += sizeof(uint8_t);
  uint8_t id1 = *((uint8_t *) s);
  fprintf(outf, " %3u", *((uint8_t *) s));
  s += sizeof(uint8_t);
  if (id1 >= 3) {
    fprintf(outf, " %13e", *((float *) s));
    s += sizeof(float);
  } else {
    fprintf(outf, " %13u", *((uint32_t *) s));
    s += sizeof(uint32_t);
  }
  fprintf(outf, " %5u", *((uint16_t *) s));
  s += sizeof(uint16_t);
  fprintf(outf, " %c\n", *s);
}

// Test code below

const char *h4 =
    "54 12 04 00 00 40 C0 00 00 55 54 12 01 02 00 00 00 00 00 55 54 12 02 03 00 00 00 00 00 55 54 12 03 00 00 40 C0 00 00 55 ";

void db_test() {
  unsigned char uc[10];
  const char *s = h4;
  while (*s) {
    for (int i = 0; i < 10; i++) {
      unsigned x;
      sscanf(s, "%x", &x);
      uc[i] = x;
      s += 3;
    }
    db_parse(stdout, uc);
  }
}

Output

T  18   4 -3.000000e+00     0 U
T  18   1             2     0 U
T  18   2             3     0 U
T  18   3 -3.000000e+00     0 U

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM