Example #1
0
        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;
            }
        }
Example #2
0
        private void btClose_Click(object sender, EventArgs e)
        {
            switch (cbDevice.SelectedIndex)
            {
            case (int)CONNECTON_TYPES.USB: serial.Close();
                break;

            case (int)CONNECTON_TYPES.PEAK:
                PCANBasic.Uninitialize(m_PcanHandle);
                m_PcanHandle = 0;
                break;

            case (int)CONNECTON_TYPES.VSCP:
                vscp.Close();
                break;

            default:
                break;
            }

            tReader.Stop();
            btOpen.Enabled  = true;
            btClose.Enabled = false;
            btSend.Enabled  = false;
        }
Example #3
0
 /// <summary>
 /// Disconnect to can bus.
 /// </summary>
 public void disconnect()//object sender, EventArgs e)
 {
     if (this._canChannel > 0)
     {
         PCANBasic.Uninitialize(this._canChannel);
     }
     this.isRunning = false;
 }
Example #4
0
 private void Form_BasicFunction_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (PCAN_COMMON_VAL.pcan_init_flag1 == 0x69 || PCAN_COMMON_VAL.pcan_init_flag2 == 0x69) //初始化成功
     {                                                                                       // usbCAN.shutDevice();
         PCANBasic.Uninitialize(0x0051);
         PCANBasic.Uninitialize(0x0052);
         //MessageBox.Show("通道all设备关闭成功");
     }
 }
Example #5
0
        public void CanClose(short Ch)
        {
#if PROGRAM_RUNNING
            TPCANStatus stsResult;
            UInt32      iBuffer;

            try
            {
                try
                {
                    Open[Ch] = false;
                    //-----------------------------[ Open can board ]
                    // Gets the current status of the message filter
                    //
                    if (!GetFilterStatus(Ch, out iBuffer))
                    {
                        return;
                    }

                    PCANBasic.Reset(m_PcanHandle[Ch]);

                    // The filter will be full opened or complete closed
                    //
                    iBuffer = PCANBasic.PCAN_FILTER_CLOSE;

                    // The filter is configured
                    //
                    stsResult = PCANBasic.SetValue(
                        m_PcanHandle[Ch],
                        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)
                    {
                        PCANBasic.Uninitialize(m_PcanHandle[Ch]);
                        return;
                    }
                }
                catch (Exception Msg)
                {
                    //MessageBox.Show(Msg.Message + "\n" + Msg.StackTrace);
                    uMessageBox.Show(title: "경고", promptText: Msg.Message + "\n" + Msg.StackTrace); //MessageBox.Show(GetFormatedError(stsResult));
                }
            }
            finally
            {
            }
            return;
#else
            return;
#endif
        }
Example #6
0
        private void btnRelease_Click(object sender, RoutedEventArgs e)
        {
            stsResult    = PCANBasic.Uninitialize(m_PcanHandle); //DISCONNECT PCAN DEVICE
            m_PcanHandle = 0;                                    //RESET PCAN HANDLE

            btnConnect.IsEnabled = true;
            cbDevice.IsEnabled   = true;
            cbBaudRate.IsEnabled = true;
            btnRefresh.IsEnabled = true;

            tbAlarmLog.Text += "Device was successfully released\n";
        }
Example #7
0
File: PCAN.cs Project: fbstj/lib.cs
        /// <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);
        }
Example #8
0
 public bool Close([Implicit] ICallContext context)
 {
     if (m_open)
     {
         this.Flush();
         m_open = false;
         while (m_receiverThread != null)
         {
             System.Threading.Thread.Sleep(100);
         }
         m_timerActions.DeactivateAll();
         TPCANStatus result = PCANBasic.Uninitialize(m_handle);
         if (this.UpdateStatusFromOperation(result) == TPCANStatus.PCAN_ERROR_OK)
         {
             return(true);
         }
     }
     return(false);
 }
Example #9
0
        public bool ConnectRelease()
        {
            // Releases a current connected PCAN-Basic channel
            //
            TPCANStatus _status = PCANBasic.Uninitialize(m_PcanHandle);

            if (_status == TPCANStatus.PCAN_ERROR_OK)
            {
                //tmrRead.Enabled = false;
                if (m_ReadThread != null)
                {
                    m_ReadThread.Abort();
                    m_ReadThread.Join();
                    m_ReadThread = null;
                }
                return(true);
            }

            return(false);
            // Sets the connection status of the main-form
            //
            //SetConnectionStatus(false);
        }
Example #10
0
        public void btnReleseCAN_Click(object sender, EventArgs e)
        {
            // Releases a current connected PCAN-Basic channel
            //
            PCANBasic.Uninitialize(m_PcanHandle);
            tmrRead.Enabled = false;


            if (m_ReadThread != null)
            {
                m_ReadThread.Abort();
                m_ReadThread.Join();
                m_ReadThread = null;
            }
            btnSaveSettings.Enabled = false;
            btnReleseCAN.Enabled    = false;

            Btn_connectCAN.Enabled = true;


            // Sets the connection status of the main-form
            //
            // SetConnectionStatus(false);
        }
Example #11
0
 public void disconnect()
 {
     // Releases a current connected PCAN-Basic channel
     PCANBasic.Uninitialize(m_PcanHandle);
 }
Example #12
0
        public void Close()
        {
            var ret = PCANBasic.Uninitialize(Channel);

            CheckError(ret);
        }
Example #13
0
        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;
        }
Example #14
0
        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();
        }
Example #15
0
        /*---------------------------------------------------------------------------------------------------*/
        public bool shutDevice()
        {
            TPCANStatus result1;

            result1 = PCANBasic.Uninitialize(PCAN_PARA1.PCANIndex);
            if (result1 == TPCANStatus.PCAN_ERROR_OK)
            {
                if (thread != null)
                {
                    thread.Abort();
                    thread = null;
                }
                //MessageBox.Show("关闭设备成功");
                switch (PCAN_PARA1.PCANIndex)
                {
                case 0x0051:
                    PCAN_COMMON_VAL.pcan_init_flag0 = 0xff;    //通道0关闭成功标志
                    break;

                case 0x0052:
                    PCAN_COMMON_VAL.pcan_init_flag1 = 0xff;    //通道1关闭成功标志
                    break;

                case 0x0053:
                    PCAN_COMMON_VAL.pcan_init_flag2 = 0xff;    //通道2关闭成功标志
                    break;

                case 0x0054:
                    PCAN_COMMON_VAL.pcan_init_flag3 = 0xff;    //通道3关闭成功标志
                    break;

                case 0x0055:
                    PCAN_COMMON_VAL.pcan_init_flag4 = 0xff;    //通道4关闭成功标志
                    break;

                case 0x0056:
                    PCAN_COMMON_VAL.pcan_init_flag5 = 0xff;    //通道5关闭成功标志
                    break;

                case 0x0057:
                    PCAN_COMMON_VAL.pcan_init_flag6 = 0xff;    //通道6关闭成功标志
                    break;

                case 0x0058:
                    PCAN_COMMON_VAL.pcan_init_flag7 = 0xff;    //通道7关闭成功标志
                    break;
                }
                Thread.Sleep(100);
                IsOpen = false;
                return(true);
            }
            else
            {
                goto errorReporting;
            }

errorReporting:
            if (thread != null)
            {
                thread.Abort();
                thread = null;
            }
            if (result1 == TPCANStatus.PCAN_ERROR_INITIALIZE)
            {
                MessageBox.Show("关闭设备失败");
            }
            else
            {
                MessageBox.Show("关闭设备失败");
            }

            return(false);
        }