예제 #1
0
        /// <summary>
        /// The close method closes the CANUSB device.
        /// </summary>
        /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns>
        override public CloseResult close()
        {
            Console.WriteLine("******* CANUSB: Closing CANUSB");

            int res = 0;

            try
            {
                res = LAWICEL.canusb_Close(m_deviceHandle);
            }
            catch (DllNotFoundException e)
            {
                Console.WriteLine("CANUSBDevice::close: " + e.Message);
                return(CloseResult.CloseError);
            }

            /*if (m_readThread.ThreadState != ThreadState.Unstarted)
             *  m_readThread.Abort();*/

            m_deviceHandle = 0;
            if (LAWICEL.ERROR_CANUSB_OK == res)
            {
                return(CloseResult.OK);
            }
            else
            {
                return(CloseResult.CloseError);
            }
        }
예제 #2
0
        /// <summary>
        /// The close method closes the CANUSB device.
        /// </summary>
        /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns>
        override public CloseResult close()
        {
            Console.WriteLine("Close called in CANUSBDevice");
            if (m_readThread != null)
            {
                if (m_readThread.ThreadState != ThreadState.Stopped && m_readThread.ThreadState != ThreadState.StopRequested)
                {
                    lock (m_synchObject)
                    {
                        m_endThread = true;
                    }
                    // m_readThread.Abort();
                }
            }
            int res = LAWICEL.canusb_Close(m_deviceHandle);

            m_deviceHandle = 0;
            if (LAWICEL.ERROR_CANUSB_OK == res)
            {
                return(CloseResult.OK);
            }
            else
            {
                return(CloseResult.CloseError);
            }
        }
예제 #3
0
        /// <summary>
        /// waitForMessage waits for a specific CAN message give by a CAN id.
        /// </summary>
        /// <param name="a_canID">The CAN id to listen for</param>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
        /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
        private uint waitForMessage(uint a_canID, uint timeout, out LAWICEL.CANMsg r_canMsg)
        {
            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    if (r_canMsg.id == 0x00)
                    {
                        nrOfWait++;
                    }
                    else if (r_canMsg.id != a_canID)
                    {
                        continue;
                    }
                    return((uint)r_canMsg.id);
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                    nrOfWait++;
                }
            }
            r_canMsg = new LAWICEL.CANMsg();
            return(0);
        }
예제 #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()
        {
            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);
        }
예제 #5
0
        /*private void AddToCanTrace(string line)
         * {
         *  if (m_EnableCanLog)
         *  {
         *      DateTime dtnow = DateTime.Now;
         *      using (StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\CanTraceCANUSBDevice.txt", true))
         *      {
         *          sw.WriteLine(dtnow.ToString("dd/MM/yyyy HH:mm:ss") + " - " + line);
         *      }
         *  }
         * }*/
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override public bool sendMessage(CANMessage a_message)
        {
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.flags = LAWICEL.CANMSG_EXTENDED; // Test for now
            msg.data  = a_message.getData();
            int writeResult;

            //AddToCanTrace("Sending message");
            AddToCanTrace("TX: " + msg.id.ToString("X6") + " " + msg.data.ToString("X16"));
            writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == LAWICEL.ERROR_CANUSB_OK)
            {
                //AddToCanTrace("Message sent successfully");
                txCount++;
                bitsPerSecond += 109;
                return(true);
            }
            else
            {
                errCount++;
                switch (writeResult)
                {
                case LAWICEL.ERROR_CANUSB_COMMAND_SUBSYSTEM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM");
                    break;

                case LAWICEL.ERROR_CANUSB_INVALID_PARAM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM");
                    break;

                case LAWICEL.ERROR_CANUSB_NO_MESSAGE:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE");
                    break;

                case LAWICEL.ERROR_CANUSB_NOT_OPEN:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN");
                    break;

                case LAWICEL.ERROR_CANUSB_OPEN_SUBSYSTEM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM");
                    break;

                case LAWICEL.ERROR_CANUSB_TX_FIFO_FULL:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL");
                    break;

                default:
                    AddToCanTrace("Message failed to send: " + writeResult.ToString());
                    break;
                }
                return(false);
            }
        }
예제 #6
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);
        }
예제 #7
0
        public override string getVersion()
        {
            StringBuilder verinfo = new StringBuilder();

            verinfo.Length = 128;
            string retval = string.Empty;

            if (m_deviceHandle > 0)
            {
                LAWICEL.canusb_VersionInfo(m_deviceHandle, verinfo);
                retval = verinfo.ToString();
            }
            return(retval);
        }
예제 #8
0
        /// <summary>
        /// waitAnyMessage waits for any message to be received.
        /// </summary>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message that was first received</param>
        /// <returns>The CAN id for the message received, otherwise 0.</returns>
        private uint waitAnyMessage(uint timeout, out LAWICEL.CANMsg r_canMsg)
        {
            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    return((uint)r_canMsg.id);
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010>
                    nrOfWait++;
                }
            }
            r_canMsg = new LAWICEL.CANMsg();
            return(0);
        }
예제 #9
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);
            }
        }
예제 #10
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);
        }
예제 #11
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            int readResult = 0;

            LAWICEL.CANMsg r_canMsg   = new LAWICEL.CANMsg();
            CANMessage     canMessage = new CANMessage();

            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        return;
                    }
                }
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    canMessage.setID(r_canMsg.id);
                    canMessage.setLength(r_canMsg.len);
                    canMessage.setTimeStamp(r_canMsg.timestamp);
                    canMessage.setFlags(r_canMsg.flags);
                    canMessage.setData(r_canMsg.data);
                    lock (m_listeners)
                    {
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                    }
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                }
            }
        }
예제 #12
0
        /// <summary>
        /// The close method closes the CANUSB device.
        /// </summary>
        /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns>
        override public CloseResult close()
        {
            int res = 0;

            try
            {
                res = LAWICEL.canusb_Close(m_deviceHandle);
            }
            catch (DllNotFoundException e)
            {
                return(CloseResult.CloseError);
            }

            m_deviceHandle = 0;
            if (LAWICEL.ERROR_CANUSB_OK == res)
            {
                return(CloseResult.OK);
            }
            else
            {
                return(CloseResult.CloseError);
            }
        }
예제 #13
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override public bool sendMessage(CANMessage a_message)
        {
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data  = a_message.getData();
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }

            int writeResult;

            writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == LAWICEL.ERROR_CANUSB_OK)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #14
0
 /// <summary>
 /// waitAnyMessage waits for any message to be received.
 /// </summary>
 /// <param name="timeout">Listen timeout</param>
 /// <param name="r_canMsg">The CAN message that was first received</param>
 /// <returns>The CAN id for the message received, otherwise 0.</returns>
 private uint waitAnyMessage(uint timeout, out LAWICEL.CANMsg r_canMsg)
 {
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == LAWICEL.ERROR_CANUSB_OK)
         {
             return (uint)r_canMsg.id;
         }
         else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
             nrOfWait++;
         }
     }
     r_canMsg = new LAWICEL.CANMsg();
     return 0;
 }
예제 #15
0
 /// <summary>
 /// waitForMessage waits for a specific CAN message give by a CAN id.
 /// </summary>
 /// <param name="a_canID">The CAN id to listen for</param>
 /// <param name="timeout">Listen timeout</param>
 /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
 /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
 private uint waitForMessage(uint a_canID, uint timeout, out LAWICEL.CANMsg r_canMsg)
 {
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == LAWICEL.ERROR_CANUSB_OK)
         {
             if (r_canMsg.id != a_canID)
                 continue;
             return (uint)r_canMsg.id;
         }
         else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010>
             nrOfWait++;
         }
     }
     r_canMsg = new LAWICEL.CANMsg();
     return 0;
 }
예제 #16
0
 override public void clearReceiveBuffer()
 {
     LAWICEL.canusb_Flush(m_deviceHandle, (byte)(FlushFlags.FLUSH_DONTWAIT | FlushFlags.FLUSH_EMPTY_INQUEUE));
 }
예제 #17
0
        /// <summary>
        /// waitForMessage waits for a specific CAN message give by a CAN id.
        /// </summary>
        /// <param name="a_canID">The CAN id to listen for</param>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
        /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
        public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg)
        {
            /*
             * int readResult = 0;
             * int nrOfWait = 0;
             * while (nrOfWait < timeout)
             * {
             *  LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg();
             *  canMsg = new CANMessage();
             *  readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
             *  if (readResult == LAWICEL.ERROR_CANUSB_OK)
             *  {
             *      //Console.WriteLine("rx id: 0x" + r_canMsg.id.ToString("X4"));
             *      if (r_canMsg.id != a_canID)
             *      {
             *          nrOfWait++;
             *          continue;
             *      }
             *      else
             *      {
             *          canMsg.setID(r_canMsg.id);
             *          canMsg.setData(r_canMsg.data);
             *          canMsg.setFlags(r_canMsg.flags);
             *          return (uint)r_canMsg.id;
             *      }
             *  }
             *  else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
             *  {
             *      Thread.Sleep(1);
             *      nrOfWait++;
             *  }
             * }
             * canMsg = new CANMessage();
             * return 0;*/
            LAWICEL.CANMsg r_canMsg;
            canMsg = new CANMessage();
            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                r_canMsg   = new LAWICEL.CANMsg();
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    Thread.Sleep(1);
                    AddToCanTrace("rx: 0x" + r_canMsg.id.ToString("X4") + r_canMsg.data.ToString("X16"));
                    if (r_canMsg.id == 0x00)
                    {
                        nrOfWait++;
                    }
                    else if (r_canMsg.id != a_canID)
                    {
                        continue;
                    }
                    canMsg.setData(r_canMsg.data);
                    canMsg.setID(r_canMsg.id);
                    canMsg.setLength(r_canMsg.len);
                    return((uint)r_canMsg.id);
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                    nrOfWait++;
                }
            }
            r_canMsg = new LAWICEL.CANMsg();
            return(0);
        }
예제 #18
0
 override public void clearTransmitBuffer()
 {
     LAWICEL.canusb_Flush(m_deviceHandle, (byte)FlushFlags.FLUSH_DONTWAIT);
 }
예제 #19
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            int readResult = 0;

            LAWICEL.CANMsg r_canMsg   = new LAWICEL.CANMsg();
            CANMessage     canMessage = new CANMessage();

            /* Stopwatch
             * while (true)
             * {
             *   lock (m_synchObject)
             *   {
             *       if (m_endThread)
             *           return;
             *   }
             *   readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
             *   if (readResult > 0)
             *   {
             *       canMessage.setID(r_canMsg.id);
             *       canMessage.setLength(r_canMsg.len);
             *       canMessage.setTimeStamp(r_canMsg.timestamp);
             *       canMessage.setFlags(r_canMsg.flags);
             *       canMessage.setData(r_canMsg.data);
             *       if (m_DoLogging)
             *       {
             *           DumpCanMsg(r_canMsg, false);
             *       }
             *       lock (m_listeners)
             *       {
             *           foreach (ICANListener listener in m_listeners)
             *           {
             *               listener.handleMessage(canMessage);
             *           }
             *       }
             *   }
             *   else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
             *   {
             *       Thread.Sleep(1);
             *   }
             * }*/

            while (true)
            {
                /*if ((thrdcnt++ % 1000) == 0)
                 * {
                 *  Console.WriteLine("Reading messages");
                 * }*/
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult > 0)
                {
                    canMessage.setID(r_canMsg.id);
                    canMessage.setLength(r_canMsg.len);
                    canMessage.setTimeStamp(r_canMsg.timestamp);
                    canMessage.setFlags(r_canMsg.flags);
                    canMessage.setData(r_canMsg.data);
                    if (m_DoLogging)
                    {
                        DumpCanMsg(r_canMsg, false);
                    }
                    lock (m_listeners)
                    {
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                    }
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010>
                }
            }
        }
예제 #20
0
 override public int GetNumberOfAdapters()
 {
     char[] buff = new char[32];
     return(LAWICEL.canusb_getFirstAdapter(buff, 32));
 }
예제 #21
0
        private void DumpCanMsg(LAWICEL.CANMsg r_canMsg, bool IsTransmit)
        {
            DateTime dt = DateTime.Now;
            try
            {
                using (StreamWriter sw = new StreamWriter(Path.Combine(m_startuppath, dt.Year.ToString("D4") + dt.Month.ToString("D2") + dt.Day.ToString("D2") + "-CanTrace.log"), true))
                {
                    if (IsTransmit)
                    {
                        // get the byte transmitted
                        int transmitvalue = (int)(r_canMsg.data & 0x000000000000FF00);
                        transmitvalue /= 256;

                        sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " TX: id=" + r_canMsg.id.ToString("D2") + " len= " + r_canMsg.len.ToString("X8") + " data=" + r_canMsg.data.ToString("X16") + " " + r_canMsg.flags.ToString("X2") + " character = " + GetCharString(transmitvalue) + "\t ts: " + r_canMsg.timestamp.ToString("X16") + " flags: " + r_canMsg.flags.ToString("X2"));
                    }
                    else
                    {
                        // get the byte received
                        int receivevalue = (int)(r_canMsg.data & 0x0000000000FF0000);
                        receivevalue /= (256 * 256);
                        sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " RX: id=" + r_canMsg.id.ToString("D2") + " len= " + r_canMsg.len.ToString("X8") + " data=" + r_canMsg.data.ToString("X16") + " " + r_canMsg.flags.ToString("X2") + " character = " + GetCharString(receivevalue) + "\t ts: " + r_canMsg.timestamp.ToString("X16") + " flags: " + r_canMsg.flags.ToString("X2"));
                    }
                }
            }
            catch (Exception E)
            {
                Console.WriteLine("Failed to write to logfile: " + E.Message);
            }
        }
예제 #22
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            int readResult = 0;

            LAWICEL.CANMsg r_canMsg   = new LAWICEL.CANMsg();
            CANMessage     canMessage = new CANMessage();

            Console.WriteLine("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        Console.WriteLine("readMessages ended");
                        return;
                    }
                }
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    //Console.WriteLine(r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16"));
                    //if (MessageContainsInformationForRealtime(r_canMsg.id))
                    {
                        canMessage.setID(r_canMsg.id);
                        canMessage.setLength(r_canMsg.len);
                        canMessage.setTimeStamp(r_canMsg.timestamp);
                        canMessage.setFlags(r_canMsg.flags);
                        canMessage.setData(r_canMsg.data);
                        lock (m_listeners)
                        {
                            bitsPerSecond += 109;
                            AddToCanTrace("RX: " + r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16"));
                            rxCount++;
                            foreach (ICANListener listener in m_listeners)
                            {
                                //while (listener.messagePending()) ; // dirty, make this better
                                listener.handleMessage(canMessage);
                            }
                            CastInformationEvent("", rxCount, txCount, errCount); // <GS-05042011> re-activated this function
                        }
                        //Thread.Sleep(1);
                    }

                    // cast event to application to process message
                    //if (MessageContainsInformationForRealtime(r_canMsg.id))
                    //{
                    //TODO: process all other known msg id's into the realtime view
                    //  CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                    //}
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    // Console.WriteLine("No message");
                    Thread.Sleep(1);
                }
                else
                {
                    Console.WriteLine("Result: " + readResult.ToString("X8"));
                }

                /*int stat = LAWICEL.canusb_Status(m_deviceHandle);
                 * if (stat != 0)
                 * {
                 *  Console.WriteLine("status: " + stat.ToString("X4"));
                 * }*/
            }
        }
예제 #23
0
        /// <summary>
        /// waitForMessage waits for a specific CAN message give by a CAN id.
        /// </summary>
        /// <param name="a_canID">The CAN id to listen for</param>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
        /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
        private uint waitForMessage(uint a_canID, uint timeout, out LAWICEL.CANMsg r_canMsg)
        {
            CANMessage canMessage = new CANMessage();

            string line = string.Empty;
            int readResult = 0;
            int nrOfWait = 0;
            while (nrOfWait < timeout)
            {
                m_port.Write("\r");
                m_port.Write("P\r");
                bool endofFrames = false;
                while (!endofFrames)
                {
                    Console.WriteLine("reading line");
                    line = m_port.ReadLine();
                    Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                    {
                        endofFrames = true;
                    }
                    else
                    {

                        if (line.Length == 14)
                        {
                            // three bytes identifier
                            r_canMsg = new LAWICEL.CANMsg();
                            r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16);
                            r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16);
                            ulong data = 0;
                            // add all the bytes
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16) << 7 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 6 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 5 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 4 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 3 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 2 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 1 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16);
                            r_canMsg.data = data;
                            canMessage.setID(r_canMsg.id);
                            canMessage.setLength(r_canMsg.len);
                            //canMessage.setTimeStamp(0);
                            canMessage.setFlags(0);
                            canMessage.setData(r_canMsg.data);
                            if (m_DoLogging)
                            {
                                DumpCanMsg(r_canMsg, false);
                            }
                            if (r_canMsg.id != a_canID)
                                continue;
                            return (uint)r_canMsg.id;

                        }
                    }

                }
                Thread.Sleep(1);
                nrOfWait++;
            }
            r_canMsg = new LAWICEL.CANMsg();
            return 0;
        }
예제 #24
0
 public override void Flush()
 {
     LAWICEL.canusb_Flush(m_deviceHandle, 0x01);
     LAWICEL.canusb_Flush(m_deviceHandle, 0x03);
 }