直接使用变量(数组中的数据返回相同)或与指针一起使用

本文关键字:指针 一起 返回 变量 数组 数据 | 更新日期: 2023-09-27 17:58:23

我有两只鸟传感器,我参考了手册中的示例代码#8,c++代码对我有效。

但我只从c sharp项目中获得了一个传感器数据。问题是,在C#示例中,bird_data[1]和bird_data[2]似乎具有相同的位置数据。在C++示例中,bird_data[1]和bird_data[2]都具有正确的数据。我从下面的代码中得到了相同的位置输出数据。

LBird1X中的文本输出是相同的LBird2X、LBird1Y是相同的LBird2Y并且LBird1Z是相同的LB ird2Z。

这可能是因为缺乏指向性吗?或者我在导入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 sharp

if (birdFrameReady(GROUP_ID))
        {
            birdGetMostRecentFrame(GROUP_ID, ref frame);
           BIRDREADING bird_data = new BIRDREADING();
            bird_data = frame.readings[i];
            for (i = 1; i < DEVCOUNT + 1; i++)
            {
                bird_data = frame.readings[i];
                string x, y, z;
                x = (bird_data.position.nX * 36 / 32767.0).ToString();
                y = (bird_data.position.nY * 36 / 32767.0).ToString();
                z = (bird_data.position.nZ * 36 / 32767.0).ToString();
                switch (i)
                {
                    case 1:
                        LBird1X.Text = i.ToString() + ":  " + x;
                        LBird1Y.Text = i.ToString() + ":  " + y;
                        LBird1Z.Text = i.ToString() + ":  " + z;
                        break;
                    case 2:
                        LBird2X.Text = i.ToString() + ":  " + x;
                        LBird2Y.Text = i.ToString() + ":  " + y;
                        LBird2Z.Text = i.ToString() + ":  " + z;
                        break;
                    default:
                        LBird2X.Text = "error";
                        LBird2Y.Text = "error";
                        LBird2Z.Text = "error";
                        break;
                }
            }

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

C锐卡住

[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)