public TPCANStatus Init() { TPCANStatus result; StringBuilder strMsg; result = PCANBasic.Initialize(mChannel, mBaudrate); if (result != TPCANStatus.PCAN_ERROR_OK) { strMsg = new StringBuilder(256); PCANBasic.GetErrorText(result, 0, strMsg); MessageBox.Show(strMsg.ToString()); } else { MessageBox.Show("PCAN-USB was initialized successfully!"); // Turn the power source oparationa on byte[] data = { 0x01, mDeviceId }; Write("000", data); // data = new byte[8]; data[0] = 0x02; Write("202", data); StartTimers(); } return(result); }
public bool Open([Implicit] ICallContext context) { if (!m_open) { TPCANStatus result = PCANBasic.Initialize(m_handle, ToPCANBaudrate(m_baudrate), TPCANType.PCAN_TYPE_ISA, 0, 0); if (this.UpdateStatusFromOperation(result) == TPCANStatus.PCAN_ERROR_OK) { if (context != null && context.LoggingEnabled) { context.Logger.Log("Open", "Opened successfully"); } m_open = true; if (m_receiverThread == null) { m_receiverThread = new Thread(new ThreadStart(this.ReceiveThreadHandler)); m_receiverThread.Start(); //Core.Main.ServiceManager.Get<> } return(true); } else { if (context != null) { context.Logger.LogError("PCAN", "Open failed"); } } } return(false); }
private void Initialize_Click(object sender, EventArgs e) { TPCANStatus stsResult; PCANBasic.Uninitialize(m_PcanHandle); stsResult = PCANBasic.Initialize(m_PcanHandle, m_Baudrate, m_HwType, Convert.ToUInt32(0x04), Convert.ToUInt16(3)); if (stsResult != TPCANStatus.PCAN_ERROR_OK) { MessageBox.Show(Convert.ToString(stsResult)); } else { MessageBox.Show("Connection Successful!"); boardVoltageTimer = new System.Timers.Timer(350); boardVoltageTimer.Elapsed += boardVoltage_thread; boardVoltageTimer.Enabled = true; statusUpdateTimer = new System.Timers.Timer(100); statusUpdateTimer.Elapsed += statusUpdate_; statusUpdateTimer.Enabled = true; chnlSwitchTimer = new System.Timers.Timer(100); chnlSwitchTimer.Elapsed += chnlSwitchThread; chnlSwitchTimer.Enabled = true; toggleSwitch1.Enabled = false;; porSWITCH.Enabled = false; crntRatingTBar.Enabled = false; Initialize.Enabled = false; } }
public PCAN(Ports device, long baudrate) { iface = device; Bitrate = baudrate; PCANBasic.Initialize((byte)iface, baud); rx = new Thread(_read); rx.IsBackground = true; rx.Start(); }
public void Open(Baudrate speed) { TPCANBaudrate bitrate; switch (speed) { case Baudrate.Baudrate5000: bitrate = TPCANBaudrate.PCAN_BAUD_5K; break; case Baudrate.Baudrate10000: bitrate = TPCANBaudrate.PCAN_BAUD_10K; break; case Baudrate.Baudrate20000: bitrate = TPCANBaudrate.PCAN_BAUD_20K; break; case Baudrate.Baudrate50000: bitrate = TPCANBaudrate.PCAN_BAUD_50K; break; case Baudrate.Baudrate100000: bitrate = TPCANBaudrate.PCAN_BAUD_100K; break; case Baudrate.Baudrate125000: bitrate = TPCANBaudrate.PCAN_BAUD_125K; break; case Baudrate.Baudrate250000: bitrate = TPCANBaudrate.PCAN_BAUD_250K; break; case Baudrate.Baudrate500000: bitrate = TPCANBaudrate.PCAN_BAUD_500K; break; case Baudrate.Baudrate800000: bitrate = TPCANBaudrate.PCAN_BAUD_800K; break; case Baudrate.Baudrate1000000: bitrate = TPCANBaudrate.PCAN_BAUD_1M; break; default: throw new Exception("Invalid baudrate."); } var ret = PCANBasic.Initialize(Channel, bitrate); CheckError(ret); }
/* * The handle of a PCAN Channel (see TPCANHandle). * Btr0Btr1 The speed for the communication (BTR0BTR1 code). * HwType The type of hardware and operation mode (see TPCANMode). * IOPort The I/O address for the parallel port. * Interrupt Interrupt number of the parallel port */ public void Btn_connectCAN_Click(object sender, EventArgs e) { TPCANStatus stsResult; //CAN/LIN0 m_PcanHandle = Convert.ToByte("51", 16); stsResult = PCANBasic.Initialize( m_PcanHandle, TPCANBaudrate.PCAN_BAUD_250K); //250kbs /*if(appCANbaudrate.SelectedIndex==0){ * stsResult = PCANBasic.Initialize( * m_PcanHandle, * TPCANBaudrate.PCAN_BAUD_250K); * }else { * stsResult = PCANBasic.Initialize( * m_PcanHandle, * TPCANBaudrate.PCAN_BAUD_500K); * * }*/ if (stsResult != TPCANStatus.PCAN_ERROR_OK) { MessageBox.Show(GetFormatedError(stsResult)); } else { // Prepares the PCAN-Basic's PCAN-Trace file // //ConfigureTraceFile(); // Display messages in grid // tmrDisplay.Enabled = true; } tmrRead.Enabled = true; btnSaveSettings.Enabled = true; btnReleseCAN.Enabled = true; Btn_connectCAN.Enabled = false; IncludeTextMessage("CAN connected"); // Sets the connection status of the main-form // //SetConnectionStatus(stsResult == TPCANStatus.PCAN_ERROR_OK); }
private void btnConnect_Click(object sender, RoutedEventArgs e) { stsResult = PCANBasic.Initialize(m_PcanHandle, m_Baudrate, m_HwType, Convert.ToUInt32("0100", 16), 3); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { btnConnect.IsEnabled = false; cbDevice.IsEnabled = false; cbBaudRate.IsEnabled = false; btnRefresh.IsEnabled = false; tbAlarmLog.Text += "Device was successfully configured\n"; } else { tbAlarmLog.Text += $"{stsResult}\n"; } }
/// <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; } } }
/// <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 btnInit_Click(object sender, EventArgs e) /* * strBitrate 波特率 * */ private bool PCAN_Init(string strBitrate) { bool bInitRet = false; m_PcanHandle = Convert.ToUInt16("51", 16); m_Baudrate = TPCANBaudrate.PCAN_BAUD_500K; m_HwType = TPCANType.PCAN_TYPE_ISA; TPCANStatus stsResult; // Connects a selected PCAN-Basic channel // if (m_IsFD) { stsResult = PCANBasic.InitializeFD(m_PcanHandle, strBitrate); } else { stsResult = PCANBasic.Initialize( m_PcanHandle, m_Baudrate, m_HwType, 256, // Convert.ToUInt32(strIO, 16), 3); // Convert.ToUInt16(strInterrupt)); } if (stsResult != TPCANStatus.PCAN_ERROR_OK) { if (stsResult != TPCANStatus.PCAN_ERROR_CAUTION) { //MessageBox.Show(GetFormatedError(stsResult)); bInitRet = false; } } else { // Prepares the PCAN-Basic's PCAN-Trace file bInitRet = true; } return(bInitRet); }
public TPCANStatus connect(TPCANHandle handler, TPCANBaudrate baudrate, TPCANType type, UInt32 io, UInt16 interrupt) { TPCANStatus stsResult; m_PcanHandle = handler; // Connects a selected PCAN-Basic channel // stsResult = PCANBasic.Initialize( m_PcanHandle, baudrate, type, io, interrupt); readCanTimer = new Timer(50); readCanTimer.Start(); readCanTimer.Elapsed += OnTimedEvent; return(stsResult); }
public bool OpenCan(short Channel, int ID, short Speed, bool m_IsFD = false) { #if PROGRAM_RUNNING TPCANStatus stsResult; UInt32 iBuffer; //-----------------------------[ Init can board ] // Connects a selected PCAN-Basic channel // try { try { m_PcanHandle[Channel] = (byte)ID; switch (Speed) { case 0: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_5K; break; case 1: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_10K; break; case 2: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_20K; break; case 3: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_33K; break; case 4: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_47K; break; case 5: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_50K; break; case 6: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_83K; break; case 7: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_95K; break; case 8: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_100K; break; case 9: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_125K; break; case 10: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_250K; break; case 11: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_500K; break; case 12: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_800K; break; case 13: m_Baudrate[Channel] = TPCANBaudrate.PCAN_BAUD_1M; break; } if (m_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_PcanHandle[Channel], s); } else { //m_HwType = TPCANType.PCAN_TYPE_ISA; stsResult = PCANBasic.Initialize( m_PcanHandle[Channel], m_Baudrate[Channel], m_HwType[Channel], 0x100, 3); } if (stsResult != TPCANStatus.PCAN_ERROR_OK) { uMessageBox.Show(promptText: GetFormatedError(stsResult), title: "경고"); //MessageBox.Show(GetFormatedError(stsResult)); } else { // Prepares the PCAN-Basic's PCAN-Trace file // ConfigureTraceFile((short)Channel); } //-----------------------------[ Open can board ] // Gets the current status of the message filter // if (!GetFilterStatus((short)Channel, out iBuffer)) { return(false); } // The filter will be full opened or complete closed // iBuffer = PCANBasic.PCAN_FILTER_OPEN; // The filter is configured // stsResult = PCANBasic.SetValue( m_PcanHandle[Channel], TPCANParameter.PCAN_MESSAGE_FILTER, ref iBuffer, sizeof(UInt32)); // If success, an information message is written, if it is not, an error message is shown // if (stsResult == TPCANStatus.PCAN_ERROR_OK) { Open[Channel] = true; } else { Open[Channel] = false; } if (stsResult == TPCANStatus.PCAN_ERROR_OK) { return(true); } else { uMessageBox.Show(promptText: GetFormatedError(stsResult), title: "경고"); //MessageBox.Show(GetFormatedError(stsResult)); } } catch (Exception Msg) { //MessageBox.Show(Msg.Message + "\n" + Msg.StackTrace); uMessageBox.Show(promptText: Msg.Message + "\n" + Msg.StackTrace, title: "경고"); //MessageBox.Show(GetFormatedError(stsResult)); } } finally { } return(false); #else return(true); #endif }
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(); }
private void btOpen_Click(object sender, EventArgs e) { switch (cbDevice.SelectedIndex) { case (int)CONNECTON_TYPES.USB: try { serial.PortName = cbPort.SelectedItem.ToString(); serial.BaudRate = 115200; serial.Open(); serial.DiscardInBuffer(); serial.DiscardOutBuffer(); System.Threading.Thread.Sleep(100); serial.DtrEnable = true; System.Threading.Thread.Sleep(100); serial.DtrEnable = false; System.Threading.Thread.Sleep(100); this.Text = nazev_aplikace + " - " + serial.PortName.ToString(); } catch (Exception x) { MessageBox.Show(x.Message); return; } break; case (int)CONNECTON_TYPES.PEAK: TPCANStatus stsResult; m_PcanHandle = 0x51; // Connects a selected PCAN-Basic channel // stsResult = PCANBasic.Initialize( m_PcanHandle, TPCANBaudrate.PCAN_BAUD_100K, TPCANType.PCAN_TYPE_ISA, (UInt32)100, (UInt16)3); if (stsResult != TPCANStatus.PCAN_ERROR_OK) { MessageBox.Show(GetFormatedError(stsResult)); return; } this.Text = nazev_aplikace + " - PEAK"; tReader.Start(); // Sets the connection status of the main-form // break; case (int)CONNECTON_TYPES.VSCP: vscp = new vscp_daemon_iface(); if (!vscp.Open(tbIp.Text, 9598)) { MessageBox.Show("Server error!"); return; } if (!vscp.Login(tbUser.Text, tbPass.Text)) { MessageBox.Show("Login or password failure"); return; } tReader.Start(); break; default: break; } btClose.Enabled = true; btOpen.Enabled = false; btSend.Enabled = true; }
/*---------------------------------------------------------------------------------------------------*/ public bool startDevice() { TPCANStatus result1 = 0; //if (CANINDEX != 0 && CANINDEX != 1) //{ // MessageBox.Show("请指定设备参数"); // return false; //} if (Form_SetCANParam.form_setcan.checkCANFDMode.Checked == true) { result1 = PCANBasic.InitializeFD(PCAN_PARA1.PCANIndex, "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"); } else { result1 = PCANBasic.Initialize(PCAN_PARA1.PCANIndex, PCAN_PARA1.BTR0_1, TPCANType.PCAN_TYPE_ISA, 0x378, 3); } if (result1 == TPCANStatus.PCAN_ERROR_OK) { //MessageBox.Show("设备启动成功"); switch (PCAN_PARA1.PCANIndex) { case 0x0051: PCAN_COMMON_VAL.pcan_init_flag0 = 0x69; //通道1初始化成功标志 break; case 0x0052: PCAN_COMMON_VAL.pcan_init_flag1 = 0x69; //通道1初始化成功标志} break; case 0x0053: PCAN_COMMON_VAL.pcan_init_flag2 = 0x69; //通道1初始化成功标志 break; case 0x0054: PCAN_COMMON_VAL.pcan_init_flag3 = 0x69; //通道1初始化成功标志} break; case 0x0055: PCAN_COMMON_VAL.pcan_init_flag4 = 0x69; //通道1初始化成功标志 break; case 0x0056: PCAN_COMMON_VAL.pcan_init_flag5 = 0x69; //通道1初始化成功标志} break; case 0x0057: PCAN_COMMON_VAL.pcan_init_flag6 = 0x69; //通道1初始化成功标志 break; case 0x0058: PCAN_COMMON_VAL.pcan_init_flag7 = 0x69; //通道1初始化成功标志} break; } if (Form_SetCANParam.form_setcan.checkCANFDMode.Checked == true) { Form_BasicFunction.form_basic.checkEnableCANFD.Enabled = true; Form_BasicFunction.form_basic.EnableCANFD_BRS.Enabled = true; } else { Form_BasicFunction.form_basic.checkEnableCANFD.Checked = false; Form_BasicFunction.form_basic.checkEnableCANFD.Enabled = false; Form_BasicFunction.form_basic.EnableCANFD_BRS.Enabled = false; Form_BasicFunction.form_basic.EnableCANFD_BRS.Checked = false; } if (thread == null) //if(true)//方便连续直接打开设备 { thread = new Thread(receivingThread); thread.Start(); } IsOpen = true; return(true); } else { goto errorReporting; } errorReporting: if (result1 == TPCANStatus.PCAN_ERROR_ILLHW) { MessageBox.Show("请插入PCAN-USB设备"); } else { MessageBox.Show("PCAN-USB打开失败"); } return(false); }