/// <summary> /// 初始化某一路CAN通道 /// </summary> private bool InitDevice() { IntPtr ptr = GetIProperty(device_handle); if (0 == (int)ptr) { return(false);//设置指定路径属性失败 } IProperty _property = (IProperty)Marshal.PtrToStructure((IntPtr)((UInt32)ptr), typeof(IProperty)); if (_property.SetValue(zlgInfo.DevChannel.ToString() + "/canfd_standard", zlgInfo.CANFD.ToString()) != 1) { return(false);//设置CANFD标准失败 } ZCAN_CHANNEL_INIT_CONFIG config = new ZCAN_CHANNEL_INIT_CONFIG(); config.canfd.mode = zlgInfo.Mode; config.can_type = (uint)ZLGType.VCI_USBCANFD_100U; uint pData = 0x00018B2E; SetArbitrationBaudRate(zlgInfo.ArbitrationBaudrate, ref pData); config.canfd.abit_timing = pData; uint _pData = 0x00010207; SetDataBaudRate(zlgInfo.DataBaudRate, ref _pData); config.canfd.dbit_timing = _pData; IntPtr pConfig = Marshal.AllocHGlobal(Marshal.SizeOf(config)); Marshal.StructureToPtr(config, pConfig, true); //int size = sizeof(ZCAN_CHANNEL_INIT_CONFIG); //IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(size); //System.Runtime.InteropServices.Marshal.StructureToPtr(config_, ptr, true); channel_handle = ZCAN_InitCAN(device_handle, (uint)zlgInfo.DevChannel, pConfig); Marshal.FreeHGlobal(pConfig); //Marshal.FreeHGlobal(ptr); if (0 == (int)channel_handle) { string strErr = "设备类型号:" + ZLGInfo.DevType.ToString() + " 设备索引号: " + zlgInfo.DevIndex.ToString() + " CAN通道号: " + zlgInfo.DevChannel.ToString(); return(false);//初始化CAN失败 } if (zlgInfo.TerminaiResistanceEnabled == 1) { if (_property.SetValue(zlgInfo.DevChannel.ToString() + "initenal_resistance", zlgInfo.TerminaiResistanceEnabled.ToString()) != 1) { return(false);//设置使能终端电阻失败 } } return(true); }
private void button_init_Click(object sender, EventArgs e) { if (!m_bOpen) { MessageBox.Show("设备还没打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } uint type = kDeviceType[comboBox_device.SelectedIndex].device_type; bool netDevice = type == Define.ZCAN_CANETTCP || type == Define.ZCAN_CANETUDP; bool pcieCanfd = type == Define.ZCAN_PCIECANFD_100U || type == Define.ZCAN_PCIECANFD_200U || type == Define.ZCAN_PCIECANFD_400U; bool usbCanfd = type == Define.ZCAN_USBCANFD_100U || type == Define.ZCAN_USBCANFD_200U || type == Define.ZCAN_USBCANFD_MINI; bool canfdDevice = usbCanfd || pcieCanfd; if (!m_bCloud) { IntPtr ptr = Method.GetIProperty(device_handle_); if (NULL == (int)ptr) { MessageBox.Show("设置指定路径属性失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } property_ = (IProperty)Marshal.PtrToStructure((IntPtr)((UInt32)ptr), typeof(IProperty)); if (netDevice) { bool tcpDevice = type == Define.ZCAN_CANETTCP; bool server = tcpDevice && comboBox_netmode.SelectedIndex == 0; if (tcpDevice) { setNetMode(); if (server) { setLocalPort(); } else { setRemoteAddr(); setRemotePort(); } } else { setLocalPort(); setRemoteAddr(); setRemotePort(); } } else { if (usbCanfd) { if (!setCANFDStandard(comboBox_standard.SelectedIndex)) //设置CANFD标准 { MessageBox.Show("设置CANFD标准失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } if (checkBox_ABIT.Checked)//设置波特率 { if (!setCustomBaudrate()) { MessageBox.Show("设置自定义波特率失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } else { if (!canfdDevice && !setBaudrate(kBaudrate[comboBox_baud.SelectedIndex])) { MessageBox.Show("设置波特率失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } } } ZCAN_CHANNEL_INIT_CONFIG config_ = new ZCAN_CHANNEL_INIT_CONFIG(); if (!m_bCloud && !netDevice) { config_.canfd.mode = (byte)comboBox_mode.SelectedIndex; if (canfdDevice) { config_.can_type = Define.TYPE_CANFD; config_.canfd.abit_timing = kAbitTiming[comboBox_ABIT.SelectedIndex]; config_.canfd.dbit_timing = kDbitTiming[comboBox_ABIT2.SelectedIndex]; } else { config_.can_type = Define.TYPE_CAN; config_.can.timing0 = kTiming0[comboBox_baud.SelectedIndex]; config_.can.timing1 = kTiming1[comboBox_baud.SelectedIndex]; config_.can.filter = 0; config_.can.acc_code = 0; config_.can.acc_mask = 0xFFFFFFFF; } } IntPtr pConfig = Marshal.AllocHGlobal(Marshal.SizeOf(config_)); Marshal.StructureToPtr(config_, pConfig, true); //int size = sizeof(ZCAN_CHANNEL_INIT_CONFIG); //IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(size); //System.Runtime.InteropServices.Marshal.StructureToPtr(config_, ptr, true); channel_handle_ = Method.ZCAN_InitCAN(device_handle_, (uint)channel_index_, pConfig); Marshal.FreeHGlobal(pConfig); //Marshal.FreeHGlobal(ptr); if (NULL == (int)channel_handle_) { MessageBox.Show("初始化CAN失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (!m_bCloud && !netDevice) { if (usbCanfd && checkBox_resistance.Checked) { if (!setResistanceEnable()) { MessageBox.Show("使能终端电阻失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } //if (!canfdDevice && !setFilterCode()) //{ // MessageBox.Show("设置滤波失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); // return; //} if (canfdDevice && !setFilter()) { MessageBox.Show("设置滤波失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } button_init.Enabled = false; }