/*        public CANMessage waitForMessage(uint a_canID, int a_timeout)
 *      {
 *          CANMessage retMsg;
 *          m_canMessage.setID(0);  // init so we cannot receive the same frame twice <GS-10022010>
 *          lock (m_canMessage)
 *          {
 *              m_waitMsgID = a_canID;
 *          }
 *          m_resetEvent.WaitOne(a_timeout, true);
 *          lock (m_canMessage)
 *          {
 *              retMsg = m_canMessage;
 *          }
 *
 *          return retMsg;
 *      }
 */
        override public void handleMessage(CANMessage a_message)
        {
            lock (m_canMessage)
            {
                if (a_message.getID() == m_waitMsgID)
                {
                    m_canMessage.setData(a_message.getData());
                    m_canMessage.setFlags(a_message.getFlags());
                    m_canMessage.setID(a_message.getID());
                    m_canMessage.setLength(a_message.getLength());
                    m_canMessage.setTimeStamp(a_message.getTimeStamp());
                    m_resetEvent.Set();
                }
            }
        }
        /// <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);
                }
            }
        }
Beispiel #3
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"));
                 * }*/
            }
        }