vc++ 6.0 串口通讯 [英] vc++ 6.0 serial communicaton

查看:61
本文介绍了vc++ 6.0 串口通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 vc++ 中,我使用 MScomm 进行串行通信,我收到这种格式的数据 02120812550006050.0,我不知道如何阅读它,它是哪种格式,开始帧和结束文件,我不知道.

In vc++ i am using MScomm for serial communication, i received data in this format 02120812550006050.0, i am not gettng how to read this ,in which format it is, begning starting frame and at the end ending file, remaing i dont know.

编辑 1:

它包含日期时间和数据我怎么能分开这个

it contains date time and data how i can seperate this one

推荐答案

有趣的字符是表示记录开始、记录结束、字段分隔符等内容的标记.在不知道实际协议的情况下,有点难以判断.

The funny characters are markers indicating things like record start, record end, field separator and so on. Without knowing the actual protocol, it's a little hard to tell.

数据容易多了.

在 000f 和 0002 标记之间,您有一个日期/时间字段,2008 年 12 月 2 日,12:55:00.

Between the 000f and 0002 markers you have a date/time field, 2nd of December 2008, 12:55:00.

在 0002 和 0003 标记之间,它看起来像一个简单的浮点数,它可能是美元价值或任何真正的东西,这取决于链接另一端的内容.

Between 0002 and 0003 marker, it looks like a simple float which could be a dollar value or anytrhing really, it depends on what's at the other end of the link.

为了将其分开,我假设您已将其读入某种可变字符数组.只需查找标记并提取它们之间的字段即可.

To separate it, I'm assuming you've read it into a variable character array of some sort. Just look for the markers and extract the fields in between them.

日期/时间是固定大小,值也可能是(因为它有一个前导 0),所以你可能只使用 memcpy 从缓冲区中提取你需要的信息,null 终止它们,转换值到一个浮点数,瞧.

The date/time is fixed size and the value probably is as well (since it has a leading 0), so you could probably just use memcpy's to pull out the information you need from the buffer, null terminate them, convert the value to a float, and voila.

如果是固定格式,你可以使用类似的东西:

If it is fixed format, you can use something like:

static void extract (char *buff, char *date, char *time, float *val) {
    // format is "\x01\x0fDDMMYYhhmmss\x02vvvvvvv\x03\x04"
    char temp[8];
    memcpy (date, buff +  2, 6); date[6] = '\0';
    memcpy (time, buff +  8, 6); time[6] = '\0';
    memcpy (temp, buff + 15, 7); temp[7] = '\0';
    *val = atof (temp);
}

并调用它:

char buff[26]; // must be pre-filled before calling extract()
char dt[8];
char tm[8];
float val;
extract (buffer, dt, tm, &val);

如果不是固定格式,您只需要编写代码来检测字段分隔符的位置并提取它们之间的内容.

If not fixed format, you just have to write code to detect the positions of the field separators and extract what's between them.

这篇关于vc++ 6.0 串口通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆