Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 直接使用变量(数据返回在数组中相同)还是与指针一起使用? 我有两个鸟群传感器,我在手册中提到了示例代码8,C++代码对我来说是有效的。_C#_C++_Pointers_Struct_Pinvoke - Fatal编程技术网

C# 直接使用变量(数据返回在数组中相同)还是与指针一起使用? 我有两个鸟群传感器,我在手册中提到了示例代码8,C++代码对我来说是有效的。

C# 直接使用变量(数据返回在数组中相同)还是与指针一起使用? 我有两个鸟群传感器,我在手册中提到了示例代码8,C++代码对我来说是有效的。,c#,c++,pointers,struct,pinvoke,C#,C++,Pointers,Struct,Pinvoke,但我只从c sharp项目中获得了一个传感器数据。问题在于,在C#示例中,bird#u数据[1]和bird#u数据[2]似乎具有相同的位置数据。在C++实例中,BidLdDATA(1)和BidLdDATA(2)都有正确的数据,从下面的代码得到相同的位置输出数据。 LBird1X中的文本输出与LBird2X相同,LBird1Y与LBird2Y相同,LBird1Z与LBird2Z相同 这是否与缺乏指向有关?或者我在导入bird.dll结构数据时出错了 C++代码: birdStartFrameStr

但我只从c sharp项目中获得了一个传感器数据。问题在于,在C#示例中,bird#u数据[1]和bird#u数据[2]似乎具有相同的位置数据。在C++实例中,BidLdDATA(1)和BidLdDATA(2)都有正确的数据,从下面的代码得到相同的位置输出数据。 LBird1X中的文本输出与LBird2X相同,LBird1Y与LBird2Y相同,LBird1Z与LBird2Z相同

这是否与缺乏指向有关?或者我在导入bird.dll结构数据时出错了

C++代码:

birdStartFrameStream(GROUP_ID);
do // Until Keypress
{
  if(birdFrameReady(GROUP_ID)) // Check if there's data available 
  {
    birdGetMostRecentFrame(GROUP_ID,&frame);//Get data from bird BIRDREADING *bird_data; // Transfers data into structure
    BIRDREADING *bird_data;
    for(int i=1; i<DEVCOUNT+1; i++ ) // Loop to get data from birds
    {
      // Change pointer to index of first bird (1)
      bird_data = &frame.reading[i]; // Convert data into inches and degrees and scale
      pos[0] = bird_data->position.nX * 36 / 32767.;
      pos[1] = bird_data->position.nY * 36 / 32767.;
      pos[2] = bird_data->position.nZ * 36 / 32767.;
      ang[0] = bird_data->angles.nAzimuth * 180. / 32767.;
      ang[1] = bird_data->angles.nElevation * 180. / 32767.;
      ang[2] = bird_data->angles.nRoll * 180. / 32767.;
      // print data
      printf("%i> %+6.1f %+6.1f %+6.1f ", i,pos[0], pos[1],pos[2]);
      //  printf("%+6.1f %+6.1f %+6.1f \n",ang[0], ang[1], ang[2]);
    } // end move data from structure to screen loop
  } // end if frame ready loop
} while(!kbhit()); // loop until any key is pressed
C尖锐结构

#pragma pack(1) // pack the following structures on one-byte boundaries

// Bird position structure
typedef struct tagBIRDPOSITION
{
    short   nX;         // x-coordinate
    short   nY;         // y-coordinate
    short   nZ;         // z-coordinate
}
BIRDPOSITION;

// Bird angles structure
typedef struct tagBIRDANGLES
{
    short   nAzimuth;   // azimuth angle
    short   nElevation; // elevation angle
    short   nRoll;      // roll angle
}
BIRDANGLES;

// Bird matrix structure
typedef struct tagBIRDMATRIX
{
    short   n[3][3];    // array of matrix elements
}
BIRDMATRIX;

// Bird quaternion structure
typedef struct tagBIRDQUATERNION
{
    short   nQ0;        // q0
    short   nQ1;        // q1
    short   nQ2;        // q2
    short   nQ3;        // q3
}
BIRDQUATERNION;

#pragma pack()  // resume normal packing of structures

// Bird reading structure
typedef struct tagBIRDREADING
{
    BIRDPOSITION    position;   // position of receiver
    BIRDANGLES      angles;     // orientation of receiver, as angles
    BIRDMATRIX      matrix;     // orientation of receiver, as matrix
    BIRDQUATERNION  quaternion; // orientation of receiver, as quaternion
    WORD            wButtons;   // button states
}
BIRDREADING;

// Bird frame structure
//
// NOTE: In stand-alone mode, the bird reading is stored in reading[0], and
//  all other array elements are unused.  In master/slave mode, the "reading"
//  array is indexed by bird number - for example, bird #1 is at reading[1],
//  bird #2 is at reading[2], etc., and reading[0] is unused.
typedef struct tagBIRDFRAME
{
    DWORD           dwTime;     // time at which readings were taken, in msecs
    BIRDREADING     reading[BIRD_MAX_DEVICE_NUM + 1];  // reading from each bird
}
BIRDFRAME;

// Bird system configuration structure
//
// NOTE: In TCP/IP mode, the following fields are NOT used:
//  byXtalSpeed
//
// NOTE: In RS232 and ISA modes, the following fields are NOT used:
//  bits BSS_FBB_ERROR, BSS_LOCAL_ERROR, BSS_LOCAL_POWER, and BSS_MASTER of bySystemStatus
//  byNumServers
//  byChassisNum
//  byNumChassisDevices
//  byFirstDeviceNum
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDPOSITION
{
    public short nX;            // x-coordinate
    public short nY;            // y-coordinate
    public short nZ;            // z-coordinate
}


// Bird angles structure
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDANGLES
{
    public short nAzimuth;  // azimuth angle
    public short nElevation;    // elevation angle
    public short nRoll;     // roll angle
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDMATRIX
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
    public short[,] n;


}


[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDQUATERNION
{
    public short nQ0;       // q0
    public short nQ1;       // q1
    public short nQ2;       // q2
    public short nQ3;       // q3
}




[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct BIRDREADING
{
    public BIRDPOSITION position;   // position of receiver
    public BIRDANGLES angles;       // orientation of receiver, as angles
    public BIRDMATRIX matrix;       // orientation of receiver, as matrix
    public BIRDQUATERNION quaternion; // orientation of receiver, as quaternion
    public ushort wButtons; // button states
}



[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct BIRDFRAME
{
    public uint dwTime;     // time at which readings were taken, in msecs
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 127)]
    public BIRDREADING[] readings; // reading from each bird
}

尝试使用StructLayout(LayoutKind.Explicit)

为什么从1开始?据我所知,这是一个数组,这意味着它是一个基于零的索引,在循环中应该从0开始,而不是1。它基于硬件设备的手册,我的模式不是独立的,它是从1开始的。0表示独立模式。已解决。。。这可能是不好的,我错误地配置了硬件“用StructLayout(LayoutKind.Explicit)标记的实例字段类型必须有FieldOffset属性”,我在更改它后得到了这种错误。该属性的含义是您在内存中手动排序数据字段。查看详细信息
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDPOSITION
{
    public short nX;            // x-coordinate
    public short nY;            // y-coordinate
    public short nZ;            // z-coordinate
}


// Bird angles structure
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDANGLES
{
    public short nAzimuth;  // azimuth angle
    public short nElevation;    // elevation angle
    public short nRoll;     // roll angle
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDMATRIX
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
    public short[,] n;


}


[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BIRDQUATERNION
{
    public short nQ0;       // q0
    public short nQ1;       // q1
    public short nQ2;       // q2
    public short nQ3;       // q3
}




[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct BIRDREADING
{
    public BIRDPOSITION position;   // position of receiver
    public BIRDANGLES angles;       // orientation of receiver, as angles
    public BIRDMATRIX matrix;       // orientation of receiver, as matrix
    public BIRDQUATERNION quaternion; // orientation of receiver, as quaternion
    public ushort wButtons; // button states
}



[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct BIRDFRAME
{
    public uint dwTime;     // time at which readings were taken, in msecs
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 127)]
    public BIRDREADING[] readings; // reading from each bird
}