c# على استخدام متغير مباشرة(عودة البيانات هي نفسها في مصفوفة) أو مع مؤشر؟



امثلة على المصفوفات بلغة c++ pdf (1)

كان لي قطيع اثنين من جهاز استشعار الطيور ولدي الرجوع إلى رمز المثال # 8 في دليل، و C ++ رمز يعمل بالنسبة لي.

ولكن أنا فقط الحصول على بيانات الاستشعار واحد من المشروع ج حاد. والمشكلة هي أنه في المثال C #، يظهر بيرد_data [1] و bird_data [2] نفس بيانات الموضع. في المثال C ++ كل من bird_data [1] و bird_data [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 حاد التي لديها نفس البيانات

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
}

Answer #1

حاول استخدام StructLayout(LayoutKind.Explicit)





pinvoke