C#封装API结构数组指针实例

来源:互联网 发布:二小姐脸型数据 南风 编辑:程序博客网 时间:2024/06/05 22:31
 int len = 0;
            VCI_ERR_INFO errInfo = new VCI_ERR_INFO();
            errInfo.Passive_ErrData = new byte[3];
            VCI_CAN_OBJ[] frameInfos=new VCI_CAN_OBJ[200];
            VCI_CAN_OBJ vData = new VCI_CAN_OBJ();
            vData.Reserved = new byte[3];
            vData.Data = new byte[8];
            IntPtr tempPtr;
            try
            {
                while (mConnResult)
                {
                    //从非托管代码中分配内存
                    tempPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(vData) * 200);
                    if (tempPtr != null)
                    {
                        //将数据从托管代码中封送到非托管代码内存快
                        System.Runtime.InteropServices.Marshal.StructureToPtr((object)vData, tempPtr, true);//System.Runtime.InteropServices.Marshal.AllocHGlobal(24);//50 *                
                    }
                    Thread.Sleep(1);
                    if (!mConnResult)
                        break;
                    try
                    {
                        //flag++;
                        len = (int)APICodeFunction.VCI_Receive((UInt32)mConfigData.CanCardType, (UInt32)mConfigData.DevIndex,
                        (UInt32)mConfigData.CanIndex, tempPtr, 200, 200);//50 * 24  ref
                    }
                    catch (ExecutionEngineException ee)
                    {
                        Trace.WriteLine(string.Format("接收数据:{0}", ee.Message), TraceLevel.Error.ToString());
                    }
                    if (len <= 0)
                    {
                        //注意:如果没有读到数据则必须调用此函数来读取出当前的错误码,
                        //千万不能省略这一步(即使你可能不想知道错误码是什么)
                        APICodeFunction.VCI_ReadErrInfo((uint)mConfigData.CanCardType, (UInt32)mConfigData.DevIndex, (UInt32)mConfigData.CanIndex, ref tempPtr);
                    }
                    else
                    {
                        byte[] frameInfosBuffer = new byte[len * 24];
                        //将数据从非托管内存块奉送到指定类型的托管对象
                        object vTestObj = System.Runtime.InteropServices.Marshal.PtrToStructure(tempPtr, typeof(VCI_CAN_OBJ));
                        VCI_CAN_OBJ vTempCan = (VCI_CAN_OBJ)vTestObj;
                        //byte[] data = vTempCan.Data;
                        //byte dataLength = vTempCan.DataLen;
                        //Debug.WriteLine(BitConverter.ToString(vTempCan.Data, 0).Replace("-", " "));
                        for (int i = 0; i < len; i++)
                        {
                            IntPtr pPonitor = new IntPtr(tempPtr.ToInt64() + System.Runtime.InteropServices.Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * i);
                            frameInfos[i] = (VCI_CAN_OBJ)System.Runtime.InteropServices.Marshal.PtrToStructure(pPonitor, typeof(VCI_CAN_OBJ));
                            frameInfos[i].ID = frameInfos[i].ID * 8;
                        }
                        //vTempCan.ID = vTempCan.ID * 8;
                        //frameInfos[0] = vTempCan;
                        OnReceivedEventHander(len, frameInfos);
             
                    }
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(tempPtr);
                }
            }
原创粉丝点击