简体   繁体   English

将原始十六进制通信数据日志处理为可读值

[英]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. 我有一个嵌入式微控制器系统,该系统通过uart与我的计算机进行通信,在这一点上,应该自己发送有效数据。 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). 我正在发送以0x54开始并以0x55结尾的数据包,这些数据包以4个为单位(每个“读取”仪器4个数据包)。 A packet contains two 'identifier' bytes after the 0x54, then a bunch of 6 bytes for data, totaling 10 bytes per packet. 数据包在0x54之后包含两个“标识符”字节,然后是一堆6个字节的数据,每个数据包总共10个字节。 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. 每转换第4个数据包换一条新线将非常有用。 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. 我想用C做到这一点(我比较熟练地编码嵌入式C,并且可以轻松地在类型之间来回转换十六进制值),但是我没有太多的编写C可执行文件的经验。 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++. 我已经安装了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 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. 数据的变化不大,可以肯定地推断出字节,但是以下操作应该可以启动OP。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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