Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Int32[] DevHandles = new Int32[20];
            Int32   DevHandle  = 0;
            Byte    LINIndex   = 0;
            bool    state;
            Int32   DevNum, ret = 0;

            LDFDecoder.LDF_CODING LDFCoding = new LDFDecoder.LDF_CODING();
            //LDFCoding.pNode[0].pSlaveNames = new StringBuilder[16];
            string[] MSGTypeStr = { "UN", "MW", "MR", "SW", "SR", "BK", "SY", "ID", "DT", "CK" };
            //扫描查找设备
            DevNum = USB_DEVICE.USB_ScanDevice(DevHandles);
            if (DevNum <= 0)
            {
                Console.WriteLine("No device connected!");
                return;
            }
            else
            {
                Console.WriteLine("Have {0} device connected!", DevNum);
            }
            DevHandle = DevHandles[0];
            //打开设备
            state = USB_DEVICE.USB_OpenDevice(DevHandle);
            if (!state)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            else
            {
                Console.WriteLine("Open device success!");
            }
            //ldf文件解析
            ret = LDFDecoder.LDF_Decode(DevHandle, "test.ldf", ref LDFCoding);
            if (ret != LDFDecoder.LDF_SUCCESS)
            {
                Console.WriteLine("LDF decode failed");
            }
            //获取帧
            int LINFrameLen = LDFCoding.FrameLen;

            LDFDecoder.LDF_LIN_FRAME[] LINFrames = new LDFDecoder.LDF_LIN_FRAME[LINFrameLen];
#if PRINT_LDF_INFO
            Console.WriteLine("LINFrameLen = " + string.Format("{0}", LINFrameLen));
            for (int i = 0; i < LINFrameLen; i++)
            {
                LINFrames[i] = (LDFDecoder.LDF_LIN_FRAME)Marshal.PtrToStructure((IntPtr)((UInt32)LDFCoding.pFrames + i * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_FRAME))), typeof(LDFDecoder.LDF_LIN_FRAME));
                Console.WriteLine("Name = " + Marshal.PtrToStringAnsi(LINFrames[i].pName));
                Console.WriteLine("--ID = 0x" + string.Format("{0:X2}", LINFrames[i].ID));
                Console.WriteLine("--Publisher = " + Marshal.PtrToStringAnsi(LINFrames[i].pPublisher));
                Console.WriteLine(string.Format("--Size = {0} Bytes", LINFrames[i].Size));
                Console.WriteLine(string.Format("--SignalListLen = {0}", LINFrames[i].SignalListLen));
                LDFDecoder.LDF_LIN_SIGNAL[] SignalList = new LDFDecoder.LDF_LIN_SIGNAL[LINFrames[i].SignalListLen];
                for (int j = 0; j < LINFrames[i].SignalListLen; j++)
                {
                    SignalList[j] = (LDFDecoder.LDF_LIN_SIGNAL)Marshal.PtrToStructure((IntPtr)((UInt32)LINFrames[i].pSignalList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SIGNAL))), typeof(LDFDecoder.LDF_LIN_SIGNAL));
                    Console.WriteLine("--SignalName = " + Marshal.PtrToStringAnsi(SignalList[j].pName));
                    Console.WriteLine(string.Format("----Size = {0} bits", SignalList[j].Size));
                    Console.WriteLine(string.Format("----Offset = {0} ", SignalList[j].Offset));
                    Console.Write("----InitValue = ");
                    if (SignalList[j].ValueType == LDFDecoder.LDF_SIGNAL_VALUE_TYPE.LDF_SIGNAL_VALUE_TYPE_SCALAR)
                    {
                        Console.WriteLine(string.Format("{0}", SignalList[j].Value.ScalarValue));
                    }
                    else
                    {
                        for (int ii = 0; ii < (SignalList[j].Size / 8); ii++)
                        {
                            Console.Write(string.Format("0x{0:X2} ", SignalList[j].Value.ArrayValue[ii]));
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("----Publisher = " + Marshal.PtrToStringAnsi(SignalList[j].pPublisher));
                    Console.Write("----Subscribers = ");

                    for (int ii = 0; ii < SignalList[j].SubscriberLen; ii++)
                    {
                        Console.Write(string.Format("{0} ", Marshal.PtrToStringAnsi(SignalList[j].pSubscribers[ii])));
                    }
                    Console.WriteLine("");
                    if ((Int32)SignalList[j].SignalCodings.pName != 0)
                    {
                        if (Marshal.PtrToStringAnsi(SignalList[j].SignalCodings.pName).Length > 0)
                        {
                            Console.WriteLine("----SignalCodings = " + Marshal.PtrToStringAnsi(SignalList[j].SignalCodings.pName));
                        }
                    }
                    Console.WriteLine("");
                }
            }
#endif
            //获取调度表
            int SchTableLen = LDFCoding.LINSchLen;
            LDFDecoder.LDF_LIN_SCH[] SchTables = new LDFDecoder.LDF_LIN_SCH[SchTableLen];
#if PRINT_LDF_INFO
            Console.WriteLine(string.Format("SchTableLen = {0}", SchTableLen));
            for (int i = 0; i < SchTableLen; i++)
            {
                SchTables[i] = (LDFDecoder.LDF_LIN_SCH)Marshal.PtrToStructure((IntPtr)((UInt32)LDFCoding.pLINSch + i * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SCH))), typeof(LDFDecoder.LDF_LIN_SCH));
                Console.Write("{0}{" + Marshal.PtrToStringAnsi(SchTables[i].pName));
                LDFDecoder.LDF_LIN_SCH_CMD[] SCHCmds = new LDFDecoder.LDF_LIN_SCH_CMD[SchTables[i].ScheduleCmdLen];
                for (int j = 0; j < SchTables[i].ScheduleCmdLen; j++)
                {
                    SCHCmds[j] = (LDFDecoder.LDF_LIN_SCH_CMD)Marshal.PtrToStructure((IntPtr)((UInt32)SchTables[i].pScheduleCmdList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SCH_CMD))), typeof(LDFDecoder.LDF_LIN_SCH_CMD));
                    Console.Write(Marshal.PtrToStringAnsi(SCHCmds[j].Frame.pName) + string.Format(" delay {0} ms;", SCHCmds[j].DelayOfMs));
                }
                Console.WriteLine("}");
            }
#endif
            //获取节点信息
            LDFDecoder.LDF_LIN_NODE Node = (LDFDecoder.LDF_LIN_NODE)Marshal.PtrToStructure(LDFCoding.pNode, typeof(LDFDecoder.LDF_LIN_NODE));
#if PRINT_LDF_INFO
            Console.WriteLine("Master=" + Marshal.PtrToStringAnsi(Node.pMasterName) + string.Format(" time_base ={0} ms,jitter={1} ms", Node.time_base, Node.jitter));
            Console.Write("Slaves=");
            for (int i = 0; i < Node.SlaveLen; i++)
            {
                Console.Write(Marshal.PtrToStringAnsi(Node.pSlaveNames[i]) + " ");
            }
            Console.WriteLine("");
#endif
            //初始化LIN适配器
            ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 9600, 1);
            if (ret != USB2LIN_EX.LIN_EX_SUCCESS)
            {
                Console.WriteLine("Config LIN failed!");
                return;
            }
            else
            {
                Console.WriteLine("Config LIN Success!");
            }
            Console.WriteLine(string.Format("SchTableLen = {0}", SchTableLen));
            //执行调度表
            for (int i = 0; i < SchTableLen; i++)
            {
                for (int j = 0; j < SchTables[i].ScheduleCmdLen; j++)
                {
                    LDFDecoder.LDF_LIN_SCH_CMD SCHCmds   = (LDFDecoder.LDF_LIN_SCH_CMD)Marshal.PtrToStructure((IntPtr)((UInt32)SchTables[i].pScheduleCmdList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SCH_CMD))), typeof(LDFDecoder.LDF_LIN_SCH_CMD));
                    LDFDecoder.LDF_LIN_FRAME   LINFrame  = SCHCmds.Frame;
                    USB2LIN_EX.LIN_EX_MSG[]    LINMsg    = new USB2LIN_EX.LIN_EX_MSG[1];
                    USB2LIN_EX.LIN_EX_MSG[]    LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10];
                    if ((Int32)LINFrame.pName != 0)//确保当前帧是有效帧
                    {
                        if ((Marshal.PtrToStringAnsi(LINFrame.pName).Equals("MasterReq")) || ((LINFrame.Type == LDFDecoder.LDF_FRAME_TYPE.LDF_FRAME_TYPE_NORMAL) && (Marshal.PtrToStringAnsi(LINFrame.pPublisher).Equals(Marshal.PtrToStringAnsi(Node.pMasterName)))))
                        {
                            //该帧为主机发送数据帧
                            if (LINFrame.Type == LDFDecoder.LDF_FRAME_TYPE.LDF_FRAME_TYPE_NORMAL)
                            {
                                LINMsg[0].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT;//可以根据ldf文件中的版本号来设置,大于等于2.0的一般都是增强校验,小于2.0的一般都是标准校验
                            }
                            else
                            {
                                LINMsg[0].CheckType = USB2LIN_EX.LIN_EX_CHECK_STD;
                            }
                            LINMsg[0].MsgType   = USB2LIN_EX.LIN_EX_MSG_TYPE_MW;
                            LINMsg[0].PID       = LINFrame.ID;
                            LINMsg[0].Timestamp = (UInt32)SCHCmds.DelayOfMs;
                            //将信号进行赋值,此处可以根据实际情况进行赋值
                            LDFDecoder.LDF_LIN_SIGNAL[] SignalList = new LDFDecoder.LDF_LIN_SIGNAL[LINFrame.SignalListLen];
                            for (int k = 0; k < LINFrame.SignalListLen; k++)
                            {
                                SignalList[k] = (LDFDecoder.LDF_LIN_SIGNAL)Marshal.PtrToStructure((IntPtr)((UInt32)LINFrame.pSignalList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SIGNAL))), typeof(LDFDecoder.LDF_LIN_SIGNAL));
                                SignalList[k].Value.ScalarValue = 0;
                            }
                            //将帧信号中的值转换到字节数组中
                            LINMsg[0].Data = new Byte[8];
                            LDFDecoder.LDF_DataFrameToRaw(ref LINFrame, LINMsg[0].Data);
                            LINMsg[0].DataLen = LINFrame.Size;
                            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINOutMsg.Length);
                            ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, (Byte)LINIndex, LINMsg, pt, 1);
                            if (ret < USB2LIN_EX.LIN_EX_SUCCESS)
                            {
                                Console.WriteLine("MasterSync LIN failed!");
                                return;
                            }
                            else
                            {
                                Console.WriteLine(string.Format("MsgLen = {0}", ret));
                                for (int k = 0; k < ret; k++)
                                {
                                    LINOutMsg[k] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + k * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG));
                                    Console.Write(Marshal.PtrToStringAnsi(LINFrame.pName) + "[" + MSGTypeStr[LINMsg[0].MsgType] + "]" + string.Format(" SYNC[{0:X2}] PID[{1:X2}] ", LINOutMsg[k].Sync, LINOutMsg[k].PID));
                                    for (int z = 0; z < LINOutMsg[k].DataLen; z++)
                                    {
                                        Console.Write(string.Format("{0:X2} ", LINOutMsg[k].Data[z]));
                                    }
                                    Console.WriteLine(string.Format("[{0:X2}] [{1:D2}:{2:D2}:{3:D2}.{4:D3}]", LINOutMsg[k].Check, (LINOutMsg[k].Timestamp / 36000000) % 60, (LINOutMsg[k].Timestamp / 600000) % 60, (LINOutMsg[k].Timestamp / 10000) % 60, (LINOutMsg[k].Timestamp / 10) % 1000));
                                }
                            }
                        }
                        else
                        {
                            //该帧为主机读数据帧
                            LINMsg[0].MsgType   = USB2LIN_EX.LIN_EX_MSG_TYPE_MR;
                            LINMsg[0].PID       = LINFrame.ID;
                            LINMsg[0].Timestamp = (UInt32)SCHCmds.DelayOfMs;
                            LINMsg[0].Data      = new Byte[8];
                            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINOutMsg.Length);
                            ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, (Byte)LINIndex, LINMsg, pt, 1);
                            if (ret < USB2LIN_EX.LIN_EX_SUCCESS)
                            {
                                Console.WriteLine("MasterSync LIN failed!");
                                return;
                            }
                            else
                            {
                                Console.WriteLine(string.Format("MsgLen = {0}", ret));
                                for (int k = 0; k < ret; k++)
                                {
                                    LINOutMsg[k] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + k * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG));
                                    Console.Write(Marshal.PtrToStringAnsi(LINFrame.pName) + "[" + MSGTypeStr[LINMsg[0].MsgType] + "]" + string.Format(" SYNC[{0:X2}] PID[{1:X2}] ", LINOutMsg[k].Sync, LINOutMsg[k].PID));
                                    for (int z = 0; z < LINOutMsg[k].DataLen; z++)
                                    {
                                        Console.Write(string.Format("{0:X2} ", LINOutMsg[k].Data[z]));
                                    }
                                    Console.WriteLine(string.Format("[{0:X2}] [{1:D2}:{2:D2}:{3:D2}.{4:D3}]", LINOutMsg[k].Check, (LINOutMsg[k].Timestamp / 36000000) % 60, (LINOutMsg[k].Timestamp / 600000) % 60, (LINOutMsg[k].Timestamp / 10000) % 60, (LINOutMsg[k].Timestamp / 10) % 1000));
                                    if (LINOutMsg[i].DataLen > 0)
                                    {
                                        //将字节数组的数据解析到帧信号中
                                        LDFDecoder.LDF_DataRawToFrame(ref LINFrame, LINOutMsg[i].Data);
                                        //循环显示信号值
                                        for (int x = 0; x < LINFrame.SignalListLen; x++)
                                        {
                                            LDFDecoder.LDF_LIN_SIGNAL Signal = (LDFDecoder.LDF_LIN_SIGNAL)Marshal.PtrToStructure((IntPtr)((UInt32)LINFrame.pSignalList + x * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SIGNAL))), typeof(LDFDecoder.LDF_LIN_SIGNAL));
                                            if (Signal.ValueType == LDFDecoder.LDF_SIGNAL_VALUE_TYPE.LDF_SIGNAL_VALUE_TYPE_SCALAR)
                                            {
                                                Console.WriteLine(Marshal.PtrToStringAnsi(LINFrame.pName) + string.Format(" = {0}", Signal.Value.ScalarValue));
                                            }
                                            else
                                            {
                                                Console.Write(Marshal.PtrToStringAnsi(LINFrame.pName) + " = ");
                                                for (int y = 0; y < Signal.Size / 8; y++)
                                                {
                                                    Console.Write(string.Format("{0:X2} ", Signal.Value.ArrayValue[k]));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            USB_DEVICE.DEVICE_INFO DevInfo = new USB_DEVICE.DEVICE_INFO();
            Int32[] DevHandles             = new Int32[20];
            Int32   DevHandle = 0;
            Byte    CANIndex  = 0;
            bool    state;
            Int32   DevNum, ret;

            //扫描查找设备
            DevNum = USB_DEVICE.USB_ScanDevice(DevHandles);
            if (DevNum <= 0)
            {
                Console.WriteLine("No device connected!");
                return;
            }
            else
            {
                Console.WriteLine("Have {0} device connected!", DevNum);
            }
            DevHandle = DevHandles[0];
            //打开设备
            state = USB_DEVICE.USB_OpenDevice(DevHandle);
            if (!state)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            else
            {
                Console.WriteLine("Open device success!");
            }
            //获取固件信息
            StringBuilder FuncStr = new StringBuilder(256);

            state = USB_DEVICE.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr);
            if (!state)
            {
                Console.WriteLine("Get device infomation error!");
                return;
            }
            else
            {
                Console.WriteLine("Firmware Info:");
                Console.WriteLine("    Name:" + Encoding.Default.GetString(DevInfo.FirmwareName));
                Console.WriteLine("    Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate));
                Console.WriteLine("    Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF);
                Console.WriteLine("    Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF);
                Console.WriteLine("    Functions:" + DevInfo.Functions.ToString("X8"));
                Console.WriteLine("    Functions String:" + FuncStr);
            }
            //初始化配置CANFD设备
            USB2CANFD.CANFD_INIT_CONFIG CANFDConfig = new USB2CANFD.CANFD_INIT_CONFIG();
            CANFDConfig.Mode         = 0; //0-正常模式,1-自发自收模式
            CANFDConfig.RetrySend    = 1; //使能自动重传
            CANFDConfig.ISOCRCEnable = 1; //使能ISOCRC
            CANFDConfig.ResEnable    = 1; //使能内部终端电阻(若总线上没有终端电阻,则必须使能终端电阻才能正常传输数据)
            //波特率参数可以用TCANLINPro软件里面的波特率计算工具计算
            //仲裁段波特率参数,波特率=40M/NBT_BRP*(1+NBT_SEG1+NBT_SEG2)
            CANFDConfig.NBT_BRP  = 1;
            CANFDConfig.NBT_SEG1 = 63;
            CANFDConfig.NBT_SEG2 = 16;
            CANFDConfig.NBT_SJW  = 16;
            //数据域波特率参数,波特率=40M/DBT_BRP*(1+DBT_SEG1+DBT_SEG2)
            CANFDConfig.DBT_BRP  = 1;
            CANFDConfig.DBT_SEG1 = 15;
            CANFDConfig.DBT_SEG2 = 4;
            CANFDConfig.DBT_SJW  = 4;
            ret = USB2CANFD.CANFD_Init(DevHandle, CANIndex, ref CANFDConfig);
            if (ret != USB2CANFD.CANFD_SUCCESS)
            {
                Console.WriteLine("Config CANFD failed!");
                return;
            }
            else
            {
                Console.WriteLine("Config CANFD Success!");
            }
            //启动CAN数据接收
            ret = USB2CANFD.CANFD_StartGetMsg(DevHandle, CANIndex);
            if (ret != USB2CANFD.CANFD_SUCCESS)
            {
                Console.WriteLine("Start receive CANFD failed!");
                return;
            }
            else
            {
                Console.WriteLine("Start receive CANFD Success!");
            }
            //发送CAN数据
            USB2CANFD.CANFD_MSG[] CanMsg = new USB2CANFD.CANFD_MSG[5];
            IntPtr pCanSendMsg           = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG)) * CanMsg.Length);//申请缓冲区

            for (int i = 0; i < 5; i++)
            {
                CanMsg[i]       = new USB2CANFD.CANFD_MSG();
                CanMsg[i].Flags = 0;//bit[0]-BRS,bit[1]-ESI,bit[2]-FDF,bit[6..5]-Channel,bit[7]-RXD
                CanMsg[i].DLC   = 8;
                CanMsg[i].ID    = (UInt32)i;
                CanMsg[i].Data  = new Byte[64];
                for (int j = 0; j < CanMsg[i].DLC; j++)
                {
                    CanMsg[i].Data[j] = (Byte)j;
                }
                //将数组中的数据复制到数据缓冲区中
                IntPtr pPonitor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG)));
                Marshal.StructureToPtr(CanMsg[i], pPonitor, true);
                byte[] buffer = new byte[Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG))];
                Marshal.Copy(pPonitor, buffer, 0, Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG)));
                Marshal.Copy(buffer, 0, (IntPtr)((UInt32)pCanSendMsg + i * Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG))), Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG)));
                Marshal.FreeHGlobal(pPonitor);//释放缓冲区
            }
            int SendedMsgNum = USB2CANFD.CANFD_SendMsg(DevHandle, CANIndex, pCanSendMsg, CanMsg.Length);

            if (SendedMsgNum >= 0)
            {
                Console.WriteLine("Success send frames:{0}", SendedMsgNum);
            }
            else
            {
                Console.WriteLine("Send CAN data failed!");
            }
            Marshal.FreeHGlobal(pCanSendMsg);//释放缓冲区
            //延时
            System.Threading.Thread.Sleep(100);
            //读取接收数据缓冲中的数据
            USB2CANFD.CANFD_MSG[] CanMsgBuffer = new USB2CANFD.CANFD_MSG[10240];
            for (int i = 0; i < CanMsgBuffer.Length; i++)
            {
                CanMsgBuffer[i]      = new USB2CANFD.CANFD_MSG();
                CanMsgBuffer[i].Data = new Byte[64];
            }
            //将缓冲区转换成指针
            IntPtr pCanGetMsg = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG)) * CanMsgBuffer.Length);//申请缓冲区
            int    GetMsgNum  = USB2CANFD.CANFD_GetMsg(DevHandle, CANIndex, pCanGetMsg, CanMsgBuffer.Length);

            if (GetMsgNum > 0)
            {
                for (int i = 0; i < GetMsgNum; i++)
                {
                    CanMsgBuffer[i] = (USB2CANFD.CANFD_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pCanGetMsg + i * Marshal.SizeOf(typeof(USB2CANFD.CANFD_MSG))), typeof(USB2CANFD.CANFD_MSG));
                    Console.WriteLine("CanMsg[{0}].ID = {1}", i, CanMsgBuffer[i].ID & USB2CANFD.CANFD_MSG_FLAG_ID_MASK);
                    Console.WriteLine("CanMsg[{0}].TimeStamp = {1}", i, CanMsgBuffer[i].TimeStamp);
                    Console.Write("CanMsg[{0}].Data = ", i);
                    for (int j = 0; j < CanMsgBuffer[i].DLC; j++)
                    {
                        Console.Write("{0:X2} ", CanMsgBuffer[i].Data[j]);
                    }
                    Console.WriteLine("\n");
                }
            }
            else if (GetMsgNum < 0)
            {
                Console.WriteLine("Get CAN data error!");
            }
            Marshal.FreeHGlobal(pCanGetMsg);//释放缓冲区
            //停止接收数据
            ret = USB2CANFD.CANFD_StopGetMsg(DevHandle, CANIndex);
            if (ret != USB2CANFD.CANFD_SUCCESS)
            {
                Console.WriteLine("Stop receive CANFD failed!");
                return;
            }
            else
            {
                Console.WriteLine("Stop receive CANFD Success!");
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            USB_DEVICE.DEVICE_INFO DevInfo = new USB_DEVICE.DEVICE_INFO();
            Int32[] DevHandles             = new Int32[20];
            Int32   DevHandle = 0;
            bool    state;
            Int32   DevNum, ret;

            //扫描查找设备
            DevNum = USB_DEVICE.USB_ScanDevice(DevHandles);
            if (DevNum <= 0)
            {
                Console.WriteLine("No device connected!");
                return;
            }
            else
            {
                Console.WriteLine("Have {0} device connected!", DevNum);
            }
            DevHandle = DevHandles[0];
            //打开设备
            state = USB_DEVICE.USB_OpenDevice(DevHandle);
            if (!state)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            else
            {
                Console.WriteLine("Open device success!");
            }
            //获取固件信息
            StringBuilder FuncStr = new StringBuilder(256);

            state = USB_DEVICE.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr);
            if (!state)
            {
                Console.WriteLine("Get device infomation error!");
                return;
            }
            else
            {
                Console.WriteLine("Firmware Info:");
                Console.WriteLine("    Name:" + Encoding.Default.GetString(DevInfo.FirmwareName));
                Console.WriteLine("    Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate));
                Console.WriteLine("    Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF);
                Console.WriteLine("    Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF);
                Console.WriteLine("    Functions:" + DevInfo.Functions.ToString("X8"));
                Console.WriteLine("    Functions String:" + FuncStr);
            }
            //启动I2C Sniffer
            GC.KeepAlive(get_data_callback);
            ret = I2C_SNIFFER.I2C_SnifferStart(DevHandles[0], 0, 5000000, get_data_callback);
            if (ret != I2C_SNIFFER.SUCCESS)
            {
                Console.WriteLine("Start i2c sniffer faild!");
                return;
            }
            Console.ReadLine();
            I2C_SNIFFER.I2C_SnifferStop(DevHandles[0]);
            USB_DEVICE.USB_CloseDevice(DevHandles[0]);
        }