- Home /
Unity 3d C++ run time error
BIRDREADING bird_data;
i receive c++ runtime error when my code include birdreading variable.Anyone have any idea about it.
Microsoft Visual C++ Runtime Library Runtime Error!
Program:C:\Program Files\Unity\Editor\Unity.exe
This application has requested the Runtime to terminate it in an unusual way.
please contact the application's support team for more information.
below is my code
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Runtime.InteropServices;
using System.Linq;
using System.Threading;
public class DLLImportFOB : MonoBehaviour
{
[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.Sequential, Pack = 0)]
public struct BIRDSYSTEMCONFIG
{
public byte bySystemStatus;
public byte byError;
public byte byNumDevices;
public byte byNumServers;
public byte byXmtrNum;
public ushort wXtalSpeed;
public double dMeasurementRate;
public byte byChassisNum;
public byte byNumChassisDevices;
public byte byFirstDeviceNum;
public ushort wSoftwareRev;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 127)]
public byte[] byFlockStatus;
}
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct BIRDDEVICECONFIG
{
public byte byStatus; // device status (see bird device status bits, above)
public byte byID; // device ID code (see bird device ID's, above)
public ushort wSoftwareRev; // software revision of device
public byte byError; // error code flagged by device
public byte bySetup; // setup information (see bird device setup bits, above)
public byte byDataFormat; // data format (see bird data formats, above)
public byte byReportRate; // rate of data reporting, in units of frames
public ushort wScaling; // full scale measurement, in inches
public byte byHemisphere; // hemisphere of operation (see bird hemisphere codes, above)
public byte byDeviceNum; // bird number
public byte byXmtrType; // transmitter type (see bird transmitter type bits, above)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
public ushort[] wAlphaMin; // filter constants (see Birdnet3 Protocol pp.26-27 for values)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
public ushort[] wAlphaMax; // filter constants (see Birdnet3 Protocol pp.26-27 for values)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
public ushort[] wVM; // filter constants (see Birdnet3 Protocol pp.26-27 for values)
BIRDANGLES anglesReferenceFrame; // reference frame of bird readings
BIRDANGLES anglesAngleAlign; // alignment of bird readings
}
enum GroupModeSettings
{
// GMS_DEFAULT, // driver will determine whether or not to use RS232 group mode
GMS_GROUP_MODE_NEVER, // RS232 group mode will never be used
GMS_GROUP_MODE_ALWAYS, // RS232 group mode will always be used
NUM_GROUP_MODE_SETTINGS
};
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdRS232GroupModeEnabled(int GroupID);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdShutDown(int GroupID);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdRS232WakeUp(int nGroupID, Boolean bStandAlone, int nNumDevices,
ushort[] pwComport, uint dwBaudRate, uint dwReadTimeout,
uint dwWriteTimeout, int nGroupMode);
[DllImport(@"bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdGetSystemConfig(int nGroupID, ref BIRDSYSTEMCONFIG psyscfg, bool bGetDriverCopy);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdSetSystemConfig(int nGroupID, ref BIRDSYSTEMCONFIG psyscfg);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdGetFastDeviceConfig(int nGroupID, int nDeviceNum, ref BIRDDEVICECONFIG pdevcfg);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdStartFrameStream(int nGroupID);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdGetMostRecentFrame(int nGroupID, ref BIRDFRAME pframe);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdFrameReady(int nGroupID);
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern string birdGetErrorMessage();
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdStopFrameStream(int nGroupID);
const int GROUP_ID = 1;
const int READ_TIMEOUT = 2000;
const int WRITE_TIMEOUT = 2000;
const int BAUD_RATE = 115200;
const double measurement_rate = 103.3;
int DEVCOUNT = 2;
string fobStatus = "Not Wake Up";
string bird1PosX="0", bird1PosY="0", bird1PosZ="0";
string bird2PosX="0", bird2PosY="0", bird2PosZ="0";
bool birdIsWakeUp = false;
bool getPosition = false;
ushort[] COM_port = new ushort[] { 0, 19, 0, 0, 0 };
BIRDSYSTEMCONFIG sysconfig;
BIRDDEVICECONFIG[] devconfig = new BIRDDEVICECONFIG[5];
BIRDFRAME frame;
double[] pos = new double[3];
double[] ang = new double[3];
//BIRDREADING bird_data;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (getPosition)
{
int i = 1;
birdStartFrameStream(GROUP_ID);
if (birdFrameReady(GROUP_ID))
{
birdGetMostRecentFrame(GROUP_ID, ref frame);
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:
bird1PosX = x;
bird1PosY = y;
bird1PosZ = z;
break;
case 2:
bird2PosX = x;
bird2PosY = y;
bird2PosZ = z;
break;
default:
bird1PosX = "Error";
bird1PosY = "Error";
bird1PosZ = "Error";
bird2PosX = "Error";
bird2PosY = "Error";
bird2PosZ = "Error";
break;
}
}
}
}
}
void OnGUI()
{
GUI.Box(new Rect(800, 50, 250, 25), "Status FOB:" + fobStatus);
GUI.Box(new Rect(800, 90, 50, 25), "Bird 1 Position X:" + bird1PosX);
GUI.Box(new Rect(900, 90, 50, 25), "Bird 1 Position Y:" + bird1PosY);
GUI.Box(new Rect(1000, 90, 50, 25), "Bird 1 Position Z:" + bird1PosZ);
GUI.Box(new Rect(800, 140, 50, 25), "Bird 2 Position X:" + bird2PosX);
GUI.Box(new Rect(900, 140, 50, 25), "Bird 2 Position Y:" + bird2PosY);
GUI.Box(new Rect(1000, 140, 50, 25), "Bird 2 Position Z:" + bird2PosZ);
if ((GUI.Button(new Rect(800, 10, 250, 25), "Connect Flock Of Bird")))
{
if (!birdRS232WakeUp(GROUP_ID, false, DEVCOUNT, COM_port, BAUD_RATE, READ_TIMEOUT, WRITE_TIMEOUT, 1))
{
fobStatus = "Failde to wake up FOB";
}
else
{
fobStatus = "Complete wake up FOB";
birdIsWakeUp = true;
}
}
if (birdIsWakeUp)
{
if ((GUI.Button(new Rect(800, 10, 250, 25), "Connect Flock Of Bird")))
{
getPosition = true;
}
}
}
}
Debug through it to see exactly what line it crashes at, then post the external code. It's definitely in the dll code
this code is work in visual studio, btw i try insert breakpoint in my code it is never stop in the line of the code, it is direct show me the runtime error after i click play.
Did you find the error? Can you send me more informations about that? Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100115im_/https://answers.unity.com/themes/thub/images/avi.jpg)