C#FreePIE YEI空间传感器3 - 流数据

时间:2013-06-07 11:06:47

标签: c# dll marshalling

我在使用ThreeSpace_API.dll时遇到问题,同时尝试将传感器支持添加到freePIE。使用轮询方法工作正常。但是我遇到流媒体问题。

使用VS 2012这绝对没问题。但是,在构建并尝试运行可执行文件后,它崩溃并出现以下错误:

Application: FreePIE.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
at FreePIE.Core.Plugins.Yei3Space.Api.tss_getLastStreamData(Int32, FreePIE.Core.Plugins.Yei3Space.TssStreamPacket ByRef, Int32)
at FreePIE.Core.Plugins.Yei3Space.Api.UpdateQuaternion(Int32,   FreePIE.Core.Plugins.SensorFusion.Quaternion)
at FreePIE.Core.Plugins.Yei3SpaceGlobalHolder.Update()

以下列方式调用:

//DLL Call to ThreeSpace_API.dll
[DllImport("ThreeSpace_API.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern TssError tss_getLastStreamData(int id, out TssStreamPacket packet, int size);

//Stream Packet Struct
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TssStreamPacket
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public float[] quat;        
}

// Starts streaming asynchronous data
public static void StartStreamingU(int device_id)
{
    int count;
    byte[] stream_slots = new byte[8];
    stream_slots[0] = (byte)TssStreaming.TSS_GET_TARED_ORIENTATION_AS_QUATERNION;
    stream_slots[1] = (byte)TssStreaming.TSS_NULL;
    stream_slots[2] = (byte)TssStreaming.TSS_NULL;
    stream_slots[3] = (byte)TssStreaming.TSS_NULL;
    stream_slots[4] = (byte)TssStreaming.TSS_NULL;
    stream_slots[5] = (byte)TssStreaming.TSS_NULL;
    stream_slots[6] = (byte)TssStreaming.TSS_NULL;
    stream_slots[7] = (byte)TssStreaming.TSS_NULL;

    count=0;
    if( !is_streaming )
    {
        while( count < 3 )
        {
            if( tss_setStreamingTiming(device_id,0,-1,1500000) == 0 )
            {
                if( tss_setStreamingSlots(device_id,stream_slots) == 0 )
                {
                    if( tss_startStreaming(device_id) == 0 )
                    {
                        is_streaming=true;
                        break;
                    }
                }
            }
            count++;
        }
    }    
}

public static TssError UpdateQuaternion(int deviceId, Quaternion quaternion)
{
    uint timestamp;
    float[] fltQuat = new float[4];
    TssStreamPacket packet = new TssStreamPacket();            
    TssError error = TssError.TSS_NO_ERROR;            
    if(is_streaming)
    {
        try
        {
            error = tss_getLastStreamData(deviceId, out packet, 16);
            fltQuat[0] = packet.quat[0];
            fltQuat[1] = packet.quat[1];
            fltQuat[2] = packet.quat[2];
            fltQuat[3] = packet.quat[3];                                        
        }
        catch (System.AccessViolationException e)
        {
            return TssError.TSS_ERROR_READ;
        }
    }
}

令人讨厌的是它通过调试器工作正常,所以我无法做多少来测试它。我已经尝试了各种替代方法来编组数据。有趣的是,通过普通的浮点数组来执行此操作似乎会阻止CPU。

任何建议都会非常感激。

1 个答案:

答案 0 :(得分:0)

包装器缺少timestamp参数。

这是来自yei_threespace_api的声明。

TSS_EXPORT TSS_Error tss_getLastStreamData(TSS_Device_Id device, char * output_data, unsigned int output_data_len, unsigned int * timestamp);

经过测试,现在工作正常!