using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Linq; using System.Diagnostics; using System.Text; using System.Threading.Tasks; using System.Threading; using USB2XXX; namespace Can { class TooMossDevice : CanDevice { private Int32 DevHandle; private Byte WriteCANIndex = 0; private Byte ReadCANIndex = 0; Thread CanSendMsgThread; List CanSendMsgBuffer; object CanSendMsgLock; public TooMossDevice() { CanSendMsgBuffer = new List(); CanSendMsgLock = new object(); CanSendMsgThread = new Thread(new ThreadStart(CanSendMessageThreadLoop)); CanSendMsgThread.IsBackground = true; CanSendMsgThread.Start(); } ~TooMossDevice() { CanSendMsgThread.Join(); CanSendMsgThread.Abort(); } public override string CanInit() { USB_DEVICE.DEVICE_INFO DevInfo = new USB_DEVICE.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; bool state; Int32 DevNum, ret; //扫描查找设备 DevNum = USB_DEVICE.USB_ScanDevice(DevHandles); if (DevNum <= 0) { return "No device connected!"; } else { //Console.WriteLine("Have {0} device connected!", DevNum); } DevHandle = DevHandles[0]; //打开设备 state = USB_DEVICE.USB_OpenDevice(DevHandle); if (!state) { return "Open device error!"; } else { //Console.WriteLine("Open device success!"); } //获取固件信息 StringBuilder FuncStr = new StringBuilder(256); state = USB_DEVICE.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr); if (!state) { return "Get device infomation error!"; } 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); */ } //初始化配置CAN USB2CAN.CAN_INIT_CONFIG CANConfig = new USB2CAN.CAN_INIT_CONFIG(); ret = USB2CAN.CAN_GetCANSpeedArg(DevHandle, ref CANConfig, 500000); if (ret != USB2CAN.CAN_SUCCESS) { return "Get CAN Speed failed!"; } else { //Console.WriteLine("Get CAN Speed Success!"); } #if false CANConfig.CAN_Mode = 1;//环回模式 #else CANConfig.CAN_Mode = 0x80;//正常模式并接入终端电阻 #endif ret = USB2CAN.CAN_Init(DevHandle, WriteCANIndex, ref CANConfig); if (ret != USB2CAN.CAN_SUCCESS) { return "Config CAN failed!"; } else { //Console.WriteLine("Config CAN Success!"); } ret = USB2CAN.CAN_Init(DevHandle, ReadCANIndex, ref CANConfig); if (ret != USB2CAN.CAN_SUCCESS) { return "Config CAN failed!"; } else { //Console.WriteLine("Config CAN Success!"); } //配置过滤器,接收所有数据 /*USB2CAN.CAN_FILTER_CONFIG CANFilter = new USB2CAN.CAN_FILTER_CONFIG(); CANFilter.Enable = 1; CANFilter.ExtFrame = 0; CANFilter.FilterIndex = 0; CANFilter.FilterMode = 0; CANFilter.MASK_IDE = 0; CANFilter.MASK_RTR = 0; CANFilter.MASK_Std_Ext = 0; CANFilter.ID_Std_Ext = 0x475; ret = USB2CAN.CAN_Filter_Init(DevHandle, ReadCANIndex, ref CANFilter); if (ret != USB2CAN.CAN_SUCCESS) { Console.WriteLine("Config CAN Filter failed!"); return; } else { Console.WriteLine("Config CAN Filter Success!"); }*/ //启动CAN接收数据 ret = USB2CAN.CAN_StartGetMsg(DevHandle, ReadCANIndex); if (ret != USB2CAN.CAN_SUCCESS) { return "Start CAN failed!"; } else { //Console.WriteLine("Start CAN Success!"); } return "OK"; } public override string CanDeInit() { //停止CAN USB2CAN.CAN_StopGetMsg(DevHandle, ReadCANIndex); return "OK"; } public override void CanSendMessage(uint id, byte[] data, int len) { CanMessage msg = new CanMessage(); msg.Id = id; msg.Len = len; msg.IsTx = true; Array.Copy(data, 0, msg.Data, 0, len); lock (CanSendMsgLock) { CanSendMsgBuffer.Add(msg); } } private void CanSendMessageThreadLoop() { CanMessage[] msg; for (;;) { msg = new CanMessage[0]; lock (CanSendMsgLock) { msg = CanSendMsgBuffer.ToArray(); CanSendMsgBuffer.Clear(); } USB2CAN.CAN_MSG[] usb2CanMsg = new USB2CAN.CAN_MSG[msg.Length]; for (int i = 0; i < usb2CanMsg.Length; i++) { usb2CanMsg[i] = new USB2CAN.CAN_MSG(); usb2CanMsg[i].ExternFlag = 0; usb2CanMsg[i].RemoteFlag = 0; usb2CanMsg[i].ID = msg[i].Id; usb2CanMsg[i].DataLen = (byte)msg[i].Len; usb2CanMsg[i].Data = msg[i].Data; } //Array.Copy(data, 0, CanMsg[0].Data, 0, len); int SendedNum = USB2CAN.CAN_SendMsg(DevHandle, WriteCANIndex, usb2CanMsg, (UInt32)usb2CanMsg.Length); Thread.Sleep(20); } } public override bool CanReadMessage(ref UInt32 id, byte[] data, ref int len) { USB2CAN.CAN_MSG[] CanMsgBuffer = new USB2CAN.CAN_MSG[1]; //申请存储数据缓冲区 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)) * CanMsgBuffer.Length); int CanNum = USB2CAN.CAN_GetMsgWithSize(DevHandle, ReadCANIndex, pt, CanMsgBuffer.Length); if (CanNum > 0) { //从缓冲区中获取数据 //CanMsgBuffer[0] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + 0 * Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))), typeof(USB2CAN.CAN_MSG)); CanMsgBuffer[0] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt), typeof(USB2CAN.CAN_MSG)); id = CanMsgBuffer[0].ID; len = CanMsgBuffer[0].DataLen; Array.Copy(CanMsgBuffer[0].Data, 0, data, 0, CanMsgBuffer[0].Data.Length); Marshal.FreeHGlobal(pt); return true; } else { Marshal.FreeHGlobal(pt); return false; } } } }