/// <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(null); if (DevNum <= 0) { MessageBox.Show(this, "无设备连接!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } //打开设备 state = usb_device.USB_OpenDevice(DeviceIndex); if (!state) { MessageBox.Show(this, "打开设备失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } 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)); 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(DeviceIndex, CANIndex, ref CAN_InitConfig, ref CMD_List); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "初始化设备失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } return(true); }
/// <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", "Execute", "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.Execute = 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); }