直接使用变量(数组返回在数组中相同)还是用指针? [英] Use a variable directly(data return are same in an array) or with pointer?

查看:175
本文介绍了直接使用变量(数组返回在数组中相同)还是用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两只鸟传感器,我参考手册中的示例代码#8,c ++代码正在为我工​​作。

I had two flock of bird sensor and i have refer to example code #8 in manual, the c++ code is working for me.

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

But i only get one sensor data from the c sharp project. The problem is that in the C# example, bird_data[1] and bird_data[2] appear to have same position data. In the C++ example both bird_data[1] and bird_data[2] have correct data.I get the same position output data from below code.

LBird1X中的文本输出是相同的LBird2X,LBird1Y是相同的LBird2Y和LBird1Z是相同的LBird2Z。

text output in the LBird1X is same LBird2X,LBird1Y is same LBird2Y and LBird1Z is same LBird2Z.

这可能是与缺乏指向?或者我在导入bird.dll结构数据时做错了什么?

Could this be to do with the lack of pointing ? Or i done something wrong in import the bird.dll stucture data?

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

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;
                }

            }

strong>

C++ Struct

#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

strong> C sharp Stuct

C sharp Stuct

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

这篇关于直接使用变量(数组返回在数组中相同)还是用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆