예제 #1
0
        public void CanReceiveReset(short Ch)
        {
#if PROGRAM_RUNNING
            TPCANStatus stsResult;

            stsResult = PCANBasic.Reset(m_PcanHandle[Ch]);
#endif
            return;
        }
예제 #2
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
        }
예제 #3
0
 public void Flush()
 {
     if (m_open)
     {
         lock (m_receiveLock)
         {
             PCANBasic.Reset(m_handle);
             foreach (var r in m_receivers)
             {
                 r.Flush(true);
             }
         }
     }
 }
예제 #4
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();
        }