Esempio n. 1
0
 private void OpenChannel(out int hnd, int bitrate)
 {
     logger.Debug("hnd = canlibCLSNET.Canlib.canOpenChannel()");
     hnd = Canlib.canOpenChannel(ChannelNumber, 0);
     logger.Debug("canlibCLSNET.Canlib.canSetBusParams(hnd)");
     Canlib.canStatus statusSetParam = Canlib.canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
     logger.Debug("canlibCLSNET.Canlib.canBusOn(hnd)");
     Canlib.canStatus statusOn = Canlib.canBusOn(hnd);
     Canlib.canIoCtl(hnd, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
 }
Esempio n. 2
0
 private void OpenChannelWithParamsC200(out int hnd, byte btr0, byte btr1)
 {
     logger.Debug("hnd = canlibCLSNET.Canlib.canOpenChannel()");
     hnd = Canlib.canOpenChannel(ChannelNumber, 0);
     logger.Debug("canlibCLSNET.Canlib.canSetBusParams(hnd)");
     Canlib.canStatus statusSetParam = Canlib.canSetBusParamsC200(hnd, btr0, btr1);
     logger.Debug("canlibCLSNET.Canlib.canBusOn(hnd)");
     Canlib.canStatus statusOn = Canlib.canBusOn(hnd);
     Canlib.canIoCtl(hnd, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
 }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
        /*
         * Looks for messages and sends them to the output box.
         */
        void Receiver_MessageHandlerLoop(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            Canlib.canStatus status  = Canlib.canStatus.canERR_NOMSG;
            bool             noError = true;
            int errorFrameCount      = 0;

            //Create a Windows event handle
            Object winHandle = new IntPtr(-1);

            status = Canlib.canIoCtl(readHandle, Canlib.canIOCTL_GET_EVENTHANDLE, ref winHandle);
            if (status != Canlib.canStatus.canOK)
            {
                worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status);
                noError = false;
            }
            status = Canlib.canAccept(readHandle, 0x610, Canlib.canFILTER_SET_CODE_STD);
            if (status != Canlib.canStatus.canOK)
            {
                worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status);
                noError = false;
            }
            status = Canlib.canAccept(readHandle, 0xFFE, Canlib.canFILTER_SET_MASK_STD);
            if (status != Canlib.canStatus.canOK)
            {
                worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status);
                noError = false;
            }

            WaitHandle waitHandle = (WaitHandle) new CanlibWaitEvent(winHandle);

            while (noError && isReaderBusOn && readHandle >= 0)
            {
                //Wait for 25ms for an event to occur on the channel (BBMS CAN communication period: 25ms)
                bool eventHappened = waitHandle.WaitOne(25);
                if (!eventHappened)
                {
                    continue;
                }

                int    id;
                byte[] data = new byte[Defined.MAX_DLC];
                int    dlc;
                int    flags;
                long   time;
                string msgLog;

                //To attain the initial msg
                status = Canlib.canRead(readHandle, out id, data, out dlc, out flags, out time);

                //This while loop is to repeat canRead() until the msg boxes for readHandle becomes exhausted.
                while (status == Canlib.canStatus.canOK)
                {
                    if ((flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                    {
                        msgLog = "***ERROR FRAME RECEIVED*** Count = " + (++errorFrameCount);
                        if (errorFrameCount > Defined.ERROR_FRAME_ENDURANCE_COUNT_LIMIT)
                        {
                            worker.ReportProgress((int)ReceiverReport.REPORT_RUNNING, "Too many error frames received. Bus off.");
                            break;
                        }
                    }
                    else
                    {
                        msgLog = String.Format("0x{0:x3}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   @{10}",
                                               id, dlc, data[0], data[1], data[2], data[3], data[4],
                                               data[5], data[6], data[7], time);
                    }

                    //Sends the message to the ProcessMessage method
                    worker.ReportProgress((int)ReceiverReport.REPORT_RUNNING, "[Rx] " + msgLog);

                    //Result status of the following canRead() will finish the while loop if status!=Canlib.canStatus.canOK
                    status = Canlib.canRead(readHandle, out id, data, out dlc, out flags, out time);
                }

                //If the reason to exit the while loop of canRead() was not canERR_NOMSG, an error is expected.
                if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    //Sends the error status to the ProcessMessage method and breaks the loop
                    worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status);
                    noError = false;
                }
            }
            if (isReaderBusOn)
            {
                worker.ReportProgress((int)ReceiverReport.FORCED_STOP, "Force Bus Off");
            }
        }