示例#1
0
 //Goes on bus
 public void busOn()
 {
     Canlib.canStatus status = Canlib.canBusOn(handle);
     if (status == Canlib.canStatus.canOK)
     {
         onBus = true;
     }
 }
示例#2
0
        /// <summary>
        /// The open method tries to connect to both busses to see if one of them is connected and
        /// active. The first strategy is to listen for any CAN message. If this fails there is a
        /// check to see if the application is started after an interrupted flash session. This is
        /// done by sending a message to set address and length (only for P-bus).
        /// </summary>
        /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is
        /// returned.</returns>
        override public OpenResult open()
        {
            Canlib.canInitializeLibrary();

            //Check if bus is connected
            if (isOpen())
            {
                close();
            }
            Thread.Sleep(200);
            m_readThread = new Thread(readMessages)
            {
                Name = "KvaserCANDevice.m_readThread"
            };

            m_endThread = false;

            // Take first adapter...
            string[] adapters = GetAdapterNames();
            if (adapters.Length == 0)
            {
                return(OpenResult.OpenError);
            }
            SetSelectedAdapter(adapters[0]);

            //Check if P bus is connected
            logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()");
            handleWrite = Canlib.canOpenChannel(ChannelNumber, 0);
            logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)");
            Canlib.canStatus statusSetParamWrite = Canlib.canSetBusParamsC200(handleWrite, 0x40, 0x37);
            logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)");
            Canlib.canStatus statusOnWrite = Canlib.canBusOn(handleWrite);
            Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
            //
            logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()");
            handleRead = Canlib.canOpenChannel(ChannelNumber, 0);
            logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)");
            Canlib.canStatus statusSetParamRead = Canlib.canSetBusParamsC200(handleRead, 0x40, 0x37);
            logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)");
            Canlib.canStatus statusOnRead = Canlib.canBusOn(handleRead);
            Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);

            if (handleWrite < 0 || handleRead < 0)
            {
                return(OpenResult.OpenError);
            }

            logger.Debug("P bus connected");
            if (m_readThread.ThreadState == ThreadState.Unstarted)
            {
                m_readThread.Start();
            }
            return(OpenResult.OK);
        }
 //Goes on bus
 public void busOn()
 {
     Canlib.canStatus status = Canlib.canBusOn(handle);
     if (status == Canlib.canStatus.canOK)
     {
         onBus = true;
         if (!dumper.IsBusy)
         {
             dumper.RunWorkerAsync();
         }
     }
 }
        //Goes on bus
        void busOnButton_Click(object sender, RoutedEventArgs e)
        {
            //Set Bitrate
            int[] bitrates = new int[] { Canlib.canBITRATE_125K, Canlib.canBITRATE_250K,
                                         Canlib.canBITRATE_500K, Canlib.BAUD_1M };
            Canlib.canStatus senderStatus = Canlib.canSetBusParams(sendHandle, bitrates[bitrateComboBox.SelectedIndex], 0, 0, 0, 0, 0);
            Canlib.canStatus readerStatus = Canlib.canSetBusParams(readHandle, bitrates[bitrateComboBox.SelectedIndex], 0, 0, 0, 0, 0);

            CheckStatus("Setting bitrate to " + ((ComboBoxItem)bitrateComboBox.SelectedValue).Content, senderStatus);
            if (senderStatus != Canlib.canStatus.canOK || readerStatus != Canlib.canStatus.canOK)
            {
                return;
            }

            //Bus On
            senderStatus = Canlib.canBusOn(sendHandle);
            readerStatus = Canlib.canBusOn(readHandle);
            CheckStatus("Bus on with bitrate " + ((ComboBoxItem)bitrateComboBox.SelectedValue).Content, senderStatus);
            if (senderStatus == Canlib.canStatus.canOK && readerStatus == Canlib.canStatus.canOK)
            {
                isSenderBusOn = true;
                isReaderBusOn = true;

                //This starts the DumpMessageLoop method
                if (!messageReceiver.IsBusy)
                {
                    messageReceiver.RunWorkerAsync();
                }

                busOnButton.IsEnabled        = false;
                bitrateComboBox.IsEnabled    = false;
                busOffButton.IsEnabled       = true;
                sendMessageButton.IsEnabled  = true;
                clearMessageButton.IsEnabled = true;
                buildMessageButton.IsEnabled = true;
            }
            else
            {
                MessageBox.Show("Bus On failed. Please try again.");
            }
        }
示例#5
0
        public KvaserCAN(MainWindow wnd) //public constructor
        {
            this.wnd = wnd;

            Canlib.canInitializeLibrary();

            hcan = Canlib.canOpenChannel(channel, Canlib.canOPEN_REQUIRE_INIT_ACCESS);
            if (hcan < 0)
            {
                string error;
                Canlib.canGetErrorText((Canlib.canStatus)hcan, out error);
                send2Terminal(error);
            }
            else
            {
                Canlib.canSetBusParams(hcan, bitr, 0, 0, 0, 0, 0); //parameters set by dafault based on bitr
                Canlib.canBusOn(hcan);
                send2Terminal("Can liin avatud");

                //DumpMessageLoop(hcan);
            }
        }
示例#6
0
        /// <summary>
        /// The open method tries to connect to both busses to see if one of them is connected and
        /// active. The first strategy is to listen for any CAN message. If this fails there is a
        /// check to see if the application is started after an interrupted flash session. This is
        /// done by sending a message to set address and length (only for P-bus).
        /// </summary>
        /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is
        /// returned.</returns>
        override public OpenResult open()
        {
            Canlib.canInitializeLibrary();

            //Check if bus is connected
            if (isOpen())
            {
                close();
            }
            Thread.Sleep(200);
            m_readThread = new Thread(readMessages)
            {
                Name = "KvaserCANDevice.m_readThread"
            };

            if (!UseOnlyPBus)
            {
                if (TrionicECU == ECU.TRIONIC7)
                {
                    logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()");
                    handleWrite = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
                    logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)");
                    Canlib.canStatus statusSetParam1 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_47K_btr0, CAN_BAUD_BTR_47K_btr1);
                    logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)");
                    Canlib.canStatus statusOn1 = Canlib.canBusOn(handleWrite);
                    Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
                    //
                    logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()");
                    handleRead = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
                    logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)");
                    Canlib.canStatus statusSetParam2 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_47K_btr0, CAN_BAUD_BTR_47K_btr1);
                    logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)");
                    Canlib.canStatus statusOn2 = Canlib.canBusOn(handleRead);
                    Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
                }
                else if (TrionicECU == ECU.TRIONIC8)
                {
                    logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()");
                    handleWrite = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
                    logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)");
                    Canlib.canStatus statusSetParam1 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_33K_btr0, CAN_BAUD_BTR_33K_btr1);
                    logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)");
                    Canlib.canStatus statusOn1 = Canlib.canBusOn(handleWrite);
                    Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
                    //
                    logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()");
                    handleRead = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
                    logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)");
                    Canlib.canStatus statusSetParam2 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_33K_btr0, CAN_BAUD_BTR_33K_btr1);
                    logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)");
                    Canlib.canStatus statusOn2 = Canlib.canBusOn(handleRead);
                    Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
                }

                if (handleWrite < 0 || handleRead < 0)
                {
                    return(OpenResult.OpenError);
                }

                logger.Debug("I bus connected");
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }

            m_endThread = false;

            //Check if P bus is connected
            logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()");
            handleWrite = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
            logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)");
            Canlib.canStatus statusSetParamWrite = Canlib.canSetBusParams(handleWrite, Canlib.canBITRATE_500K, 0, 0, 0, 0, 0);
            logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)");
            Canlib.canStatus statusOnWrite = Canlib.canBusOn(handleWrite);
            Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
            //
            logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()");
            handleRead = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
            logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)");
            Canlib.canStatus statusSetParamRead = Canlib.canSetBusParams(handleRead, Canlib.canBITRATE_500K, 0, 0, 0, 0, 0);
            logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)");
            Canlib.canStatus statusOnRead = Canlib.canBusOn(handleRead);
            Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);

            if (handleWrite < 0 || handleRead < 0)
            {
                return(OpenResult.OpenError);
            }

            logger.Debug("P bus connected");
            if (m_readThread.ThreadState == ThreadState.Unstarted)
            {
                m_readThread.Start();
            }
            return(OpenResult.OK);
        }
示例#7
0
        /// <summary>
        /// 打开CAN通道
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="baud"></param>
        /// <returns></returns>
        public bool OpenChannel(int channel, string baud)
        {
            int canFreq;

            canHandler = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL);
            if (canHandler != (int)Canlib.canStatus.canOK)
            {
                return(false);
            }

            switch (baud)
            {
            case "50000":
                canFreq = Canlib.BAUD_50K;
                break;

            case "62000":
                canFreq = Canlib.BAUD_62K;
                break;

            case "83000":
                canFreq = Canlib.BAUD_83K;
                break;

            case "100000":
                canFreq = Canlib.BAUD_100K;
                break;

            case "125000":
                canFreq = Canlib.BAUD_125K;
                break;

            case "250000":
                canFreq = Canlib.BAUD_250K;
                break;

            case "500000":
                canFreq = Canlib.BAUD_500K;
                break;

            case "1000000":
                canFreq = Canlib.BAUD_1M;
                break;

            default:
                canFreq = Canlib.BAUD_500K;
                break;
            }

            Canlib.canStatus canStatus = Canlib.canSetBusParams(canHandler, canFreq, 0x80, 0x3A, 1, 1, 0);
            if (canStatus != Canlib.canStatus.canOK)
            {
                return(false);
            }

            canStatus = Canlib.canBusOn(canHandler);
            if (canStatus != Canlib.canStatus.canOK)
            {
                return(false);
            }

            return(true);
        }
示例#8
0
        private void canThreadFunct()
        {
            int canHandle;

            Canlib.canStatus status;

            int channel_number = 0;
            int id;

            byte[] msg = new byte[8];
            int    dlc;
            int    flag;
            long   time;
            long   timeout;
            int    val0   = 0;
            int    val1   = 0;
            int    val2   = 0;
            int    val3   = 0;
            long   count0 = 1;
            long   count1 = 2;
            long   count2 = 3;
            long   count3 = 4;

            Canlib.canInitializeLibrary();
            canHandle = Canlib.canOpenChannel(channel_number, Canlib.canOPEN_NO_INIT_ACCESS);
            CheckStatus((Canlib.canStatus)canHandle, "canOpenChannel");

            // Next, take the channel on bus using the canBusOn method. This
            // needs to be done before we can send a message.
            status = Canlib.canBusOn(canHandle);
            CheckStatus(status, "canBusOn");

            timeout = 1000;

            while (!exit_request)
            {
                status = Canlib.canReadWait(canHandle, out id, msg, out dlc, out flag, out time, timeout);
                CheckStatus(status, "canReadWait");
                if (id == 0x700)
                {
                    val0   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count0 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (id == 0x701)
                {
                    val1   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count1 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (id == 0x702)
                {
                    val2   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count2 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (id == 0x703)
                {
                    val3   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count3 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (count0 == count1 && count0 == count2 && count0 == count3)
                {
                    realtimeChart.DataSample genValue = new realtimeChart.DataSample();
                    genValue.value[0] = val0;
                    genValue.value[1] = val1;
                    genValue.value[2] = val2;
                    genValue.value[3] = val3;
                    mainChart.AddValue(genValue);
                }
            }
            status = Canlib.canBusOff(canHandle);
            CheckStatus(status, "canBusOff");
        }
示例#9
0
        /*
         * Looks for messages and sends them to the output box.
         */
        private void DumpMessageLoop(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            Canlib.canStatus status;
            int id;

            byte[] data = new byte[8];
            int    dlc;
            int    flags;
            long   time;
            bool   noError = true;
            var    msg     = "";

            //Open up a new handle for reading
            readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL);

            status = Canlib.canBusOn(readHandle);

            while (noError && onBus && readHandle >= 0)
            {
                #region 接收指定ID报文
                var message = new byte[8];

                var idRS16 = Convert.ToInt32(idBox.Text, 16);
                var idRS   = Convert.ToInt32(idBox.Text);

                status = Canlib.canReadSpecific(readHandle, idRS16, message, out int dlcRS, out int flagRS, out long timeRS);
                if (status == Canlib.canStatus.canOK)
                {
                    DumpMessage dumpMessage = new DumpMessage();

                    if ((flagRS & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                    {
                        //msgRS = "ERROR FRAME RECEIVED" + Environment.NewLine;
                    }
                    else
                    {
                        dumpMessage.id    = idRS16;
                        dumpMessage.idStr = idBox.Text;
                        dumpMessage.dlc   = dlcRS;
                        dumpMessage.data  = message;
                        dumpMessage.flags = flagRS;
                        dumpMessage.time  = timeRS;
                    }
                    //Sends the message to the ProcessMessage method
                    worker.ReportProgress(1, dumpMessage);
                }
                else if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    //Sends the error status to the ProcessMessage method and breaks the loop
                    worker.ReportProgress(100, status);
                }
                #endregion

                #region 接收所有报文
                //status = Canlib.canReadWait(readHandle, out id, data, out dlc, out flags, out time, 50);
                //if (status == Canlib.canStatus.canOK)
                //{
                //    DumpMessage dumpMessage = new DumpMessage();

                //    if ((flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                //    {
                //        //msg = "***ERROR FRAME RECEIVED***" + Environment.NewLine;
                //    }
                //    else
                //    {
                //        dumpMessage.id = id;
                //        dumpMessage.dlc = dlc;
                //        dumpMessage.data = data;
                //        dumpMessage.flags = flags;
                //        dumpMessage.time = time;
                //    }
                //    //Sends the message to the ProcessMessage method
                //    worker.ReportProgress(0, dumpMessage);
                //}
                //else if (status != Canlib.canStatus.canERR_NOMSG)
                //{
                //    //Sends the error status to the ProcessMessage method and breaks the loop
                //    worker.ReportProgress(100, status);
                //    noError = false;
                //}
                #endregion
            }
            Canlib.canBusOff(readHandle);
        }