本文介绍了如何读取十六进制数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我有一个通过蓝牙进行通讯的脉搏血氧饱和度测定仪
我的c ++程序可以通过COM3将其连接,并尝试检索其数据(脉冲率和饱和氧气).
但是设备的数据格式混乱.这是数据格式的说明:

9560包含6个字节的标题信息,最少14个字节的抽查数据和2个字节的页脚信息.

为了确定可扩展Spot-check数据的总长度,主机必须从标头的字节5和6中捕获数据长度.

在最小数据长度为14的情况下,字节5和6中定义的数据长度将为
(0x00)(0x0E)(十进制14字节).

抽查数据由??组成.抽查时间,SpO2,脉搏频率和状态.

Hello
I have a pulse oximetry device which communicate via bluetooth
my c++ program can connect it via COM3, and tries to retrieve its data(pulse rate and oxygen sat.)
but the data format of device is confused. here is the remark of data format:

The 9560 includes 6 bytes of header information, a minimum of 14 bytes of spot-check data, and 2 bytes of footer information.

To determine the total length for the expandable Spot-check data, the host must capture the data length from bytes 5 and 6 of the header.

With the minimum data length of 14, the data length defined in bytes 5 and 6 will be
(0x00) (0x0E) (14 bytes decimal).

The Spot-check data consists of – time of spot-check, SpO2, Pulse Rate, and status.

            Byte#  Data               Information                        Format
            1       00                 NULL start                         Hex
            2       02                 STX – start of pocket               Hex
Header      3       00                 Packet type MSB                    Hex
            4       0D                 Packet type LSB                    Hex
            5       00                 *Data Length MSB (variable)        Hex
            6       0E                 *Data Length LSB (variable)        Hex

            7       20                 Hundredths place of Year           BCD
            8  Year of Measurement     Year of Measurement                BCD
            9  Month of Measurement    Month of Measurement               BCD
            10 Day of Measurement      Day of Measurement                 BCD
            11 Hour of Measurement     Hour of Measurement (00-23)        BCD
expandable  12 Minute of Measurement   Minute of Measurement (00-59)      BCD
Spot-check  13 Second of Measurement   Second of Measurement (00-59)      BCD
data        14 00                      Fraction of second                 BCD
            15 STATUS MSB              See STATUS specification below     Hex
            16 STATUS LSB              See STATUS specification below     Hex
            17 Pulse Rate MSB          See HR format below                Hex
            18 Pulse Rate LSB          See HR format below                Hex
            19 00 to FF                Reserved for future use            Hex
            20 SpO2                    See SpO2 format below              Hex

footer      21 Checksum LSB           LSB of sum Footer of Spot-check Data    Hex
            22 03                     ETX – end of transmission             Hex



我的代码的一部分:



part of my code:

        char buf[22];

DWORD read = 0;


if (ReadFile(comport,&buf,22,&read,NULL)){

    DWORD i;

    for(i=0;i<read;i++){
        printf(" %d\n", (unsigned char)buf[i]);
    }
    printf("\n");

}else {printf("cannot read...");}



我得到这样的输出:
1 128 112 0 241 1 128 112 98 83 1 128 111 147 131 1 128 111 0 240 1 128

您能帮我如何以正确的格式读取和打印数据吗?
谢谢

我用以下命令打开端口:



I get output like this:
1 128 112 0 241 1 128 112 98 83 1 128 111 147 131 1 128 111 0 240 1 128

Could you please help me how to read and print data in correct format
thanks

i open the port with:

if ((comport = CreateFile("\\\\.\\COM3",                // open com3:
                GENERIC_READ | GENERIC_WRITE, // for reading and writing
                0,                            // exclusive access
                NULL,                         // no security attributes
                OPEN_EXISTING,
                0,
                0)) == INVALID_HANDLE_VALUE)
{
    printf("cannot open the port\n");
}

推荐答案



unsigned char buf[22];
DWORD read = 0;

if (ReadFile(comport, buf, 22, &read, NULL))
{
    DWORD i;
    for(i=0; i < read; i++)
    {
        printf(" %X", buf[i]);
    }
    printf("\n");
}
else
{
    DWORD dwError = GetLastError();
    printf("Error %d\n", dwError);
}


如果您的阅读失败,您应该能够看到原因.


If your read fails you should be able to see the reason why.


这篇关于如何读取十六进制数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:16