windows 如何调试阅读C++中的Catman二进制文件,程序在特定点读取不正确

tp5buhyn  于 2023-01-10  发布在  Windows
关注(0)|答案(1)|浏览(110)

我正在用C编写一个程序,在这个程序中我读取了从Catman的数据采集设备接收到的二进制文件。到目前为止,它一直工作正常,但我被困在一个点上,我认为我要么读得太多,要么读得太少,我不知道与我的C代码相比,Python代码有什么不同,而Python代码正是我想要重新创建的。
这里有很多重复的代码行,所以我想先说我从一个python库中得到了一个facit,它做了我想做的事情。所以我将展示我的C代码看起来与python代码不同的部分。我将在帖子的结尾打印整个可重复的代码。
Here is the python libary for what I want to do: https://github.com/leonbohmann/APReader/blob/9d76be8e94860a0711f2c7c4973641706c5ea479/apread/entries.py#L32
因此,我的项目是重新创建Python库,但对于C
。我在读取"扩展通道头"时遇到了麻烦,其中"HPFilt"字段开始填充乱码/意外数据。
下面是python从"扩展通道头"中读取的内容:
{"起始时间":44916.26666666667,"日期":3.333333333333335,"传感器类型":0,"电源电压":0,"筛选字符":0,"过滤频率":0,"皮重":0.0,"零值":0.0,"测量范围":0.0,"字符内":[0.0,0.0,0.0,0.0],"序列号":""、"物理单元":'','本机单元':""、"插槽":0,"子槽":0,"放大器类型":0,"应用程序类型":0,"k系数":0.0,"b因子":0.0,"测量信号":0,"放大器输入":0,"高压过滤器":0,"联机库导入信息":0,
下面是python代码的一部分:

exthdr['T0'] = rdr.read_double() # (pos0+) 8
    exthdr['dt'] = rdr.read_double() # 16
    exthdr['SensorType'] = rdr.read_int16() # 18
    exthdr['SupplyVoltage'] = rdr.read_int16() # 20
    
    exthdr['FiltChar'] = rdr.read_int16() # 22
    exthdr['FiltFreq'] = rdr.read_int16() # 24
    exthdr['TareVal'] = rdr.read_float() # 28
    exthdr['ZeroVal'] = rdr.read_float() # 32   
    exthdr['MeasRange'] = rdr.read_float() # 36
    exthdr['InChar'] = [rdr.read_float() for i in range(4)] # 40, 44, 48, 52
    
    exthdr['SerNo'] = rdr.read_string(32) # 84
    exthdr['PhysUnit'] = rdr.read_string(8) # 92
    exthdr['NativeUnit'] = rdr.read_string(8) # 100
    
    exthdr['Slot'] = rdr.read_int16() # 102
    exthdr['SubSlot'] = rdr.read_int16() # 104
    exthdr['AmpType'] = rdr.read_int16() # 106
    exthdr['APType'] = rdr.read_int16() # 108
    exthdr['kFactor'] = rdr.read_float() # 112
    exthdr['bFactor'] = rdr.read_float() # 116
    
    exthdr['MeasSig'] = rdr.read_int16() # 118
    exthdr['AmpInput'] = rdr.read_int16() # 120
    exthdr['HPFilt'] = rdr.read_int16() # 122
    exthdr['OLImportInfo'] = rdr.read_byte() # 123

下面是我的C程序从"扩展通道头"中读取的内容:
通道1 Tzero:44916.266667通道1日期:3.333333通道1传感器类型:0通道1电源电压:0通道1过滤字符:0通道1滤波频率:0通道1校准值:0.000000通道1测量范围:0.000000通道1英寸字符[0]:0.000000通道1英寸[1]:0.000000通道1英寸[2]:0.000000通道1英寸[3]:0.000000通道1序列号:第1通道物理单元:通道1本地单位:信道1时隙:0通道1子时隙:0通道1安培类型:0通道1 AP类型:0通道1 k系数:0.000000通道1 b系数:0.000000通道1测量值:0通道1安培输入:0通道1高压滤波器:538968832通道1 OL导入:20
下面是C
代码一部分:

rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.TZero, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.TZero));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.dt, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.dt));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.sensorType, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.sensorType));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.supplyVoltage, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.supplyVoltage));

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtChar, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtChar));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtFreq, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtFreq));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.tareVal, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.tareVal));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.zeroVal, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.zeroVal));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.measRange, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.measRange));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[0], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[0]));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[1], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[1]));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[2], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[2]));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[3], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[3]));

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.serNo, 32);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.physUnit, 8);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.nativeUnit, 8);

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.slot, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.slot));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.subSlot, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.subSlot));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampType, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampType));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.APType, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.APType));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.kFactor, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.kFactor));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.bFactor, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.bFactor));

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.measSig, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.measSig));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampInput, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampInput));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.HPFilt, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.HPFilt));                  // Should read 0...
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.OLImport, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.OLImport));

所以在"dt"字段后面的某个地方,我的代码要么读得太多,要么读得太少。我已经检查了我使用的类型,这样它们就没有什么不同了,但也可能是我误解了一些东西,所以下面是它们:

struct catman_VB_DB_CHANHEADER
{
    double TZero;
    double dt;
    int sensorType;
    int supplyVoltage;
    int filtChar;
    int filtFreq;
    float tareVal;
    float zeroVal;
    float measRange;
    float inChar[4];
    char serNo[32];//std::string serNo;
    char physUnit[8];//std::string physUnit;
    char nativeUnit[8];//std::string nativeUnit;
    int slot;
    int subSlot;
    int ampType;
    int APType;
    float kFactor;
    float bFactor;
    int measSig;
    int ampInput;
    int HPFilt;
    byte OLImport;
};

到目前为止,我的理论是,我可能没有正确读取SerNo、physUnit和nativeUnit的空字段。也许这两种语言之间的做法不同?我尝试过读取它们,但没有读取它们。唯一的区别是,我得到了不同的乱码字段。
PS.整个可复制代码:

struct catman_VB_DB_CHANHEADER
{
    double TZero;
    double dt;
    int sensorType;
    int supplyVoltage;
    int filtChar;
    int filtFreq;
    float tareVal;
    float zeroVal;
    float measRange;
    float inChar[4];
    char serNo[32];//std::string serNo;
    char physUnit[8];//std::string physUnit;
    char nativeUnit[8];//std::string nativeUnit;
    int slot;
    int subSlot;
    int ampType;
    int APType;
    float kFactor;
    float bFactor;
    int measSig;
    int ampInput;
    int HPFilt;
    byte OLImport;
};
struct catmanGlobalSection
{
    short fileID;
    long dataOffset;
    short fileCommentLength;
    //byte fileComment[fileCommentLength];
    short additionalDataOffsetNoOfBytes;
    //byte additionalDataOffset[additionalDataOffsetNoOfBytes];
    short reserveStringNoOfBytes[32];
    //byte reserveString[reserveStringNoOfBytes];
    short noOfChannels;
    long maxChannelLength;
    long ChannelLengthOffset[16];
    long reductionFactor;
};
struct catmanChannelHeaderSection
{
    short channelLocation;
    long channelLength;
    short channelNameLength;
    //byte channelname[channelNameLength];
    short unitLength;
    //byte unit[unitLength];
    short channelCommentLength;
    //byte channelComment[channelCommentLength];
    short format;
    short dataWidth;
    double dateAndTimeOfMeasurement;
    long extendedChannelHeaderSize;
    catman_VB_DB_CHANHEADER extendedChannelHeader;
};
struct catmanFormat
{
    catmanGlobalSection globalSection;
    catmanChannelHeaderSection ChannelHeaderSection[16];
};
static catmanFormat catmanData;

std::ifstream rf("FTP Folder/Recorder 14_2022_12_21_06_24_00.bin", std::ios::in | std::ios::binary);

rf.read((char*)&catmanData.globalSection.fileID, sizeof(catmanData.globalSection.fileID));
rf.read((char*)&catmanData.globalSection.dataOffset, sizeof(catmanData.globalSection.dataOffset));
if(shortVersion)
    rf.ignore(catmanData.globalSection.dataOffset);
else
{
    rf.read((char*)&catmanData.globalSection.fileCommentLength, sizeof(catmanData.globalSection.fileCommentLength));
    rf.ignore(catmanData.globalSection.fileCommentLength);
    /* These lines misaligns the buffer... They should be included according to Catman's binary format document. Leave them commented for now.
    rf.read((char*)&catmanData.globalSection.additionalDataOffsetNoOfBytes, sizeof(catmanData.globalSection.additionalDataOffsetNoOfBytes));
    rf.ignore(catmanData.globalSection.additionalDataOffsetNoOfBytes);
    */
    for (int i = 0; i < 32; i++)
    {
        rf.read((char*)&catmanData.globalSection.reserveStringNoOfBytes[i], sizeof(catmanData.globalSection.reserveStringNoOfBytes[i]));
        rf.ignore(catmanData.globalSection.reserveStringNoOfBytes[i]);
    }
}
rf.read((char*)&catmanData.globalSection.noOfChannels, sizeof(catmanData.globalSection.noOfChannels));
rf.read((char*)&catmanData.globalSection.maxChannelLength, sizeof(catmanData.globalSection.maxChannelLength));
for (int i = 0; i < catmanData.globalSection.noOfChannels; i++)
{
    rf.read((char*)&catmanData.globalSection.ChannelLengthOffset[i], sizeof(catmanData.globalSection.ChannelLengthOffset[i]));
}
rf.read((char*)&catmanData.globalSection.reductionFactor, sizeof(catmanData.globalSection.reductionFactor));

for (int i = 0; i < catmanData.globalSection.noOfChannels; i++)
{
    rf.read((char*)&catmanData.ChannelHeaderSection[i].channelLocation, sizeof(catmanData.ChannelHeaderSection[i].channelLocation));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].channelLength, sizeof(catmanData.ChannelHeaderSection[i].channelLength));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].channelNameLength, sizeof(catmanData.ChannelHeaderSection[i].channelNameLength));
    rf.ignore(catmanData.ChannelHeaderSection[i].channelNameLength);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].unitLength, sizeof(catmanData.ChannelHeaderSection[i].unitLength));
    rf.ignore(catmanData.ChannelHeaderSection[i].unitLength);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].channelCommentLength, sizeof(catmanData.ChannelHeaderSection[i].channelCommentLength));
    rf.ignore(catmanData.ChannelHeaderSection[i].channelCommentLength);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].format, sizeof(catmanData.ChannelHeaderSection[i].format));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].dataWidth, sizeof(catmanData.ChannelHeaderSection[i].dataWidth));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].dateAndTimeOfMeasurement, sizeof(catmanData.ChannelHeaderSection[i].dateAndTimeOfMeasurement));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeaderSize, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeaderSize));
    
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.TZero, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.TZero));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.dt, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.dt));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.sensorType, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.sensorType));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.supplyVoltage, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.supplyVoltage));

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtChar, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtChar));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtFreq, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.filtFreq));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.tareVal, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.tareVal));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.zeroVal, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.zeroVal));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.measRange, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.measRange));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[0], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[0]));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[1], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[1]));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[2], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[2]));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[3], sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.inChar[3]));

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.serNo, 32);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.physUnit, 8);
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.nativeUnit, 8);

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.slot, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.slot));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.subSlot, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.subSlot));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampType, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampType));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.APType, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.APType));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.kFactor, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.kFactor));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.bFactor, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.bFactor));

    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.measSig, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.measSig));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampInput, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.ampInput));
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.HPFilt, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.HPFilt));                  // Should read 0...
    rf.read((char*)&catmanData.ChannelHeaderSection[i].extendedChannelHeader.OLImport, sizeof(catmanData.ChannelHeaderSection[i].extendedChannelHeader.OLImport));
}
pxyaymoc

pxyaymoc1#

定义

std::ifstream rf(..., std::ios::out, std::ios::binary);

包含***两个***错误:
1.在 output 模式下打开一个仅输入流。

  1. std::ios::outstd::ios::binary是按位 * 标志 *,需要一起按位“或”,而不是作为单独的参数传递。
    汇总:
std::ofstream rf(..., std::ios::out | std::ios::binary);

相关问题