예제 #1
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()
        {
            Console.WriteLine("******* CANUSB: Opening CANUSB");
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            //Check if I bus is connected
            if (m_deviceHandle != 0)
            {
                close();
            }
            Thread.Sleep(200);
            if (!_useOnlyPBus)
            {
                Console.WriteLine("Getting handle");
                m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero,
                                                     "0xcb:0x9a", // Slow
                                                     LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL,
                                                     LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL,
                                                     LAWICEL.CANUSB_FLAG_TIMESTAMP);
                if (m_deviceHandle != 0)
                {
                    if (waitAnyMessage(1000, out msg) != 0)
                    {
                        if (m_readThread.ThreadState == ThreadState.Unstarted)
                        {
                            m_readThread.Start();
                        }
                        return(OpenResult.OK);
                    }
                }
                if (m_deviceHandle != 0)
                {
                    close();
                }
                Thread.Sleep(200);
            }
            if (m_deviceHandle != 0)
            {
                close();
            }

            //I bus wasn't connected.
            //Check if P bus is connected
            m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero,
                                                 LAWICEL.CAN_BAUD_500K, //500Kb/s
                                                 LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL,
                                                 LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL,
                                                 LAWICEL.CANUSB_FLAG_TIMESTAMP);
            if (boxIsThere())
            {
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }

            close();
            return(OpenResult.OpenError);
        }
예제 #2
0
        /*private void CastInformationEvent(CANMessage message)
         * {
         *  if (onReceivedAdditionalInformationFrame != null)
         *  {
         *      onReceivedAdditionalInformationFrame(this, new InformationFrameEventArgs(message));
         *  }
         * }
         *
         * private bool MessageContainsInformationForRealtime(uint msgId)
         * {
         *  bool retval = false;
         *  switch (msgId)
         *  {
         *      case 0x1A0:         //1A0h - Engine information
         *      case 0x280:         //280h - Pedals, reverse gear
         *      case 0x290:         //290h - Steering wheel and SID buttons
         *      case 0x2F0:         //2F0h - Vehicle speed
         *      case 0x320:         //320h - Doors, central locking and seat belts
         *      case 0x370:         //370h - Mileage
         *      case 0x3A0:         //3A0h - Vehicle speed
         *      case 0x3B0:         //3B0h - Head lights
         *      case 0x3E0:         //3E0h - Automatic Gearbox
         *      case 0x410:         //410h - Light dimmer and light sensor
         *      case 0x430:         //430h - SID beep request (interesting for Knock indicator?)
         *      case 0x460:         //460h - Engine rpm and speed
         *      case 0x4A0:         //4A0h - Steering wheel, Vehicle Identification Number
         *      case 0x520:         //520h - ACC, inside temperature
         *      case 0x530:         //530h - ACC
         *      case 0x5C0:         //5C0h - Coolant temperature, air pressure
         *      case 0x630:         //630h - Fuel usage
         *      case 0x640:         //640h - Mileage
         *      case 0x7A0:         //7A0h - Outside temperature
         *          retval = true;
         *          break;
         *  }
         *  return retval;
         * }*/

        /// <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(bool is500KB)
        {
            Console.WriteLine("******* CANUSB: Opening CANUSB");
            rxCount  = 0;
            txCount  = 0;
            errCount = 0;

            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            //Check if I bus is connected
            if (m_deviceHandle != 0)
            {
                close();
            }
            Thread.Sleep(200);
            m_readThread = new Thread(readMessages);

            if (m_deviceHandle != 0)
            {
                close();
            }
            m_endThread = false;
            string speed = "250";

            if (is500KB)
            {
                speed = "500";
            }
            Console.WriteLine(speed);
            m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero,
                                                 speed,
                                                 LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL,
                                                 LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL,
                                                 LAWICEL.CANUSB_FLAG_TIMESTAMP);
            Console.WriteLine("Checking box presence");
            Console.WriteLine("Handle: " + m_deviceHandle.ToString("X8"));
            if (m_deviceHandle == 0x00000000)
            {
                return(OpenResult.OpenError);
            }
            if (boxIsThere())
            {
                Console.WriteLine("Box is there, starting thread");
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }
            Console.WriteLine("Box not there");
            close();
            return(OpenResult.OpenError);
        }
예제 #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()
        {
            if (m_deviceHandle != 0)
            {
                close();
            }

            m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero,
                                                 "0x40:0x37",
                                                 LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL,
                                                 LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL,
                                                 LAWICEL.CANUSB_FLAG_TIMESTAMP);

            if (m_deviceHandle > 0)
            {
                LAWICEL.canusb_Flush(m_deviceHandle, 0x01);
                Console.WriteLine("Creating new reader thread");
                m_readThread = new Thread(readMessages);
                try
                {
                    m_readThread.Priority = ThreadPriority.Normal; // realtime enough
                }
                catch (Exception E)
                {
                    Console.WriteLine(E.Message);
                }
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }
            else
            {
                // second try after unload of dlls?

                //close();
                return(OpenResult.OpenError);
            }
        }
예제 #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()
        {
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            //Check if I bus is connected
            close();
            m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero,
                                                 "0xcb:0x9a",
                                                 LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL,
                                                 LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL,
                                                 LAWICEL.CANUSB_FLAG_TIMESTAMP);
            if (waitAnyMessage(1000, out msg) != 0)
            {
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }
            close();

            //I bus wasn't connected.
            //Check if P bus is connected
            m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero,
                                                 LAWICEL.CAN_BAUD_500K,
                                                 LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL,
                                                 LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL,
                                                 LAWICEL.CANUSB_FLAG_TIMESTAMP);
            if (boxIsThere())
            {
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }

            close();
            return(OpenResult.OpenError);
        }