/// <summary> /// CHECK AVAILABLE DEVICES /// </summary> private void CheckDevice() { uint isChannelValid; UInt32 iDeviceID = 0; dev.Clear(); try { foreach (var Device in m_NonPnPHandles) { stsResult = PCANBasic.GetValue(Device, TPCANParameter.PCAN_CHANNEL_CONDITION, out isChannelValid, sizeof(uint)); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { if (isChannelValid == PCANBasic.PCAN_CHANNEL_AVAILABLE) { stsResult = PCANBasic.GetValue(Device, TPCANParameter.PCAN_DEVICE_ID, out iDeviceID, sizeof(UInt32)); switch (Device) { case 0x41: case 0x42: case 0x43: cbDevice.Items.Add($"PCAN PCI({iDeviceID}h)"); dev.Add(Device, $"PCAN PCI({iDeviceID}h)"); break; case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: cbDevice.Items.Add($"PCAN USB({iDeviceID}h)"); dev.Add(Device, $"PCAN USB({iDeviceID}h)"); break; case 0x801: case 0x802: case 0x803: case 0x804: case 0x805: cbDevice.Items.Add($"PCAN LAN({iDeviceID}h)"); dev.Add(Device, $"PCAN LAN({iDeviceID}h)"); break; } } } } } catch (DllNotFoundException) { tbAlarmLog.Text += "PCANBasic.dll file Not Found."; } }
public static uint detectCONNECT() { TPCANStatus stsResult; uint iChannelsCount = 0; stsResult = PCANBasic.GetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_ATTACHED_CHANNELS_COUNT, out iChannelsCount, sizeof(uint)); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { return(iChannelsCount); } else { return(0xff);//error } }
/// <summary> /// Gets the current status of the PCAN-Basic message filter /// </summary> /// <param name="status">Buffer to retrieve the filter status</param> /// <returns>If calling the function was successfull or not</returns> private bool GetFilterStatus(out uint status) { TPCANStatus stsResult; // Tries to get the sttaus of the filter for the current connected hardware // stsResult = PCANBasic.GetValue(m_PcanHandle, TPCANParameter.PCAN_MESSAGE_FILTER, out status, sizeof(UInt32)); // If it fails, a error message is shown // if (stsResult != TPCANStatus.PCAN_ERROR_OK) { MessageBox.Show(GetFormatedError(stsResult)); return(false); } return(true); }
/// <summary> /// Inits the can bus channel. /// </summary> private void initCanBusChannel() { UInt32 iBuffer; TPCANStatus stsResult; // Clears the Channel combioBox and fill it again with // the PCAN-Basic handles for no-Plug&Play hardware and // the detected Plug&Play hardware try { for (int i = 0; i < m_HandlesArray.Length; i++) { // Checks for a Plug&Play Handle and, according with the return value, includes it // into the list of available hardware channels. stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_CHANNEL_CONDITION, out iBuffer, sizeof(UInt32)); if ((stsResult == TPCANStatus.PCAN_ERROR_OK) && (iBuffer == PCANBasic.PCAN_CHANNEL_AVAILABLE)) { this._canChannel = m_HandlesArray[i]; break; } } } catch (DllNotFoundException) { //MessageBox.Show("Unable to find the library: PCANBasic.dll !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } catch (Exception ee) { Console.WriteLine(ee.Message); } if (this._canChannel > 0) { // Connects a selected PCAN-Basic channel stsResult = PCANBasic.Initialize(this._canChannel, m_Baudrate, TPCANType.PCAN_TYPE_ISA, 0, 0); //no use this line parameters if (stsResult != TPCANStatus.PCAN_ERROR_OK) { isConnectToCanbus = false; } else { isConnectToCanbus = true; } } }
public uint Check_opendevice_ifFD()//检测打开当前的设备是否支持FD { TPCANStatus stsResult; uint iChannelsCount = 0; stsResult = PCANBasic.GetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_ATTACHED_CHANNELS_COUNT, out iChannelsCount, sizeof(uint)); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { TPCANChannelInformation[] info = new TPCANChannelInformation[iChannelsCount]; stsResult = PCANBasic.GetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_ATTACHED_CHANNELS, info); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { //Form_BasicFunction.form_basic.checkEnableCANFD.Checked = false; //Form_BasicFunction.form_basic.EnableCANFD_BRS.Enabled = false; //Form_BasicFunction.form_basic.EnableCANFD_BRS.Checked = false; if (PCAN_COMMON_VAL.can_channel_buf == 0x0051)//channel = 0 即通道1 { if (info[0].device_name == "PCAN-USB FD") { checkCANFDMode.Enabled = true; } else { checkCANFDMode.Enabled = false; } } else if (PCAN_COMMON_VAL.can_channel_buf == 0x0052)//channel = 1 即通道2 { if (info[1].device_name == "PCAN-USB FD") { checkCANFDMode.Enabled = true; } else { checkCANFDMode.Enabled = false; } } } return(iChannelsCount); } else { return(0xff);//error } }
/// <summary>Enmerate all ports, and their USB Device Number</summary> public static List <Ports> All() { var all = new List <Ports>(); uint fff = 0; TPCANStatus status; foreach (Ports d in Enum.GetValues(typeof(Ports))) { PCANBasic.Initialize((byte)d, TPCANBaudrate.PCAN_BAUD_1M); status = PCANBasic.GetValue((byte)d, TPCANParameter.PCAN_DEVICE_NUMBER, out fff, 1); if (status != TPCANStatus.PCAN_ERROR_INITIALIZE) { all.Add(d); } PCANBasic.Uninitialize((byte)d); } return(all); }
private void btnHwRefresh_Click(object sender, EventArgs e) { UInt32 iBuffer; TPCANStatus stsResult; bool isFD; // Clears the Channel combioBox and fill it again with // the PCAN-Basic handles for no-Plug&Play hardware and // the detected Plug&Play hardware // cbChannel.Items.Clear(); try { for (int i = 0; i < m_HandlesArray.Length; i++) { // Includes all no-Plug&Play Handles if (m_HandlesArray[i] <= PCANBasic.PCAN_DNGBUS1) { cbChannel.Items.Add(FormatChannelName(m_HandlesArray[i])); } else { // Checks for a Plug&Play Handle and, according with the return value, includes it // into the list of available hardware channels. // stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_CHANNEL_CONDITION, out iBuffer, sizeof(UInt32)); if ((stsResult == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.PCAN_CHANNEL_AVAILABLE) == PCANBasic.PCAN_CHANNEL_AVAILABLE)) { stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_CHANNEL_FEATURES, out iBuffer, sizeof(UInt32)); isFD = (stsResult == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.FEATURE_FD_CAPABLE) == PCANBasic.FEATURE_FD_CAPABLE); cbChannel.Items.Add(FormatChannelName(m_HandlesArray[i], isFD)); } } } cbChannel.SelectedIndex = cbChannel.Items.Count - 1; btnConnect.Enabled = cbChannel.Items.Count > 0; } catch (DllNotFoundException) { MessageBox.Show("Unable to find the library: PCANBasic.dll !", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } }
private void UpdatePorts() { if (_commThread.ThreadRunning()) { return; } // ReSharper disable once ConstantNullCoalescingCondition string[] ports = SerialPort.GetPortNames() ?? new string[0]; if (_lastPortCount == ports.Length) { return; } listPorts.BeginUpdate(); listPorts.Items.Clear(); int index = listPorts.Items.Add("ENET"); Regex rx = new Regex("(COM[0-9]+).*"); foreach (string port in ports) { string portFixed = rx.Replace(port, "$1"); index = listPorts.Items.Add(portFixed); } try { TPCANStatus stsResult = PCANBasic.GetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_CHANNEL_CONDITION, out UInt32 _, sizeof(UInt32)); //if ((stsResult == TPCANStatus.PCAN_ERROR_OK) && (iBuffer == PCANBasic.PCAN_CHANNEL_AVAILABLE)) if (stsResult == TPCANStatus.PCAN_ERROR_OK) { index = listPorts.Items.Add("CAN"); } } catch (Exception) { // ignored } listPorts.SelectedIndex = index; listPorts.EndUpdate(); buttonConnect.Enabled = listPorts.SelectedIndex >= 0; _lastPortCount = ports.Length; }
/// <summary> /// Gets the current status of the PCAN-Basic message filter /// </summary> /// <param name="status">Buffer to retrieve the filter status</param> /// <returns>If calling the function was successfull or not</returns> private bool GetFilterStatus(short Ch, out uint status) { #if PROGRAM_RUNNING TPCANStatus stsResult; status = 0; try { try { // Tries to get the sttaus of the filter for the current connected hardware // stsResult = PCANBasic.GetValue(m_PcanHandle[Ch], TPCANParameter.PCAN_MESSAGE_FILTER, out status, sizeof(UInt32)); // If it fails, a error message is shown // if (stsResult != TPCANStatus.PCAN_ERROR_OK) { //MessageBox.Show(GetFormatedError(stsResult)); uMessageBox.Show(promptText: GetFormatedError(stsResult), title: "경고"); return(false); } } catch (Exception Msg) { //MessageBox.Show(Msg.Message + "\n" + Msg.StackTrace); uMessageBox.Show(promptText: Msg.Message + "\n" + Msg.StackTrace, title: "경고"); } } finally { } #else status = 0; #endif return(true); }
private void GetHwInfor() { UInt32 iBuffer; TPCANStatus stsResult; bool isFD; UInt32 Ch; UInt32 DeviceCh; StringBuilder HwName = new StringBuilder(); //StringBuilder HwDevice = new StringBuilder(); // Clears the Channel combioBox and fill it again with // the PCAN-Basic handles for no-Plug&Play hardware and // the detected Plug&Play hardware // CanList.Clear(); try { for (int i = 0; i < m_HandlesArray.Length; i++) { // Includes all no-Plug&Play Handles if (m_HandlesArray[i] <= PCANBasic.PCAN_DNGBUS1) { CanList.Add(FormatChannelName(m_HandlesArray[i])); } else { // Checks for a Plug&Play Handle and, according with the return value, includes it // into the list of available hardware channels. // stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_CHANNEL_CONDITION, out iBuffer, sizeof(UInt32)); if ((stsResult == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.PCAN_CHANNEL_AVAILABLE) == PCANBasic.PCAN_CHANNEL_AVAILABLE)) { stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_CONTROLLER_NUMBER, out Ch, sizeof(UInt32)); stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_HARDWARE_NAME, HwName, 20); stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_CHANNEL_FEATURES, out iBuffer, sizeof(UInt32)); isFD = (stsResult == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.FEATURE_FD_CAPABLE) == PCANBasic.FEATURE_FD_CAPABLE); if (isFD) { string s = "f_clock_mhz = 20,nom_brp = 5,nom_tseg1 = 2,nom_tseg2 = 1,nom_sjw = 1,data_brp = 2,data_tseg1 = 3, data_tseg2 = 1,data_sjw = 1"; stsResult = PCANBasic.InitializeFD( m_HandlesArray[i], s); } else { //m_HwType = TPCANType.PCAN_TYPE_ISA; stsResult = PCANBasic.Initialize( m_HandlesArray[i], m_Baudrate[i], m_HwType[i], 0x100, 3); } stsResult = PCANBasic.GetValue(m_HandlesArray[i], TPCANParameter.PCAN_DEVICE_NUMBER, out DeviceCh, sizeof(UInt32)); PCANBasic.Uninitialize(m_HandlesArray[i]); //CanList.Add(FormatChannelName(m_HandlesArray[i], isFD, (Int32)Ch)); CanList.Add(FormatChannelName(m_HandlesArray[i], isFD, (Int32)Ch, HwName.ToString(), DeviceCh)); } } } } catch (DllNotFoundException) { //MessageBox.Show("Unable to find the library: PCANBasic.dll !", "Error!"/*, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error*/); uMessageBox.Show(title: "Error!", "Unable to find the library: PCANBasic.dll !"); //MessageBox.Show(GetFormatedError(stsResult)); //Environment.Exit(-1); } return; }
public PeakCan(int p_peak_id, TPCANBaudrate p_baud_rate = TPCANBaudrate.PCAN_BAUD_1M) { TPCANStatus status; uint condition; uint device_id; foreach (TPCANHandle channel in USB_CHANNELS) { status = PCANBasic.GetValue(channel, TPCANParameter.PCAN_CHANNEL_CONDITION, out condition, sizeof(UInt32)); if (status == TPCANStatus.PCAN_ERROR_OK && (condition & PCANBasic.PCAN_CHANNEL_AVAILABLE) == PCANBasic.PCAN_CHANNEL_AVAILABLE) { status = PCANBasic.Initialize(channel, p_baud_rate); if (status == TPCANStatus.PCAN_ERROR_OK) { status = PCANBasic.GetValue(channel, TPCANParameter.PCAN_DEVICE_NUMBER, out device_id, sizeof(UInt32)); if (status == TPCANStatus.PCAN_ERROR_OK && device_id == p_peak_id) { m_sock = channel; } PCANBasic.Uninitialize(channel); } } } if (m_sock == 0) { throw new Exception($"PEAK CAN USB adapt with id 0x{p_peak_id:X} not found"); } else { status = PCANBasic.Initialize(m_sock, p_baud_rate); if (status != TPCANStatus.PCAN_ERROR_OK) { throw new Exception($"Error initializing CAN with id 0x{p_peak_id:X}"); } status = PCANBasic.Reset(m_sock); if (status != TPCANStatus.PCAN_ERROR_OK) { throw new Exception(GetFormatedError(status)); } uint numeric_buffer = PCANBasic.PCAN_PARAMETER_ON; status = PCANBasic.SetValue(m_sock, TPCANParameter.PCAN_RECEIVE_EVENT, ref numeric_buffer, sizeof(UInt32)); if (status != TPCANStatus.PCAN_ERROR_OK) { throw new Exception(GetFormatedError(status)); } status = PCANBasic.GetStatus(m_sock); if (status != TPCANStatus.PCAN_ERROR_OK) { throw new Exception(GetFormatedError(status)); } } m_queue_rx = new BlockingCollection <TPCANMsg>(); m_queue_tx = new BlockingCollection <TPCANMsg>(); m_thread_stop = false; m_thread_rx = new Thread(new ThreadStart(ReadRawFrame)); m_thread_rx.IsBackground = true; m_thread_rx.Start(); m_thread_tx = new Thread(new ThreadStart(WriteRawFrame)); m_thread_tx.IsBackground = true; m_thread_tx.Start(); }