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); } //初始化配置CAN USB2CAN.CAN_INIT_CONFIG CANConfig = new USB2CAN.CAN_INIT_CONFIG(); #if CAN_MODE_LOOPBACK CANConfig.CAN_Mode = 1; //环回模式 #else CANConfig.CAN_Mode = 0; //正常模式 #endif CANConfig.CAN_ABOM = 0; //禁止自动离线 CANConfig.CAN_NART = 1; //禁止报文重传 CANConfig.CAN_RFLM = 0; //FIFO满之后覆盖旧报文 CANConfig.CAN_TXFP = 1; //发送请求决定发送顺序 //配置波特率,波特率 = 100M/(BRP*(SJW+BS1+BS2)) CANConfig.CAN_BRP = 2; CANConfig.CAN_BS1 = 15; CANConfig.CAN_BS2 = 5; CANConfig.CAN_SJW = 1; ret = USB2CAN.CAN_Init(DevHandle, CANIndex, ref CANConfig); if (ret != USB2CAN.CAN_SUCCESS) { Console.WriteLine("Config CAN failed!"); return; } 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; ret = USB2CAN.CAN_Filter_Init(DevHandle, CANIndex, 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, CANIndex); if (ret != USB2CAN.CAN_SUCCESS) { Console.WriteLine("Start CAN failed!"); return; } else { Console.WriteLine("Start CAN Success!"); } #if CAN_SEND_MSG//发送CAN帧 USB2CAN.CAN_MSG[] CanMsg = new USB2CAN.CAN_MSG[5]; for (int i = 0; i < 5; i++) { CanMsg[i] = new USB2CAN.CAN_MSG(); CanMsg[i].ExternFlag = 0; CanMsg[i].RemoteFlag = 0; CanMsg[i].ID = (UInt32)i; CanMsg[i].DataLen = 8; CanMsg[i].Data = new Byte[CanMsg[i].DataLen]; for (int j = 0; j < CanMsg[i].DataLen; j++) { CanMsg[i].Data[j] = (Byte)j; } } int SendedNum = USB2CAN.CAN_SendMsg(DevHandle, CANIndex, CanMsg, (UInt32)CanMsg.Length); if (SendedNum >= 0) { Console.WriteLine("Success send frames:{0}", SendedNum); } else { Console.WriteLine("Send CAN data failed!"); } #endif #if CAN_GET_STATUS USB2CAN.CAN_STATUS CANStatus = new USB2CAN.CAN_STATUS(); ret = USB2CAN.CAN_GetStatus(DevHandle, CANIndex, ref CANStatus); if (ret == USB2CAN.CAN_SUCCESS) { Console.WriteLine("TSR = {0:X8}", CANStatus.TSR); Console.WriteLine("ESR = {0:X8}", CANStatus.ESR); } else { Console.WriteLine("Get CAN status error!\n"); } #endif //延时 System.Threading.Thread.Sleep(500); #if CAN_GET_MSG for (int t = 0; t < 10; t++) { USB2CAN.CAN_MSG[] CanMsgBuffer = new USB2CAN.CAN_MSG[1024]; //申请存储数据缓冲区 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)) * CanMsgBuffer.Length); int CanNum = USB2CAN.CAN_GetMsgWithSize(DevHandle, CANIndex, pt, CanMsgBuffer.Length); if (CanNum > 0) { for (int i = 0; i < CanNum; i++) { //从缓冲区中获取数据 CanMsgBuffer[i] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))), typeof(USB2CAN.CAN_MSG)); Console.WriteLine("CanMsg[{0}].ID = {1}", i, CanMsgBuffer[i].ID); Console.WriteLine("CanMsg[{0}].TimeStamp = {1}", i, CanMsgBuffer[i].TimeStamp); Console.Write("CanMsg[{0}].Data = ", i); for (int j = 0; j < CanMsgBuffer[i].DataLen; j++) { Console.Write("{0:X2} ", CanMsgBuffer[i].Data[j]); } Console.WriteLine("\n"); } } else if (CanNum < 0) { Console.WriteLine("Get CAN data error!"); } //延时 System.Threading.Thread.Sleep(100); //释放申请的数据缓冲区 Marshal.FreeHGlobal(pt); } //停止CAN USB2CAN.CAN_StopGetMsg(DevHandle, CANIndex); #endif }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte ADC_Channel = 0x01; bool state; Int32 DevNum, ret; Int32 ADC_NUMS = 10; UInt16[] Buffer = new UInt16[40960]; //扫描查找设备 DevNum = usb_device.USB_ScanDevice(DevHandles); if (DevNum <= 0) { Console.WriteLine("No device connected!"); Console.ReadLine(); 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!"); Console.ReadLine(); return; } else { Console.WriteLine("Open device success!"); } //获取固件信息 #if GET_FIRMWARE_INFO StringBuilder FuncStr = new StringBuilder(256); state = usb_device.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr); if (!state) { Console.WriteLine("Get device infomation error!"); Console.ReadLine(); 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); } #endif //初始化设备 ret = USB2ADC.ADC_Init(DevHandle, ADC_Channel, 1000000); if (ret != USB2ADC.ADC_SUCCESS) { Console.WriteLine("Init adc error!"); Console.ReadLine(); return; } //读取ADC数据 ret = USB2ADC.ADC_Read(DevHandle, Buffer, ADC_NUMS); if (ret != USB2ADC.ADC_SUCCESS) { Console.WriteLine("Read adc error!\n"); Console.ReadLine(); return; } else { for (int i = 0; i < ADC_NUMS * BitCount(ADC_Channel); i++) { Console.WriteLine("ADC Data[%d] = %fV\n", i, (Buffer[i] * 3.3) / 4095); } } //关闭设备 usb_device.USB_CloseDevice(DevHandle); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_FLASH_CONFIG SPIFlashConfig = new USB2SPI.SPI_FLASH_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 SPIIndex = 0; bool state; Int32 DevNum, ret; Byte[] WriteBuffer = new Byte[64]; Byte[] ReadBuffer = new Byte[20480]; //扫描查找设备 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];//选择设备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); Console.WriteLine(" Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8")); } //根据W25Q64配置相关参数 SPIFlashConfig.CMD_EraseSector = 0x20; SPIFlashConfig.CMD_ReadData = 0x03; SPIFlashConfig.CMD_ReadFast = 0x0B; SPIFlashConfig.CMD_ReadID = 0x9F; SPIFlashConfig.CMD_ReadStatus = 0x05; SPIFlashConfig.CMD_WriteEnable = 0x06; SPIFlashConfig.CMD_WritePage = 0x02; SPIFlashConfig.CMD_EraseChip = 0xC7; SPIFlashConfig.EraseSectorAddressBytes = 3; SPIFlashConfig.ID_Length = 3; SPIFlashConfig.NumPages = 32768; SPIFlashConfig.PageSize = 256; SPIFlashConfig.ReadDataAddressBytes = 3; SPIFlashConfig.ReadFastAddressBytes = 3; SPIFlashConfig.SectorSize = 4096; SPIFlashConfig.WritePageAddressBytes = 3; ret = USB2SPI.SPI_FlashInit(DevHandle, SPIIndex, 50000000 >> 2, ref SPIFlashConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize Device Error!"); return; } //读取芯片ID ret = USB2SPI.SPI_FlashReadID(DevHandle, SPIIndex, SPIFlashConfig.ID); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Get Device ID Error!"); return; } else { Console.Write("ID = "); for (int i = 0; i < SPIFlashConfig.ID_Length; i++) { Console.Write(SPIFlashConfig.ID[i].ToString("X2")); } Console.WriteLine("\n"); if ((SPIFlashConfig.ID[0] == 0xFF) && (SPIFlashConfig.ID[1] == 0xFF)) { return; } } //擦除扇区 ret = USB2SPI.SPI_FlashEraseSector(DevHandle, SPIIndex, 0, 1); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Erase Sector Error!"); return; } Byte[] TestBuffer = new Byte[20 * 1024]; for (int i = 0; i < TestBuffer.Length; i++) { TestBuffer[i] = (Byte)i; } ret = USB2SPI.SPI_FlashWrite(DevHandle, SPIIndex, 0, TestBuffer, 256); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Flash Write Error!"); return; } ret = USB2SPI.SPI_FlashReadFast(DevHandle, SPIIndex, 0, TestBuffer, 256); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Flash Read Error!"); return; } for (int i = 0; i < 256; i++) { if ((i % 16) == 0) { Console.WriteLine(""); } Console.Write(TestBuffer[i].ToString("X2") + " "); } Console.WriteLine("\n"); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte LINIndex = 0; bool state; Int32 DevNum, ret = 0; String[] MSGTypeStr = new String[10] { "UN", "MW", "MR", "SW", "SR", "BK", "SY", "ID", "DT", "CK" }; String[] CKTypeStr = new String[5] { "STD", "EXT", "USER", "NONE", "ERROR" }; //扫描查找设备 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); } //初始化配置LIN #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST //主机写数据 USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[5]; USB2LIN_EX.LIN_EX_MSG[] LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10]; LINMsg[0] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[0].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[0].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 for (int f = 1; f < LINMsg.Length; f++) { LINMsg[f] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[f].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW;//主机发送数据 LINMsg[f].DataLen = 8; LINMsg[f].Data = new Byte[8]; for (int i = 0; i < LINMsg[1].DataLen; i++) { LINMsg[f].Data[i] = (Byte)((f << 4) | i); } LINMsg[f].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[f].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[f].PID = (Byte)(f + 1); } //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINOutMsg.Length); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, LINIndex, LINMsg, pt, LINMsg.Length); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); #else Console.WriteLine("Start Get LIN Data..."); //设置ID为LIN_EX_MSG_TYPE_SW模式,这样主机就可以读取到数据 USB2LIN_EX.LIN_EX_MSG[] LINSlaveMsg = new USB2LIN_EX.LIN_EX_MSG[10]; for (int i = 0; i < 10; i++) { LINSlaveMsg[i] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[i].Data = new Byte[8]; LINSlaveMsg[i].PID = (Byte)i; LINSlaveMsg[i].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; LINSlaveMsg[i].DataLen = 7; for (int j = 0; j < LINSlaveMsg[i].DataLen; j++) { LINSlaveMsg[i].Data[j] = (Byte)((i << 4) | j); } LINSlaveMsg[i].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW;//从机发送数据模式 } //设置从机模式下所有ID都为从接收数据模式,这样就可以获取到主机发送过来的所有数据,初始化配置为从机后,默认所有ID都为接收数据模式,所以若是监听LIN总线数据,这个函数可以不用调用 ret = USB2LIN_EX.LIN_EX_SlaveSetIDMode(DevHandle, LINIndex, LINSlaveMsg, 10); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN ID Mode failed!"); return; } else { Console.WriteLine("Config LIN ID Mode Success!"); } //循环获取接收到的数据,该操作可以用作LIN总线数据监控 Console.WriteLine("Start get data..."); while (true) { USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1024];//缓冲区尽量大一点,防止益处 //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINMsg.Length); ret = USB2LIN_EX.LIN_EX_SlaveGetData(DevHandle, LINIndex, pt); for (int i = 0; i < ret; i++) { LINMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.WriteLine("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINMsg[i].MsgType], LINMsg[i].Sync, LINMsg[i].PID); for (int j = 0; j < LINMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}]:{3}]:{4}].{5}]]\n", CKTypeStr[LINMsg[i].CheckType], LINMsg[i].Check, (LINMsg[i].Timestamp / 36000000) % 60, (LINMsg[i].Timestamp / 600000) % 60, (LINMsg[i].Timestamp / 10000) % 60, (LINMsg[i].Timestamp / 10) % 1000); } System.Threading.Thread.Sleep(100); } //释放内存 Marshal.FreeHGlobal(pt); #endif Console.WriteLine("Close Device!"); //关闭设备 usb_device.USB_CloseDevice(DevHandle); return; }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2PWM.PWM_CONFIG PWMConfig = new USB2PWM.PWM_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; bool state; Int32 DevNum, ret; Byte[] WriteBuffer = new Byte[64]; Byte[] ReadBuffer = new Byte[20480]; //扫描查找设备 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); Console.WriteLine(" Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8")); } PWMConfig.ChannelMask = 0xFF;//初始化所有通道 PWMConfig.Polarity = new byte[8]; for (int i = 0; i < 8; i++) { PWMConfig.Polarity[i] = 1;//将所有PWM通道都设置为正极性 } PWMConfig.Precision = new ushort[8]; for (int i = 0; i < 8; i++) { PWMConfig.Precision[i] = 100;//将所有通道的占空比调节精度都设置为1% } PWMConfig.Prescaler = new ushort[8]; for (int i = 0; i < 8; i++) { PWMConfig.Prescaler[i] = 10;//将所有通道的预分频器都设置为10,则PWM输出频率为200MHz/(PWMConfig.Precision*PWMConfig.Prescaler) } PWMConfig.Pulse = new ushort[8]; for (int i = 0; i < 8; i++) { PWMConfig.Pulse[i] = (UInt16)(PWMConfig.Precision[i] * 30 / 100);//将所有通道的占空比都设置为30% } //初始化PWM ret = USB2PWM.PWM_Init(DevHandle, ref PWMConfig); if (ret != USB2PWM.PWM_SUCCESS) { Console.WriteLine("Initialize pwm faild!\n"); Console.ReadLine(); return; } else { Console.WriteLine("Initialize pwm sunccess!\n"); } //启动PWM,RunTimeOfUs之后自动停止,利用该特性可以控制输出脉冲个数,脉冲个数=RunTimeOfUs*200/(PWMConfig.Precision*PWMConfig.Prescaler) Int32 RunTimeOfUs = 10000; ret = USB2PWM.PWM_Start(DevHandle, PWMConfig.ChannelMask, RunTimeOfUs); if (ret != USB2PWM.PWM_SUCCESS) { Console.WriteLine("Start pwm faild!\n"); Console.ReadLine(); return; } else { Console.WriteLine("Start pwm sunccess!\n"); } //停止PWM /* * ret = USB2PWM.PWM_Stop(DevHandle,PWMConfig.ChannelMask); * if (ret != USB2PWM.PWM_SUCCESS) * { * Console.WriteLine("Stop pwm faild!\n"); * Console.ReadLine(); * return; * }else{ * Console.WriteLine("Stop pwm sunccess!\n"); * } */ //关闭设备 usb_device.USB_CloseDevice(DevHandle); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte LINIndex = 0; bool state; Int32 DevNum, ret = 0; String[] MSGTypeStr = new String[10] { "UN", "MW", "MR", "SW", "SR", "BK", "SY", "ID", "DT", "CK" }; String[] CKTypeStr = new String[5] { "STD", "EXT", "USER", "NONE", "ERROR" }; //扫描查找设备 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); } //初始化配置LIN #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST /************************************主机写数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[5]; USB2LIN_EX.LIN_EX_MSG[] LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10]; //添加第一帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 MsgIndex++; //添加第二帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; //主机发送数据 LINMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[MsgIndex].PID = 0x32; //可以只传入ID,校验位底层会自动计算 LINMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //根据自己实际情况修改数据 for (int i = 0; i < LINMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //添加第三帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; //主机发送数据 LINMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[MsgIndex].PID = 0x32; //可以只传入ID,校验位底层会自动计算 LINMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //根据自己实际情况修改数据 for (int i = 0; i < LINMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要发送更多帧数据,请按照前面的方式继续添加********************/ //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * MsgIndex); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, LINIndex, LINMsg, pt, MsgIndex); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); //释放内存 Marshal.FreeHGlobal(pt); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); /************************************主机读数据************************************/ MsgIndex = 0; //添加第一帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 MsgIndex++; //添加第二帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MR; //主机读数据 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].PID = 0x33; //可以只传入ID,校验位底层会自动计算 MsgIndex++; //添加第三帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MR; //主机读数据 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].PID = 0x33; //可以只传入ID,校验位底层会自动计算 MsgIndex++; /********************需要发送更多帧数据,请按照前面的方式继续添加********************/ //将数组转换成指针 pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * MsgIndex); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, LINIndex, LINMsg, pt, MsgIndex); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); //释放内存 Marshal.FreeHGlobal(pt); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++)//实际读取到的字节数据是LINOutMsg[i].DataLen的值 { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); #else /************************************从机发送数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; //设置ID为LIN_EX_MSG_TYPE_SW模式,从机接收到主机发送的帧头后就会返回预先定义好的数据 USB2LIN_EX.LIN_EX_MSG[] LINSlaveMsg = new USB2LIN_EX.LIN_EX_MSG[3]; //配置第一帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x34; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第二帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x35; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第三帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x36; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要配置更多帧,请按照前面的方式继续添加********************/ //默认所有ID都是从机接收数据模式,该模式下可以用于数据的监听 //调用该函数后,只会修改传入对应ID的模式,其他的不会被改变 ret = USB2LIN_EX.LIN_EX_SlaveSetIDMode(DevHandle, LINIndex, LINSlaveMsg, MsgIndex); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN ID Mode failed!"); return; } else { Console.WriteLine("Config LIN ID Mode Success!"); } //循环获取接收到的数据,该操作可以用作LIN总线数据监控 Console.WriteLine("Start Get LIN Data..."); int RunTimeMs = 10000;//只运行10秒钟,10秒钟后结束数据监听,可自行修改 while (RunTimeMs > 0) { USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1024];//缓冲区尽量大一点,防止益处 //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINMsg.Length); ret = USB2LIN_EX.LIN_EX_SlaveGetData(DevHandle, LINIndex, pt); for (int i = 0; i < ret; i++) { LINMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.WriteLine("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINMsg[i].MsgType], LINMsg[i].Sync, LINMsg[i].PID); for (int j = 0; j < LINMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}]:{3}]:{4}].{5}]]\n", CKTypeStr[LINMsg[i].CheckType], LINMsg[i].Check, (LINMsg[i].Timestamp / 36000000) % 60, (LINMsg[i].Timestamp / 600000) % 60, (LINMsg[i].Timestamp / 10000) % 60, (LINMsg[i].Timestamp / 10) % 1000); } //释放内存 Marshal.FreeHGlobal(pt); System.Threading.Thread.Sleep(10); RunTimeMs -= 10; } #endif Console.WriteLine("Close Device!"); //关闭设备 usb_device.USB_CloseDevice(DevHandle); return; }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; bool state; Int32 DevNum, ret; Thread CANSendMsgThread = new Thread(new ThreadStart(SendMsgThread)); Thread CANReadMsgThread = new Thread(new ThreadStart(ReadMsgThread)); //扫描查找设备 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); } //初始化配置CAN USB2CAN.CAN_INIT_CONFIG CANConfig = new USB2CAN.CAN_INIT_CONFIG(); CANConfig.CAN_Mode = 0x80; //正常模式 CANConfig.CAN_ABOM = 0; //禁止自动离线 CANConfig.CAN_NART = 1; //禁止报文重传 CANConfig.CAN_RFLM = 0; //FIFO满之后覆盖旧报文 CANConfig.CAN_TXFP = 1; //发送请求决定发送顺序 //配置波特率,波特率 = 42M/(BRP*(SJW+BS1+BS2)) CANConfig.CAN_BRP = 2; CANConfig.CAN_BS1 = 15; CANConfig.CAN_BS2 = 5; CANConfig.CAN_SJW = 1; ret = USB2CAN.CAN_Init(DevHandle, CANSendIndex, ref CANConfig); if (ret != USB2CAN.CAN_SUCCESS) { Console.WriteLine("Config Send CAN failed!"); return; } else { Console.WriteLine("Config Send CAN Success!"); } ret = USB2CAN.CAN_Init(DevHandle, CANReadIndex, ref CANConfig); if (ret != USB2CAN.CAN_SUCCESS) { Console.WriteLine("Config Read CAN failed!"); return; } else { Console.WriteLine("Config Read 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; ret = USB2CAN.CAN_Filter_Init(DevHandle, CANReadIndex, 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, CANReadIndex); if (ret != USB2CAN.CAN_SUCCESS) { Console.WriteLine("Start CAN failed!"); return; } else { Console.WriteLine("Start CAN Success!"); } //启动接收数据线程 CANReadMsgFlag = true; CANReadMsgThread.Start(); //启动发送数据线程 CANSendMsgFlag = true; CANSendMsgThread.Start(); //按下回车后结束发送接收线程 Console.ReadLine(); //结束线程 CANSendMsgFlag = false; CANReadMsgFlag = false; CANSendMsgThread.Join(); CANReadMsgThread.Join(); //停止CAN USB2CAN.CAN_StopGetMsg(DevHandle, CANReadIndex); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 IICIndex = 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 USB2IIC.IIC_CONFIG IICConfig = new USB2IIC.IIC_CONFIG(); IICConfig.AddrBits = 7; //7bit地址模式 IICConfig.ClockSpeedHz = 400000; //时钟频率400KHz IICConfig.Master = 1; //主机模式 ret = USB2IIC.IIC_Init(DevHandle, IICIndex, ref IICConfig); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Config IIC failed!"); return; } else { Console.WriteLine("Config IIC IIC_SUCCESS!"); } //配置芯片 UInt16 Config = (1 << 15) | //Begin a single conversion(when in power-down mode) (0 << 12) | //AINP=AIN0 and AINN=AIN1(default) (1 << 9) | //FS=±4.096V (0 << 8) | //0:Continuous conversion mode 1:Power-down single-shot mode (7 << 5) | //860SPS (0 << 4) | //Traditional comparator with hysteresis (0 << 3) | //Active low (1 << 2) | //Latching comparator Non-latching comparator (0 << 0); //Assert after one conversion byte[] WriteDataBuffer = new byte[3]; WriteDataBuffer[0] = 0x01; WriteDataBuffer[1] = (byte)(Config >> 8); WriteDataBuffer[2] = (byte)(Config & 0xFF); Int16 SlaveAddr = 0x48; //调用写数据函数 ret = USB2IIC.IIC_WriteBytes(DevHandle, IICIndex, SlaveAddr, WriteDataBuffer, WriteDataBuffer.Length, 10); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Write IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } else { Console.WriteLine("Config ADS1115 success!"); } //循环读取数据 int ReadDataNum = 10; byte[] ReadDataBuffer = new byte[2]; for (int i = 0; i < ReadDataNum; i++) { WriteDataBuffer[0] = 0x00; ret = USB2IIC.IIC_WriteReadBytesOfEvent(DevHandle, IICIndex, SlaveAddr, WriteDataBuffer, 1, ReadDataBuffer, ReadDataBuffer.Length, 1 << 2, 0x10, 200); //ret = USB2IIC.IIC_WriteReadBytes(DevHandle, IICIndex, SlaveAddr, WriteDataBuffer, 1, ReadDataBuffer, ReadDataBuffer.Length, 10); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("WriteRead IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } else { Int16 ADCData = (Int16)((ReadDataBuffer[0] << 8) | (ReadDataBuffer[1])); Console.WriteLine(String.Format("ADCData[{0:D}] = {1:F6} V", i, ADCData * 4.096 / 32768.0)); } System.Threading.Thread.Sleep(100); } }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte LINIndex = 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); } //初始化配置LIN USB2LIN.LIN_CONFIG LINConfig = new USB2LIN.LIN_CONFIG(); LINConfig.BaudRate = 19200; LINConfig.BreakBits = USB2LIN.LIN_BREAK_BITS_11; LINConfig.CheckMode = USB2LIN.LIN_CHECK_MODE_EXT; #if LIN_MASTER LINConfig.MasterMode = USB2LIN.LIN_MASTER; #else LINConfig.MasterMode = USB2LIN.LIN_SLAVE; #endif ret = USB2LIN.LIN_Init(DevHandle, LINIndex, ref LINConfig); if (ret != USB2LIN.LIN_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER //主机写数据 for (Byte ID = 0; ID < 10; ID++) { USB2LIN.LIN_MSG[] msg = new USB2LIN.LIN_MSG[1]; msg[0].Data = new Byte[9]; for (Byte i = 0; i < 8; i++) { msg[0].Data[i] = (Byte)(ID + i); } msg[0].DataLen = 8; msg[0].ID = ID; ret = USB2LIN.LIN_Write(DevHandle, LINIndex, msg, 1); if (ret != USB2LIN.LIN_SUCCESS) { Console.WriteLine("LIN write data failed!\n"); return; } else { Console.WriteLine("LIN write data success!\n"); } //延时 System.Threading.Thread.Sleep(10); } //主机读数据 for (Byte ID = 0; ID < 10; ID++) { USB2LIN.LIN_MSG[] msg = new USB2LIN.LIN_MSG[1]; msg[0].Data = new Byte[9]; msg[0].ID = ID; IntPtr[] ptArray = new IntPtr[1]; ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN.LIN_MSG)) * msg.Length); IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN.LIN_MSG))); Marshal.Copy(ptArray, 0, pt, 1); //将数组中的数据拷贝到指针所指区域 for (int k = 0; k < msg.Length; k++) { Marshal.StructureToPtr(msg[k], (IntPtr)((UInt32)pt + k * Marshal.SizeOf(typeof(USB2LIN.LIN_MSG))), true); } ret = USB2LIN.LIN_Read(DevHandle, LINIndex, pt, 1); if (ret < USB2LIN.LIN_SUCCESS) { Console.WriteLine("LIN read data failed!\n"); return; } else { msg[0] = (USB2LIN.LIN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + 0 * Marshal.SizeOf(typeof(USB2LIN.LIN_MSG))), typeof(USB2LIN.LIN_MSG)); Console.Write("Master LIN Read: ID = 0x{0:X2} Data = ", msg[0].ID); for (int i = 0; i < msg[0].DataLen; i++) { Console.Write("0x{0:X2} ", msg[0].Data[i]); } Console.WriteLine(""); } } #else //设置从机模式下所有ID都为从接收数据模式,这样就可以获取到主机发送过来的所有数据,初始化配置为从机后,默认所有ID都为接收数据模式,所以若是监听LIN总线数据,这个函数可以不用调用 USB2LIN.LIN_MSG[] LINSlaveData = new USB2LIN.LIN_MSG[64]; for (Byte i = 0; i < 64; i++) { LINSlaveData[i] = new USB2LIN.LIN_MSG(); LINSlaveData[i].DataLen = 9; //最大8Byte数据+1Byte和校验 LINSlaveData[i].ID = i; //ID值 LINSlaveData[i].Data = new Byte[9]; } ret = USB2LIN.LIN_SlaveSetIDMode(DevHandle, LINIndex, USB2LIN.LIN_SLAVE_READ, LINSlaveData, 64); if (ret != USB2LIN.LIN_SUCCESS) { Console.WriteLine("Set LIN operation mode failed!"); return; } else { Console.WriteLine("Set LIN operation mode success!"); } //从机接收数据,若要连续不断的监控LIN总线数据,可以循环调用LIN_SlaveGetData函数 USB2LIN.LIN_MSG[] LINSlaveDataBuffer = new USB2LIN.LIN_MSG[1024];//为了防止缓冲区溢出,可以将接收数据缓冲区设置大一点 for (int i = 0; i < LINSlaveDataBuffer.Length; i++) { LINSlaveDataBuffer[i] = new USB2LIN.LIN_MSG(); LINSlaveDataBuffer[i].Data = new Byte[9]; } while (true) { IntPtr[] ptArray = new IntPtr[2]; ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN.LIN_MSG)) * LINSlaveDataBuffer.Length); IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN.LIN_MSG))); Marshal.Copy(ptArray, 0, pt, 1); ret = USB2LIN.LIN_SlaveGetData(DevHandle, LINIndex, pt); if (ret < USB2LIN.LIN_SUCCESS) { Console.WriteLine("LIN slave read data error!"); return; } else if (ret == 0) { //Console.WriteLine("LIN slave read no data!"); } else { Console.WriteLine("LIN slave read data:"); for (int i = 0; i < ret; i++) { LINSlaveDataBuffer[i] = (USB2LIN.LIN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN.LIN_MSG))), typeof(USB2LIN.LIN_MSG)); Console.Write("Data[{0}]: ", i); Console.Write("ID = 0x{0:X2} ", LINSlaveDataBuffer[i].ID); Console.Write("Data = "); for (int j = 0; j < LINSlaveDataBuffer[i].DataLen; j++) { Console.Write("0x{0:X2} ", LINSlaveDataBuffer[i].Data[j]); } Console.WriteLine(""); } } //延时 System.Threading.Thread.Sleep(10); } #endif }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 IICIndex = 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 USB2IIC.IIC_CONFIG IICConfig = new USB2IIC.IIC_CONFIG(); IICConfig.AddrBits = 7; //7bit地址模式 IICConfig.ClockSpeedHz = 400000; //时钟频率400KHz IICConfig.EnablePu = 1; #if SLAVE_WRITE_TEST || SLAVE_READ_TEST IICConfig.Master = 0; //从机模式 IICConfig.OwnAddr = 0x71; //从机地址 #else IICConfig.Master = 1; //主机模式 #endif ret = USB2IIC.IIC_Init(DevHandle, IICIndex, ref IICConfig); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Config IIC failed!"); return; } else { Console.WriteLine("Config IIC IIC_SUCCESS!"); } #if AUTO_GET_SLAVE_ADDR //获取总线上能应答的从机地址 Int16[] SlaveAddrs = new Int16[128]; //地址存储缓冲区 int SlaveAddrNum = 0; //返回应答的地址个数 SlaveAddrNum = USB2IIC.IIC_GetSlaveAddr(DevHandle, IICIndex, SlaveAddrs); if (SlaveAddrNum >= 0) { Console.WriteLine("Get {0} slave address!", SlaveAddrNum); for (int i = 0; i < SlaveAddrNum; i++) { Console.Write(SlaveAddrs[i].ToString("X2") + " "); } Console.WriteLine(""); } else { Console.WriteLine("Get slave address error!"); return; } #endif #if SLAVE_WRITE_TEST //IIC 从机模式写数据 Byte[] SlaveWriteBuffer = new Byte[256]; for (int i = 0; i < SlaveWriteBuffer.Length; i++) { SlaveWriteBuffer[i] = (Byte)i; } for (int i = 0; i < (SlaveWriteBuffer.Length / 8); i++) { ret = USB2IIC.IIC_SlaveWriteBytes(DevHandle, IICIndex, SlaveWriteBuffer, 8, 10000); if (ret < 0) { Console.WriteLine("Slave write data error!"); break; } else if (ret == 0) { Console.WriteLine("Slave write data IIC_SUCCESS!"); } else if (ret > 0) { Console.WriteLine("Slave write data IIC_SUCCESS! have {0} byte data remain", ret); } } Console.WriteLine("Slave write end!"); return; #endif #if SLAVE_READ_TEST //IIC 从机模式读数据 Byte[] SlaveReadBuffer = new Byte[256]; for (int i = 0; i < 16; i++) { ret = USB2IIC.IIC_SlaveReadBytes(DevHandle, IICIndex, SlaveReadBuffer, 10000); if (ret < 0) { Console.WriteLine("Slave read data error!"); break; } else if (ret == 0) { Console.WriteLine("Slave read data IIC_SUCCESS! but no data"); } else if (ret > 0) { Console.WriteLine("Slave read data IIC_SUCCESS! have {0} byte data have read", ret); for (i = 0; i < ret; i++) { Console.WriteLine(SlaveReadBuffer[i].ToString("X2")); } Console.WriteLine(""); } } Console.WriteLine("Slave read end!\n"); return; #endif //IIC 写数据 Byte[] WriteBuffer = new Byte[8]; for (int i = 0; i < WriteBuffer.Length; i++) { WriteBuffer[i] = (Byte)i; } ret = USB2IIC.IIC_WriteBytes(DevHandle, IICIndex, 0x50, WriteBuffer, WriteBuffer.Length, 1000); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Write IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } else { Console.WriteLine("Write IIC Success!"); } //延时 System.Threading.Thread.Sleep(10); //IIC 读数据 Byte[] ReadBuffer = new Byte[8]; ret = USB2IIC.IIC_ReadBytes(DevHandle, IICIndex, 0x50, ReadBuffer, ReadBuffer.Length, 1000); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Read IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } else { Console.WriteLine("Read IIC Success!"); Console.WriteLine("Read Data:"); for (int i = 0; i < ReadBuffer.Length; i++) { Console.Write(ReadBuffer[i].ToString("X2") + " "); } Console.WriteLine(""); } //延时 System.Threading.Thread.Sleep(10); //IIC 写读数据。也就是先发送数据,然后再次产生START信号,再读数据 //注意:对于每个I2C设备,必须按照设备要求正确读写,否则可能会导致设备工作不正常,从而导致无法读写数据 WriteBuffer[0] = 0x08; ret = USB2IIC.IIC_WriteReadBytes(DevHandle, IICIndex, 0x50, WriteBuffer, 1, ReadBuffer, ReadBuffer.Length, 1000); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("WriteRead IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } else { Console.WriteLine("WriteRead IIC Success!"); Console.WriteLine("Read Data:"); for (int i = 0; i < ReadBuffer.Length; i++) { Console.Write(ReadBuffer[i].ToString("X2") + " "); } Console.WriteLine(""); } //关闭设备 usb_device.USB_CloseDevice(DevHandle); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 ADS1256Index = USB2SPI.SPI1_CS0; UInt32 ResetPinMask, DrdyPinMask; bool state; Int32 DevNum, ret; Byte[] WriteBuffer = new Byte[64]; Byte[] ReadBuffer = new Byte[20480]; //扫描查找设备 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); Console.WriteLine(" Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8")); } //配置RESET引脚和DRADY引脚 if (ADS1256Index == 0) { ResetPinMask = 1 << 9; DrdyPinMask = 1 << 8; } else { ResetPinMask = 1 << 1; DrdyPinMask = 1 << 0; } ret = USB2GPIO.GPIO_SetOutput(DevHandle, ResetPinMask, 1); if (ret != USB2GPIO.GPIO_SUCCESS) { Console.WriteLine("Initialize gpio error!"); return; } //配置SPI总线相关参数 SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_HDX; SPIConfig.ClockSpeedHz = 1562500; SPIConfig.CPHA = 1; SPIConfig.CPOL = 0; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_MASTER; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, ADS1256Index, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize device error!"); return; } //RESET引脚输出低脉冲复位ADS1256 USB2GPIO.GPIO_Write(DevHandle, ResetPinMask, 0); //输出低电平 System.Threading.Thread.Sleep(10); USB2GPIO.GPIO_Write(DevHandle, ResetPinMask, ResetPinMask); //输出高电平 //准备配置寄存器数据 WriteBuffer[0] = 0x50; //ADS1256_CMD_WREG WriteBuffer[1] = 0x04; WriteBuffer[2] = 0x04; WriteBuffer[3] = 0x08; //配置AIN0为单端模式 WriteBuffer[4] = 0; //PGA WriteBuffer[5] = 0xA1; //1000SPS WriteBuffer[6] = 0xFF; ret = USB2SPI.SPI_WriteBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 7, (Int32)DrdyPinMask, 0x00, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write Data Error!"); return; } //准备读配置寄存器数据,验证配置数据写入是否成功 WriteBuffer[0] = 0x10;//ADS1256_CMD_RREG; WriteBuffer[1] = 4; ret = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 2, ReadBuffer, 5, 10, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write&Read Data Error!"); return; } //判断写入配置寄存器的值和读出寄存器的值是否一样 if ((WriteBuffer[3] != ReadBuffer[1]) || (WriteBuffer[4] != ReadBuffer[2]) || (WriteBuffer[5] != ReadBuffer[3])) { Console.WriteLine("Config ADS1256 Error!"); return; } //发送连续采集数据命令,该命令只适合连续采集一个通道的情况 WriteBuffer[0] = 0xFC; //ADS1256_CMD_SYNC WriteBuffer[1] = 0x00; //ADS1256_CMD_WAKEUP WriteBuffer[2] = 0x03; //ADS1256_CMD_RDATAC ret = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 3, ReadBuffer, 3, 10, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write&Read Data Error!"); return; } Console.WriteLine("Continuous Get ADC Data!"); //检测DRADY引脚下降沿之后读回数据 int ReadDataNum = 10; ret = USB2SPI.SPI_BlockReadBytesOfEvent(DevHandle, ADS1256Index, ReadBuffer, 3, ReadDataNum, (Int32)DrdyPinMask, 0x10, 100); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event BlockRead Data Error!"); return; } //将读回的数据转换为实际的电压值 for (int i = 0; i < ReadDataNum; i++) { int ADCDataTemp = (ReadBuffer[i * 3 + 0] << 16) | (ReadBuffer[i * 3 + 1] << 8) | ReadBuffer[i * 3 + 2]; if ((ADCDataTemp & 0x800000) != 0x00) { ADCDataTemp = (Int32)(0xFF000000 | ((UInt32)ADCDataTemp)); } double Volutage = (ADCDataTemp * 0.59604644775390625) / 1000000; Console.WriteLine("ADC[{0}] = {1} V", i, Volutage); } //发送停止连续采样数据命令 WriteBuffer[0] = 0x0F;//ADS1256_CMD_SDATAC ret = USB2SPI.SPI_WriteBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 1, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write Data Error!"); return; } Console.WriteLine("Circulate Get ADC Data!"); //循环采集每个通道数据 ReadDataNum = 3; byte Channel = 0; WriteBuffer[0] = 0x50 | 1;//ADS1256_CMD_WREG WriteBuffer[1] = 0x00; WriteBuffer[2] = (byte)((Channel << 4) | 0x08); WriteBuffer[3] = 0xFC; //ADS1256_CMD_SYNC WriteBuffer[4] = 0x00; //ADS1256_CMD_WAKEUP WriteBuffer[5] = 0x01; //ADS1256_CMD_RDATA for (int i = 0; i < ReadDataNum; i++) { for (Channel = 0; Channel < 8; Channel++) { WriteBuffer[2] = (byte)((Channel << 4) | 0x08); ret = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 6, ReadBuffer, 3, 10, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write&Read Data Error!"); return; } else { //丢弃第一次的数据 if ((i == 0) && (Channel == 0)) { continue; } int ADCDataTemp = (ReadBuffer[0] << 16) | (ReadBuffer[1] << 8) | ReadBuffer[2]; if ((ADCDataTemp & 0x800000) != 0x00) { ADCDataTemp = (Int32)(0xFF000000 | ((UInt32)ADCDataTemp)); } double Volutage = (ADCDataTemp * 0.59604644775390625) / 1000000; Console.WriteLine("ADC_CH[{0}][{1}] = {2} V", i, Channel == 0 ? 7 : Channel - 1, Volutage); } } } }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 IICIndex = 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 USB2IIC.IIC_CONFIG IICConfig = new USB2IIC.IIC_CONFIG(); IICConfig.AddrBits = 7; //7bit地址模式 IICConfig.ClockSpeedHz = 400000; //时钟频率400KHz IICConfig.Master = 1; //主机模式 ret = USB2IIC.IIC_Init(DevHandle, IICIndex, ref IICConfig); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Config IIC failed!"); return; } else { Console.WriteLine("Config IIC IIC_SUCCESS!"); } #if AUTO_GET_SLAVE_ADDR //获取总线上能应答的从机地址 Int16[] SlaveAddrs = new Int16[128]; //地址存储缓冲区 int SlaveAddrNum = 0; //返回应答的地址个数 SlaveAddrNum = USB2IIC.IIC_GetSlaveAddr(DevHandle, IICIndex, SlaveAddrs); if (SlaveAddrNum >= 0) { Console.WriteLine("Get {0} slave address!", SlaveAddrNum); for (int i = 0; i < SlaveAddrNum; i++) { Console.Write(SlaveAddrs[i].ToString("X2") + " "); } Console.WriteLine(""); } else { Console.WriteLine("Get slave address error!"); return; } #endif //向芯片写数据 Int16 SlaveAddr = 0x50; UInt16 PageSize = 8; UInt16 PageNum = 32; UInt16 PageAddr = 0; Byte[] WriteDataBuffer = new Byte[1 + PageSize];//第1字节存储写数据起始地址,每页只有8字节,所以每次只能写8字节数据 for (int i = 0; i < PageNum; i++) { //第1字节为子地址 WriteDataBuffer[0] = (Byte)(PageAddr & 0xFF); //填充要写的数据 for (int j = 0; j < PageSize; j++) { WriteDataBuffer[1 + j] = (Byte)j; } //调用写数据函数 ret = USB2IIC.IIC_WriteBytes(DevHandle, IICIndex, SlaveAddr, WriteDataBuffer, WriteDataBuffer.Length, 1000); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Write IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } PageAddr += PageSize; //延时10ms,否则可能会导致写数据失败 System.Threading.Thread.Sleep(10); } Console.WriteLine("Write IIC success!"); //从芯片读数据 Byte[] ReadDataBuffer = new Byte[PageNum * PageSize]; PageAddr = 0x00;//读数据起始地址 WriteDataBuffer[0] = (Byte)(PageAddr & 0xFF); //调用写读数据函数 ret = USB2IIC.IIC_WriteReadBytes(DevHandle, IICIndex, SlaveAddr, WriteDataBuffer, 1, ReadDataBuffer, ReadDataBuffer.Length, 1000); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("WriteRead IIC failed!"); Console.WriteLine("Error Code:{0}", ret); return; } else { Console.WriteLine("WriteRead IIC Success!"); Console.WriteLine("Read Data:"); for (int i = 0; i < ReadDataBuffer.Length; i++) { Console.Write(ReadDataBuffer[i].ToString("X2") + " "); } Console.WriteLine(""); } Console.WriteLine("WriteRead IIC success!"); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 SPIIndex = 0; bool state; Int32 DevNum, ret; Byte[] WriteBuffer = new Byte[64]; Byte[] ReadBuffer = new Byte[20480]; //扫描查找设备 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); } //配置SPI总线相关参数 Console.Write("Please input SPI channel(0 or 1):"); SPIIndex = Convert.ToInt32(Console.ReadLine()); Console.Write("Please input CPHA(0 or 1):"); SPIConfig.CPHA = Convert.ToByte(Console.ReadLine()); Console.Write("Please input CPOL(0 or 1):"); SPIConfig.CPOL = Convert.ToByte(Console.ReadLine()); Console.WriteLine("SPIConfig.CPHA = " + SPIConfig.CPHA.ToString()); Console.WriteLine("SPIConfig.CPOL = " + SPIConfig.CPOL.ToString()); //配置SPI总线相关参数(配置为从机模式) SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_FDX; SPIConfig.ClockSpeedHz = 50000000; //SPIConfig.CPHA = 0; //SPIConfig.CPOL = 1; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_SLAVE; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, SPIIndex, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize device error!"); return; } /* * for (int i = 0; i < WriteBuffer.Length; i++) * { * WriteBuffer[i] = (Byte)i; * } * while(true){ * ret = USB2SPI.SPI_SlaveWriteBytes(DevHandle, SPIIndex, WriteBuffer, 16, 5000); * if (ret != USB2SPI.SPI_SUCCESS) * { * Console.WriteLine("SPI slave write data error!"); * return; * } * Console.ReadLine(); * } */ Console.WriteLine("Press any key to exit the data reception!"); GC.KeepAlive(spi_get_data_callback); USB2SPI.SPI_SlaveContinueRead(DevHandle, SPIIndex, spi_get_data_callback); Console.ReadLine(); USB2SPI.SPI_SlaveContinueReadStop(DevHandle, SPIIndex); Console.WriteLine("Test SPI_SUCCESS!"); return; }
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 = 0; //扫描查找设备 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); } //初始化配置LIN #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 0, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 0, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 1, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 1, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST //启动多线程写数据 Thread LIN1SendMsgThread = new Thread(() => LIN_SendMsgThread(DevHandle, 0)); Thread LIN2SendMsgThread = new Thread(() => LIN_SendMsgThread(DevHandle, 1)); LIN_SendMsgFlag[0] = true; LIN_SendMsgFlag[1] = true; LIN1SendMsgThread.Start(); LIN2SendMsgThread.Start(); //按下回车后结束发送接收线程 Console.ReadLine(); LIN_SendMsgFlag[0] = false; LIN_SendMsgFlag[1] = false; LIN1SendMsgThread.Join(); LIN2SendMsgThread.Join(); #else /************************************从机发送数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; //设置ID为LIN_EX_MSG_TYPE_SW模式,从机接收到主机发送的帧头后就会返回预先定义好的数据 USB2LIN_EX.LIN_EX_MSG[] LINSlaveMsg = new USB2LIN_EX.LIN_EX_MSG[3]; //配置第一帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x34; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第二帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x35; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第三帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x36; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要配置更多帧,请按照前面的方式继续添加********************/ //默认所有ID都是从机接收数据模式,该模式下可以用于数据的监听 //调用该函数后,只会修改传入对应ID的模式,其他的不会被改变 ret = USB2LIN_EX.LIN_EX_SlaveSetIDMode(DevHandle, LINIndex, LINSlaveMsg, MsgIndex); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN ID Mode failed!"); return; } else { Console.WriteLine("Config LIN ID Mode Success!"); } //循环获取接收到的数据,该操作可以用作LIN总线数据监控 Console.WriteLine("Start Get LIN Data..."); int RunTimeMs = 10000;//只运行10秒钟,10秒钟后结束数据监听,可自行修改 while (RunTimeMs > 0) { USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1024];//缓冲区尽量大一点,防止益处 //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINMsg.Length); ret = USB2LIN_EX.LIN_EX_SlaveGetData(DevHandle, LINIndex, pt); for (int i = 0; i < ret; i++) { LINMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.WriteLine("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINMsg[i].MsgType], LINMsg[i].Sync, LINMsg[i].PID); for (int j = 0; j < LINMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}]:{3}]:{4}].{5}]]\n", CKTypeStr[LINMsg[i].CheckType], LINMsg[i].Check, (LINMsg[i].Timestamp / 36000000) % 60, (LINMsg[i].Timestamp / 600000) % 60, (LINMsg[i].Timestamp / 10000) % 60, (LINMsg[i].Timestamp / 10) % 1000); } //释放内存 Marshal.FreeHGlobal(pt); System.Threading.Thread.Sleep(10); RunTimeMs -= 10; } #endif Console.WriteLine("Close Device!"); //关闭设备 usb_device.USB_CloseDevice(DevHandle); return; }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 IICIndex = 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); Console.WriteLine(" Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8")); } //初始化配置I2C USB2IIC.IIC_CONFIG IICConfig = new USB2IIC.IIC_CONFIG(); IICConfig.AddrBits = 7; //7bit地址模式 IICConfig.ClockSpeedHz = 400000; //时钟频率400KHz IICConfig.Master = 0; //从机模式 IICConfig.OwnAddr = 0x71; //从机地址 ret = USB2IIC.IIC_Init(DevHandle, IICIndex, ref IICConfig); if (ret != USB2IIC.IIC_SUCCESS) { Console.WriteLine("Config IIC failed!"); return; } else { Console.WriteLine("Config IIC IIC_SUCCESS!"); } while (true) { //IIC 从机模式读数据 Byte[] SlaveReadBuffer = new Byte[20480]; ret = USB2IIC.IIC_SlaveReadBytes(DevHandle, IICIndex, SlaveReadBuffer, 1000);//超时时间设置为1000ms,超时之后,不管是否读到主机发送过来的数据,函数都返回 if (ret < 0) { Console.WriteLine("Slave read data error!"); break; } else if (ret > 0) { Console.WriteLine("Slave read data IIC_SUCCESS! have {0} byte data have read", ret); for (int i = 0; i < ret; i++) { Console.Write(SlaveReadBuffer[i].ToString("X2") + " "); } Console.WriteLine(""); //读到数据之后,将读到的数据发送到适配器内部数据缓冲区,等待主机来读取 //IIC 从机模式写数据 ret = USB2IIC.IIC_SlaveWriteBytes(DevHandle, IICIndex, SlaveReadBuffer, ret, 3000);//超时时间设置为1000ms,超时之后,不管主机是否读取数据,函数都返回 if (ret < 0) { Console.WriteLine("Slave write data error!"); break; } else if (ret == 0) { Console.WriteLine("Slave write data IIC_SUCCESS!"); } else if (ret > 0) { Console.WriteLine("Slave write data IIC_SUCCESS! have {0} byte data remain", ret); } } } }
/// <summary> /// 初始化配置USB2XXX CAN适配器 /// </summary> /// <returns></returns> private bool configDevice() { int ret, DevNum; bool state; DeviceIndex = this.comboBoxDeviceIndex.SelectedIndex; CANIndex = this.comboBoxCANIndex.SelectedIndex; //扫描查找设备 DevNum = usb_device.USB_ScanDevice(DevHandles); if (DevNum <= 0) { MessageBox.Show(this, "无设备连接!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } //打开设备 state = usb_device.USB_OpenDevice(DevHandles[DeviceIndex]); if (!state) { MessageBox.Show(this, "打开设备失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } //获取设备类型 usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); StringBuilder FuncStr = new StringBuilder(256); state = usb_device.DEV_GetDeviceInfo(DevHandles[DeviceIndex], ref DevInfo, FuncStr); if (!state) { MessageBox.Show(this, "获取设备信息失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } DevType = ((UInt64)DevInfo.SerialNumber[0] << 32) | (DevInfo.SerialNumber[1]); USB2CAN.CBL_CMD_LIST CMD_List = new USB2CAN.CBL_CMD_LIST(); String[] cmdStr = { "Erase", "WriteInfo", "Write", "Check", "SetBaudRate", "Excute", "CmdSuccess", "CmdFaild" }; byte[] cmdData = new byte[cmdStr.Length]; for (int i = 0; i < this.listViewCmdList.Items.Count; i++) { ListViewItem item = listViewCmdList.Items[i]; if (item.SubItems[0].Text == cmdStr[i]) { cmdData[i] = byte.Parse(item.SubItems[1].Text); } } CMD_List.Erase = cmdData[0]; CMD_List.WriteInfo = cmdData[1]; CMD_List.Write = cmdData[2]; CMD_List.Check = cmdData[3]; CMD_List.SetBaudRate = cmdData[4]; CMD_List.Excute = cmdData[5]; CMD_List.CmdSuccess = cmdData[6]; CMD_List.CmdFaild = cmdData[7]; USB2CAN.CAN_INIT_CONFIG CAN_InitConfig = new USB2CAN.CAN_INIT_CONFIG(); int BaudRate = int.Parse(this.comboBoxBaudRate.Text.Substring(0, this.comboBoxBaudRate.Text.Length - 4)); if (DevType == DevTypeUSB2CANB) { CAN_InitConfig.CAN_BRP = (UInt32)CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].PreScale; CAN_InitConfig.CAN_SJW = CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].SJW; CAN_InitConfig.CAN_BS1 = CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].BS1; CAN_InitConfig.CAN_BS2 = CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].BS2; } else { CAN_InitConfig.CAN_BRP = (UInt32)CANBaudRateTab[GetBaudRateIndex(BaudRate)].PreScale; CAN_InitConfig.CAN_SJW = CANBaudRateTab[GetBaudRateIndex(BaudRate)].SJW; CAN_InitConfig.CAN_BS1 = CANBaudRateTab[GetBaudRateIndex(BaudRate)].BS1; CAN_InitConfig.CAN_BS2 = CANBaudRateTab[GetBaudRateIndex(BaudRate)].BS2; } ret = USB2CAN.CAN_BL_Init(DevHandles[DeviceIndex], CANIndex, ref CAN_InitConfig, ref CMD_List); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "初始化设备失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } return(true); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 SPIIndex = 0; bool state; Int32 DevNum, ret; Byte[] WriteBuffer = new Byte[64]; Byte[] ReadBuffer = new Byte[20480]; //扫描查找设备 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); Console.WriteLine(" Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8")); } //配置SPI总线相关参数 SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_HDX; SPIConfig.ClockSpeedHz = 50000000; SPIConfig.CPHA = 0; SPIConfig.CPOL = 0; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_MASTER; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, SPIIndex, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize device error!"); return; } //SPI发送数据 for (int i = 0; i < WriteBuffer.Length; i++) { WriteBuffer[i] = (Byte)i; } ret = USB2SPI.SPI_WriteBytes(DevHandle, SPIIndex, WriteBuffer, WriteBuffer.Length); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI write data error!"); return; } //SPI异步发送数据,调用该函数后会立即返回,但是SPI数据不一定发送完毕,但是在下一次发送数据之前会保证数据发送完毕 for (int i = 0; i < 64; i++) { ret = USB2SPI.SPI_WriteBytesAsync(DevHandle, SPIIndex, WriteBuffer, WriteBuffer.Length); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI async write data error!"); return; } } //SPI接收数据 ret = USB2SPI.SPI_ReadBytes(DevHandle, SPIIndex, ReadBuffer, 32); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI read data error!"); return; } else { Console.WriteLine("Read Data:"); for (int i = 0; i < 32; i++) { Console.Write(ReadBuffer[i].ToString("X2") + " "); if (((i + 1) % 16) == 0) { Console.WriteLine(""); } } Console.WriteLine(""); } //SPI先发送数据,再接收数据,整个过程片选信号一直有效 int IntervalTime = 10;//发送和接收数据之间的时间间隔,单位为us ret = USB2SPI.SPI_WriteReadBytes(DevHandle, SPIIndex, WriteBuffer, WriteBuffer.Length, ReadBuffer, 32, IntervalTime); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI write read data error!"); return; } else { Console.WriteLine("Read Data:"); for (int i = 0; i < 32; i++) { Console.Write(ReadBuffer[i].ToString("X2") + " "); if (((i + 1) % 16) == 0) { Console.WriteLine(""); } } Console.WriteLine(""); } Console.WriteLine("Test SPI_SUCCESS!"); return; }