Esempio n. 1
0
        private void ReadRawFrame()
        {
            AutoResetEvent can_event = new AutoResetEvent(false);

            uint        numeric_buffer = Convert.ToUInt32(can_event.SafeWaitHandle.DangerousGetHandle().ToInt32());
            TPCANStatus 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));
            }

            TPCANMsg raw_frame;

            while (!m_thread_stop)
            {
                if (can_event.WaitOne(50))
                {
                    do
                    {
                        if ((status = PCANBasic.Read(m_sock, out raw_frame)) == TPCANStatus.PCAN_ERROR_OK)
                        {
                            m_queue_rx.Add(raw_frame);
                        }
                    } while (!Convert.ToBoolean(status & TPCANStatus.PCAN_ERROR_QRCVEMPTY));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Configures the PCAN-Trace file for a PCAN-Basic Channel
        /// </summary>
        private void ConfigureTraceFile()
        {
            UInt32      iBuffer;
            TPCANStatus stsResult;

            // Configure the maximum size of a trace file to 5 megabytes
            //
            iBuffer   = 5;
            stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_TRACE_SIZE, ref iBuffer, sizeof(UInt32));
            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                IncludeTextMessage(GetFormatedError(stsResult));
            }

            // Configure the way how trace files are created:
            // * Standard name is used
            // * Existing file is ovewritten,
            // * Only one file is created.
            // * Recording stopts when the file size reaches 5 megabytes.
            //
            iBuffer   = PCANBasic.TRACE_FILE_SINGLE | PCANBasic.TRACE_FILE_OVERWRITE;
            stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_TRACE_CONFIGURE, ref iBuffer, sizeof(UInt32));
            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                IncludeTextMessage(GetFormatedError(stsResult));
            }
        }
Esempio n. 3
0
File: PCAN.cs Progetto: fbstj/lib.cs
        private void _read()
        {               // threaded recieve listener
            AutoResetEvent m_ReceiveEvent = new AutoResetEvent(false);
            UInt32         iBuffer;
            TPCANStatus    stsResult;

            iBuffer = Convert.ToUInt32(m_ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
            // Sets the handle of the Receive-Event.
            stsResult = PCANBasic.SetValue((byte)iface, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));

            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                throw new Exception(GetFormatedError(stsResult));
            }

            while (true)
            {
                if (m_ReceiveEvent.WaitOne(50))
                {                       // wait or sleep
                    TPCANMsg m; TPCANTimestamp ts;
                    do
                    {                           // read all messages from queue, and 'receive' them
                        stsResult = PCANBasic.Read((byte)iface, out m, out ts);

                        if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                        {
                            Receive.Invoke(Frame(m));
                            TimedReceive.Invoke(Time(ts), Frame(m));
                        }
                    } while ((!Convert.ToBoolean(stsResult & TPCANStatus.PCAN_ERROR_QRCVEMPTY)));
                }
            }
        }
Esempio n. 4
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
        }
Esempio n. 5
0
        /// <summary>
        /// Configures the Debug-Log file of PCAN-Basic
        /// </summary>
        private void ConfigureLogFile()
        {
            UInt32 iBuffer;

            // Sets the mask to catch all events
            //
            iBuffer = PCANBasic.LOG_FUNCTION_ALL;

            // Configures the log file.
            // NOTE: The Log capability is to be used with the NONEBUS Handle. Other handle than this will
            // cause the function fail.
            //
            PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_CONFIGURE, ref iBuffer, sizeof(UInt32));
        }
Esempio n. 6
0
        /// <summary>
        /// Configures the PCAN-Trace file for a PCAN-Basic Channel
        /// </summary>
        private void ConfigureTraceFile(short Ch)
        {
#if PROGRAM_RUNNING
            UInt32      iBuffer;
            TPCANStatus stsResult;

            try
            {
                try
                {
                    // Configure the maximum size of a trace file to 5 megabytes
                    //
                    iBuffer   = 5;
                    stsResult = PCANBasic.SetValue(m_PcanHandle[Ch], TPCANParameter.PCAN_TRACE_SIZE, ref iBuffer, sizeof(UInt32));
                    if (stsResult != TPCANStatus.PCAN_ERROR_OK)
                    {
                        uMessageBox.Show(promptText: GetFormatedError(stsResult), title: "경고");                                         //MessageBox.Show(GetFormatedError(stsResult));
                    }
                    // Configure the way how trace files are created:
                    // * Standard name is used
                    // * Existing file is ovewritten,
                    // * Only one file is created.
                    // * Recording stopts when the file size reaches 5 megabytes.
                    //
                    iBuffer   = PCANBasic.TRACE_FILE_SINGLE | PCANBasic.TRACE_FILE_OVERWRITE;
                    stsResult = PCANBasic.SetValue(m_PcanHandle[Ch], TPCANParameter.PCAN_TRACE_CONFIGURE, ref iBuffer, sizeof(UInt32));
                    if (stsResult != TPCANStatus.PCAN_ERROR_OK)
                    {
                        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: "경고");
                }
            }
            finally
            {
            }
#endif
            return;
        }
Esempio n. 7
0
        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
        }
Esempio n. 8
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();
        }
Esempio n. 9
0
        //private void btnParameterSet_Click(object sender, EventArgs e)
        private bool PCAN_SetParameter(uint nDevice)
        {
            bool bSetRet = false;

            TPCANStatus stsResult;
            UInt32      iBuffer;
            bool        bActivate;

            bActivate = true; //rdbParamActive.Checked;

            // Sets a PCAN-Basic parameter value
            //
            switch (0)
            {
            // The device identifier of a channel will be set
            //
            case 0:
                iBuffer   = nDevice;   // Convert.ToUInt32(nudDeviceIdOrDelay.Value);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_DEVICE_ID, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage("The desired Device-ID was successfully configured");
                bSetRet = true;
                break;

            // The 5 Volt Power feature of a channel will be set
            //
            case 1:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_5VOLTS_POWER, ref iBuffer, sizeof(UInt32));
                break;

            // The feature for automatic reset on BUS-OFF will be set
            //
            case 2:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_BUSOFF_AUTORESET, ref iBuffer, sizeof(UInt32));
                break;

            // The CAN option "Listen Only" will be set
            //
            case 3:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_LISTEN_ONLY, ref iBuffer, sizeof(UInt32));
                break;

            // The feature for logging debug-information will be set
            //
            case 4:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_STATUS, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The feature for logging debug information was successfully {0}", bActivate ? "activated" : "deactivated"));
                break;

            // The channel option "Receive Status" will be set
            //
            case 5:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_RECEIVE_STATUS, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The channel option \"Receive Status\" was set to {0}", bActivate ? "ON" : "OFF"));
                break;

            // The feature for tracing will be set
            //
            case 7:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_TRACE_STATUS, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The feature for tracing data was successfully {0}", bActivate ? "activated" : "deactivated"));
                break;

            // The feature for identifying an USB Channel will be set
            //
            case 8:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_CHANNEL_IDENTIFYING, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The procedure for channel identification was successfully {0}", bActivate ? "activated" : "deactivated"));
                break;

            // The feature for using an already configured speed will be set
            //
            case 10:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_BITRATE_ADAPTING, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The feature for bit rate adaptation was successfully {0}", bActivate ? "activated" : "deactivated"));
                break;

            // The option "Allow Status Frames" will be set
            //
            case 17:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_ALLOW_STATUS_FRAMES, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The reception of Status frames was successfully {0}", bActivate ? "enabled" : "disabled"));
                break;

            // The option "Allow RTR Frames" will be set
            //
            case 18:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_ALLOW_RTR_FRAMES, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The reception of RTR frames was successfully {0}", bActivate ? "enabled" : "disabled"));
                break;

            // The option "Allow Error Frames" will be set
            //
            case 19:
                iBuffer   = (uint)(bActivate ? PCANBasic.PCAN_PARAMETER_ON : PCANBasic.PCAN_PARAMETER_OFF);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_ALLOW_ERROR_FRAMES, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage(string.Format("The reception of Error frames was successfully {0}", bActivate ? "enabled" : "disabled"));
                break;

            // The option "Interframes Delay" will be set
            //
            case 20:
                iBuffer   = nDevice;   // Convert.ToUInt32(nudDeviceIdOrDelay.Value);
                stsResult = PCANBasic.SetValue(m_PcanHandle, TPCANParameter.PCAN_INTERFRAME_DELAY, ref iBuffer, sizeof(UInt32));
                //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                //    IncludeTextMessage("The delay between transmitting frames was successfully set");
                break;

            // The current parameter is invalid
            //
            default:
                stsResult = TPCANStatus.PCAN_ERROR_UNKNOWN;
                MessageBox.Show("Wrong parameter code.");
                break;
            }

            // If the function fail, an error message is shown
            //
            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                //MessageBox.Show(GetFormatedError(stsResult));
                bSetRet = true;
            }
            return(bSetRet);
        }