C++ 读bin文件,部分代码。赚经验。

编号:1
Head:
magicWord[0] = 0x0102   magicWord[1] = 0x0304   magicWord[2] = 0x0506   magicWord[3] = 0x0708
version = 0x02010004
totalPacketLen = 288
platform = 0x000a1443
frameNumber = 12
timeCpuCycles = 172969774
numDetectedObj = 99
numTLVs = 2
subFrameNumber = 0

TLV_head:
TLV_head type = 0
TLV_head length = 128

VitalSignsDemo_OutputStats:
rangeBinIndexMax = 0
rangeBinIndexPhase = 8
maxVal = 277225536.00
processingCyclesOut = 6916614
rangeBinStartIndex = 8
rangeBinEndIndex = 29
unwrapPhasePeak_mm = 0.49
outputFilterBreathOut = 0.08
outputFilterHeartOut = -0.08
heartRateEst_FFT = 46.88
heartRateEst_FFT_4Hz = 94.92
heartRateEst_xCorr = 133.33
heartRateEst_peakCount_filtered = 0.00
breathingRateEst_FFT = 8.20
breathingRateEst_xCorr = 36.36
breathingRateEst_peakCount = 0.00
confidenceMetricBreathOut = 3.52
confidenceMetricBreathOut_xCorr = 0.00
confidenceMetricHeartOut = 0.00
confidenceMetricHeartOut_4Hz = 0.00
confidenceMetricHeartOut_xCorr = 0.00
sumEnergyBreathWfm = 27.66
sumEnergyHeartWfm = 4.59
motionDetectedFlag = 0.00
breathingRateEst_harmonicEnergy = 0.00
heartRateEst_harmonicEnergy = 0.00
reserved7 = 0.00
reserved8 = 0.00
reserved9 = 0.00
reserved10 = 0.00
reserved11 = 0.00
reserved12 = 0.00
reserved13 = 0.00
reserved14 = 0.00

TLV_head:
TLV_head type = 1
TLV_head length = 88

编号:2
Head:
magicWord[0] = 0x0102   magicWord[1] = 0x0304   magicWord[2] = 0x0506   magicWord[3] = 0x0708
version = 0x02010004
totalPacketLen = 288
platform = 0x000a1443
frameNumber = 60
timeCpuCycles = 652964598
numDetectedObj = 99
numTLVs = 2
subFrameNumber = 0

TLV_head:
TLV_head type = 0
TLV_head length = 128

VitalSignsDemo_OutputStats:
rangeBinIndexMax = 0
rangeBinIndexPhase = 8
maxVal = 35906888.00
processingCyclesOut = 6915730
rangeBinStartIndex = 8
rangeBinEndIndex = 29
unwrapPhasePeak_mm = 5.92
outputFilterBreathOut = 0.12
outputFilterHeartOut = 0.73
heartRateEst_FFT = 71.48
heartRateEst_FFT_4Hz = 124.22
heartRateEst_xCorr = 66.67
heartRateEst_peakCount_filtered = 4.69
breathingRateEst_FFT = 30.47
breathingRateEst_xCorr = 30.77
breathingRateEst_peakCount = 9.38
confidenceMetricBreathOut = 2.05
confidenceMetricBreathOut_xCorr = 0.85
confidenceMetricHeartOut = 0.30
confidenceMetricHeartOut_4Hz = 0.30
confidenceMetricHeartOut_xCorr = 0.76
sumEnergyBreathWfm = 6103.43
sumEnergyHeartWfm = 2001.75
motionDetectedFlag = 1.00
breathingRateEst_harmonicEnergy = 0.00
heartRateEst_harmonicEnergy = 0.00
reserved7 = 0.00
reserved8 = 0.00
reserved9 = 0.00
reserved10 = 0.00
reserved11 = 0.00
reserved12 = 0.00
reserved13 = 0.00
reserved14 = 0.00

TLV_head:
TLV_head type = 1
TLV_head length = 88

编号:3
Head:
magicWord[0] = 0x0102   magicWord[1] = 0x0304   magicWord[2] = 0x0506   magicWord[3] = 0x0708
version = 0x02010004
totalPacketLen = 288
platform = 0x000a1443
frameNumber = 62
timeCpuCycles = 672967133
numDetectedObj = 99
numTLVs = 2
subFrameNumber = 0

TLV_head:
TLV_head type = 0
TLV_head length = 128

VitalSignsDemo_OutputStats:
rangeBinIndexMax = 0
rangeBinIndexPhase = 8
maxVal = 42392180.00
processingCyclesOut = 6919524
rangeBinStartIndex = 8
rangeBinEndIndex = 29
unwrapPhasePeak_mm = 4.92
outputFilterBreathOut = -0.00
outputFilterHeartOut = 1.32
heartRateEst_FFT = 71.48
heartRateEst_FFT_4Hz = 124.22
heartRateEst_xCorr = 66.67
heartRateEst_peakCount_filtered = 4.69
breathingRateEst_FFT = 30.47
breathingRateEst_xCorr = 30.00
breathingRateEst_peakCount = 9.38
confidenceMetricBreathOut = 2.93
confidenceMetricBreathOut_xCorr = 0.85
confidenceMetricHeartOut = 0.30
confidenceMetricHeartOut_4Hz = 0.30
confidenceMetricHeartOut_xCorr = 0.76
sumEnergyBreathWfm = 4945.99
sumEnergyHeartWfm = 3252.34
motionDetectedFlag = 1.00
breathingRateEst_harmonicEnergy = 0.00
heartRateEst_harmonicEnergy = 0.00
reserved7 = 0.00
reserved8 = 0.00
reserved9 = 0.00
reserved10 = 0.00
reserved11 = 0.00
reserved12 = 0.00
reserved13 = 0.00
reserved14 = 0.00

TLV_head:
TLV_head type = 1
TLV_head length = 88

编号:4
Head:
magicWord[0] = 0x0102   magicWord[1] = 0x0304   magicWord[2] = 0x0506   magicWord[3] = 0x0708
version = 0x02010004
totalPacketLen = 288
platform = 0x000a1443
frameNumber = 64
timeCpuCycles = 692964050
numDetectedObj = 99
numTLVs = 2
subFrameNumber = 0

TLV_head:
TLV_head type = 0
TLV_head length = 128

VitalSignsDemo_OutputStats:
rangeBinIndexMax = 0
rangeBinIndexPhase = 8
maxVal = 38704580.00
processingCyclesOut = 6913536
rangeBinStartIndex = 8
rangeBinEndIndex = 29
unwrapPhasePeak_mm = 4.50
outputFilterBreathOut = -0.17
outputFilterHeartOut = 0.55
heartRateEst_FFT = 71.48
heartRateEst_FFT_4Hz = 124.22
heartRateEst_xCorr = 66.67
heartRateEst_peakCount_filtered = 4.69
breathingRateEst_FFT = 29.30
breathingRateEst_xCorr = 30.77
breathingRateEst_peakCount = 9.38
confidenceMetricBreathOut = 2.98
confidenceMetricBreathOut_xCorr = 0.87
confidenceMetricHeartOut = 0.30
confidenceMetricHeartOut_4Hz = 0.30
confidenceMetricHeartOut_xCorr = 0.76
sumEnergyBreathWfm = 4038.94
sumEnergyHeartWfm = 3728.13
motionDetectedFlag = 1.00
breathingRateEst_harmonicEnergy = 0.00
heartRateEst_harmonicEnergy = 0.00
reserved7 = 0.00
reserved8 = 0.00
reserved9 = 0.00
reserved10 = 0.00
reserved11 = 0.00
reserved12 = 0.00
reserved13 = 0.00
reserved14 = 0.00

TLV_head:
TLV_head type = 1
TLV_head length = 88

编号:5
Head:
magicWord[0] = 0x0102   magicWord[1] = 0x0304   magicWord[2] = 0x0506   magicWord[3] = 0x0708
version = 0x02010004
totalPacketLen = 288
platform = 0x000a1443
frameNumber = 66
timeCpuCycles = 712964963
numDetectedObj = 99
numTLVs = 2
subFrameNumber = 0

TLV_head:
TLV_head type = 0
TLV_head length = 128

VitalSignsDemo_OutputStats:
rangeBinIndexMax = 0
rangeBinIndexPhase = 8
maxVal = 230556496.00
processingCyclesOut = 6914486
rangeBinStartIndex = 8
rangeBinEndIndex = 29
unwrapPhasePeak_mm = 4.34
outputFilterBreathOut = -0.38
outputFilterHeartOut = -1.58
heartRateEst_FFT = 71.48
heartRateEst_FFT_4Hz = 124.22
heartRateEst_xCorr = 66.67
heartRateEst_peakCount_filtered = 4.69
breathingRateEst_FFT = 28.12
breathingRateEst_xCorr = 30.00
breathingRateEst_peakCount = 9.38
confidenceMetricBreathOut = 2.57
confidenceMetricBreathOut_xCorr = 0.91
confidenceMetricHeartOut = 0.30
confidenceMetricHeartOut_4Hz = 0.30
confidenceMetricHeartOut_xCorr = 0.76
sumEnergyBreathWfm = 3479.94
sumEnergyHeartWfm = 4708.27
motionDetectedFlag = 1.00
breathingRateEst_harmonicEnergy = 0.00
heartRateEst_harmonicEnergy = 0.00
reserved7 = 0.00
reserved8 = 0.00
reserved9 = 0.00
reserved10 = 0.00
reserved11 = 0.00
reserved12 = 0.00
reserved13 = 0.00
reserved14 = 0.00

TLV_head:
TLV_head type = 1
TLV_head length = 88

共取到了 5个数据

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#define FILENAME "DataOutput_XWR14xx.bin"



typedef struct Header {
    uint16_t    magicWord[4];  

    uint32_t    version;            
    uint32_t    totalPacketLen;      
    uint32_t    platform;            
    uint32_t    frameNumber;         
    uint32_t    timeCpuCycles;       
    uint32_t    numDetectedObj;      
    uint32_t    numTLVs;
    uint32_t    subFrameNumber;
}Header_t;

Header_t head;

typedef struct MmwDemo_output_message_tl_t
{
    //tlv是通讯协议,在bin里这个type一个是0一个是1,
    //用来识别他之后的数据是output还是rangeprofile,
    uint32_t    type;  //0   //1    
    //length告知有多少字节数据
    uint32_t    length;

} MmwDemo_output_message_tl;

MmwDemo_output_message_tl   TLV_head, TLV_head1;


typedef struct VitalSignsDemo_OutputStats_t
{
    uint16_t rangeBinIndexMax;          // 1
    uint16_t rangeBinIndexPhase;        // 1
    float maxVal;                       // 2
    uint32_t processingCyclesOut;       // 3
    uint16_t rangeBinStartIndex;        // 4
    uint16_t rangeBinEndIndex;          // 4
    float unwrapPhasePeak_mm;           // 5
    float outputFilterBreathOut;        // 6
    float outputFilterHeartOut;         // 7
    float heartRateEst_FFT;             // 8心率FFT
    float heartRateEst_FFT_4Hz;         // 9
    float heartRateEst_xCorr;           // 10
    float heartRateEst_peakCount_filtered;  // 11峰值间隔
    float breathingRateEst_FFT;             // 12呼吸FFT
    float breathingRateEst_xCorr;           // 13
    float breathingRateEst_peakCount;       // 14
    float confidenceMetricBreathOut;        // 15呼吸置信度
    float confidenceMetricBreathOut_xCorr;  // 16
    float confidenceMetricHeartOut;         // 17心率置信度
    float confidenceMetricHeartOut_4Hz;     // 18
    float confidenceMetricHeartOut_xCorr;   // 19
    float sumEnergyBreathWfm;               // 20
    float sumEnergyHeartWfm;                // 21
    float motionDetectedFlag;               // 22
    float breathingRateEst_harmonicEnergy;  // 23
    float heartRateEst_harmonicEnergy;      // 24
    float reserved7;  //25
    float reserved8;  //26
    float reserved9;  //27
    float reserved10; //28
    float reserved11; //29
    float reserved12; //30
    float reserved13; //31
    float reserved14; //32
} VitalSignsDemo_OutputStats;

VitalSignsDemo_OutputStats outputStats;

void printOutputStats() {
    printf("\nVitalSignsDemo_OutputStats:\n");
    printf("rangeBinIndexMax = %hd\n", outputStats.rangeBinIndexMax);   //1
    printf("rangeBinIndexPhase = %hd\n", outputStats.rangeBinIndexPhase);  //1
    printf("maxVal = %.2f\n", outputStats.maxVal);  //2
    printf("processingCyclesOut = %d\n", outputStats.processingCyclesOut);  //3
    printf("rangeBinStartIndex = %hd\n", outputStats.rangeBinStartIndex);  //4
    printf("rangeBinEndIndex = %hd\n", outputStats.rangeBinEndIndex);  //4

    printf("unwrapPhasePeak_mm = %.2f\n", outputStats.unwrapPhasePeak_mm);  //5
    printf("outputFilterBreathOut = %.2f\n", outputStats.outputFilterBreathOut);  //6
    printf("outputFilterHeartOut = %.2f\n", outputStats.outputFilterHeartOut);  //7
    printf("heartRateEst_FFT = %.2f\n", outputStats.heartRateEst_FFT);  //8
    printf("heartRateEst_FFT_4Hz = %.2f\n", outputStats.heartRateEst_FFT_4Hz);  //9
    printf("heartRateEst_xCorr = %.2f\n", outputStats.heartRateEst_xCorr);  //10
    printf("heartRateEst_peakCount_filtered = %.2f\n", outputStats.heartRateEst_peakCount_filtered);  //11
    printf("breathingRateEst_FFT = %.2f\n", outputStats.breathingRateEst_FFT);  //12
    printf("breathingRateEst_xCorr = %.2f\n", outputStats.breathingRateEst_xCorr);  //13
    printf("breathingRateEst_peakCount = %.2f\n", outputStats.breathingRateEst_peakCount);  //14
    printf("confidenceMetricBreathOut = %.2f\n", outputStats.confidenceMetricBreathOut);  //15
    printf("confidenceMetricBreathOut_xCorr = %.2f\n", outputStats.confidenceMetricBreathOut_xCorr);  //16
    printf("confidenceMetricHeartOut = %.2f\n", outputStats.confidenceMetricHeartOut);  //17
    printf("confidenceMetricHeartOut_4Hz = %.2f\n", outputStats.confidenceMetricHeartOut_4Hz);  //18
    printf("confidenceMetricHeartOut_xCorr = %.2f\n", outputStats.confidenceMetricHeartOut_xCorr);  //19
    printf("sumEnergyBreathWfm = %.2f\n", outputStats.sumEnergyBreathWfm);  //20
    printf("sumEnergyHeartWfm = %.2f\n", outputStats.sumEnergyHeartWfm);  //21
    printf("motionDetectedFlag = %.2f\n", outputStats.motionDetectedFlag);  //22
    printf("breathingRateEst_harmonicEnergy = %.2f\n", outputStats.breathingRateEst_harmonicEnergy);  //23
    printf("heartRateEst_harmonicEnergy = %.2f\n", outputStats.heartRateEst_harmonicEnergy);  //24
    printf("reserved7 = %.2f\n", outputStats.reserved7);  //25
    printf("reserved8 = %.2f\n", outputStats.reserved8);  //26
    printf("reserved9 = %.2f\n", outputStats.reserved9);  //27
    printf("reserved10 = %.2f\n", outputStats.reserved10);  //28
    printf("reserved11 = %.2f\n", outputStats.reserved11);  //29
    printf("reserved12 = %.2f\n", outputStats.reserved12);  //30
    printf("reserved13 = %.2f\n", outputStats.reserved13);  //31
    printf("reserved14 = %.2f\n", outputStats.reserved14);  //32


}

void printTLV_head(MmwDemo_output_message_tl *TLV_head) {
    printf("\nTLV_head:\n");
    printf("TLV_head type = %d\n", TLV_head->type);
    printf("TLV_head length = %d\n", TLV_head->length);
}
void printHead() {
    printf("Head:\n");
    printf("magicWord[0] = %#06x\t",head.magicWord[0]);
    printf("magicWord[1] = %#06x\t", head.magicWord[1]);
    printf("magicWord[2] = %#06x\t", head.magicWord[2]);
    printf("magicWord[3] = %#06x\n", head.magicWord[3]);
    printf("version = %#010x\n",head.version);
    printf("totalPacketLen = %d\n", head.totalPacketLen);
    printf("platform = %#010x\n", head.platform);

    printf("frameNumber = %d\n", head.frameNumber);
    printf("timeCpuCycles = %d\n", head.timeCpuCycles);
    printf("numDetectedObj = %d\n", head.numDetectedObj);
    printf("numTLVs = %d\n", head.numTLVs);
    printf("subFrameNumber = %d\n", head.subFrameNumber);
}
void readFile() {
    FILE* file;
    file = fopen(FILENAME, "r");
    int j = 1;
    if (file == NULL) {
        perror("无法打开文件"); // 错误处理
        exit(1);
    }
    while (1) {
    // 读取文件内容
    size_t bytesRead;
    int SIZE = sizeof(Header_t);
    while (1) {
        bytesRead = fread(&head, 1, sizeof(Header_t), file);
        if (head.magicWord[0] == 0x0102 && head.magicWord[1] == 0x0304 && head.magicWord[2] == 0x0506 && head.magicWord[3] == 0x0708)
            break;
        else
        {
            fseek(file, 4-SIZE, SEEK_CUR);
        }
        
    }
    
    printf("编号:%d\n",j++);
    if (bytesRead > 0) {
        // 打印读取的数据
        printHead();
    }
    else { break; }

    bytesRead = fread(&TLV_head, 1, sizeof(MmwDemo_output_message_tl), file);

    if (bytesRead > 0) {
        // 打印读取的数据
        printTLV_head(&TLV_head);
    }
    else { break; }


    bytesRead = fread(&outputStats, 1, sizeof(VitalSignsDemo_OutputStats), file);

    if (bytesRead > 0) {
        // 打印读取的数据
        printOutputStats();
    }
    else { break; }

    bytesRead = fread(&TLV_head1, 1, sizeof(MmwDemo_output_message_tl), file);

    if (bytesRead > 0) {
        // 打印读取的数据
        printTLV_head(&TLV_head1);
    }
    else { break; }


    uint32_t t;
    int* tmp = (int*)malloc(TLV_head1.length*4);
    bytesRead = fread(&tmp, TLV_head1.length, 4, file);
    if (bytesRead == 0) {
        printf("\n共取到了 %d个数据", j-1);
        fclose(file);
        exit(1);
    }

    /*
    for (int i = 0; i < TLV_head1.length; i++) {
        bytesRead = fread(&t, 1, 4, file);    
        if (!bytesRead) { 
            printf("\n i = %d ,j = %d时失败",i,j);
            fclose(file);
            exit(1); 
        }
        printf("%04x ", t);        
    } 
    */
    
    puts("");

    }
    fclose(file); // 关闭文件
}
int main1()
{
    readFile();
    return 0;
}

 

你可能感兴趣的:(c语言,算法,开发语言)