Esempio n. 1
0
        //-------------------------------------------------------------------------

        /**
         *  Waits for any message to be received.
         *
         *  @param      timeout     timeout
         *  @param      msg         message
         *
         *  @return                 message ID if received, 0 otherwise
         */
        private uint wait_message(uint timeout, out CANMessage msg)
        {
            msg = new CANMessage();
            Debug.Assert(msg != null);

            int   wait_cnt = 0;
            uint  id;
            byte  length;
            ulong data;

            while (wait_cnt < timeout)
            {
                if (MctAdapter_ReceiveMessage(out id, out length, out data))
                {
                    // message received
                    msg.setID(id);
                    msg.setLength(length);
                    msg.setData(data);

                    return(id);
                }

                // wait a bit
                Thread.Sleep(1);
                ++wait_cnt;
            }

            // nothing was received
            return(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)
        {
            CANMessage canMessage = new CANMessage();

            string line = string.Empty;

            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(r_canMsg.timestamp);
                            canMessage.setFlags(0);
                            canMessage.setData(r_canMsg.data);
                            if (m_DoLogging)
                            {
                                DumpCanMsg(r_canMsg, false);
                            }
                            return((uint)r_canMsg.id);
                        }
                    }
                }
                Thread.Sleep(1);
                nrOfWait++;
            }
            r_canMsg = new LAWICEL.CANMsg();
            return(0);
        }
Esempio n. 3
0
        //-------------------------------------------------------------------------

        /**
         *  Handles incoming messages.
         */
        private void readMessages()
        {
            CANMessage canMessage = new CANMessage();
            string     rxMessage  = string.Empty;

            Console.WriteLine("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        Console.WriteLine("readMessages ended");
                        return;
                    }
                }

                try
                {
                    if (m_serialPort.IsOpen)
                    {
                        do
                        {
                            rxMessage = m_serialPort.ReadLine();
                            rxMessage = rxMessage.Replace("\r", ""); // remove prompt characters... we don't need that stuff
                            rxMessage = rxMessage.Replace("\n", ""); // remove prompt characters... we don't need that stuff
                        } while (rxMessage.StartsWith("w") == false);

                        uint id = Convert.ToUInt32(rxMessage.Substring(1, 3), 16);
                        canMessage.setID(id);
                        canMessage.setLength(8);
                        canMessage.setData(0x0000000000000000);
                        for (uint i = 0; i < 8; i++)
                        {
                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(5 + (2 * (int)i), 2), 16), i);
                        }

                        lock (m_listeners)
                        {
                            //AddToCanTrace("RX: " + canMessage.getID().ToString("X3") + " " + canMessage.getLength().ToString("X1") + " " + canMessage.getData().ToString("X16"));
                            //Console.WriteLine("MSG: " + rxMessage);
                            foreach (ICANListener listener in m_listeners)
                            {
                                listener.handleMessage(canMessage);
                            }
                            //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                        }
                        // give up CPU for a moment
                        Thread.Sleep(1);
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("MSG: " + rxMessage);
                }
            }
        }
Esempio n. 4
0
        //-------------------------------------------------------------------------

        /**
         *  Handles incoming messages.
         */
        private void read_messages()
        {
            uint  id;
            byte  length;
            ulong data;

            CANMessage msg = new CANMessage();

            Debug.Assert(msg != null);

            // main loop
            while (true)
            {
                // check tor thread termination request
                Debug.Assert(this.term_mutex != null);
                lock (this.term_mutex)
                {
                    if (this.term_requested)
                    {
                        return;
                    }
                }

                // receive messages
                while (MctAdapter_ReceiveMessage(out id, out length, out data))
                {
                    // convert message
                    msg.setID(id);
                    msg.setLength(length);
                    msg.setData(data);

                    // pass message to listeners
                    lock (this.m_listeners)
                    {
                        foreach (ICANListener listener in this.m_listeners)
                        {
                            listener.handleMessage(msg);
                        }
                    }
                }

                // give up CPU for a moment
                Thread.Sleep(1);
            }
        }
        //-------------------------------------------------------------------------
        /**
        Handles incoming messages.
        */
        private void readMessages()
        {
            CANMessage canMessage = new CANMessage();
            string rxMessage = string.Empty;

            Console.WriteLine("readMessages started");
            while (true)
            {
            lock (m_synchObject)
            {
                if (m_endThread)
                {
                    Console.WriteLine("readMessages ended");
                    return;
                }
            }

            try
            {
                if (m_serialPort.IsOpen)
                {
                    do
                    {
                        rxMessage = m_serialPort.ReadLine();
                        rxMessage = rxMessage.Replace("\r", ""); // remove prompt characters... we don't need that stuff
                        rxMessage = rxMessage.Replace("\n", ""); // remove prompt characters... we don't need that stuff
                    } while (rxMessage.StartsWith("w") == false);

                    uint id = Convert.ToUInt32(rxMessage.Substring(1, 3), 16);
                    canMessage.setID(id);
                    canMessage.setLength(8);
                    canMessage.setData(0x0000000000000000);
                    for (uint i = 0; i < 8; i++)
                        canMessage.setCanData(Convert.ToByte(rxMessage.Substring(5 + (2 * (int)i), 2), 16), i);

                    lock (m_listeners)
                    {
                        //AddToCanTrace("RX: " + canMessage.getID().ToString("X3") + " " + canMessage.getLength().ToString("X1") + " " + canMessage.getData().ToString("X16"));
                        //Console.WriteLine("MSG: " + rxMessage);
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                        //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                    }
                    // give up CPU for a moment
                    Thread.Sleep(1);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("MSG: " + rxMessage);
            }
            }
        }
Esempio n. 6
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;
        }
Esempio n. 7
0
        // int thrdcnt = 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)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                if (m_port.IsOpen)
                {
                    // read the status?
                    string line = string.Empty;
                    m_port.Write("\r");
                    m_port.Write("A\r"); // poll for all pending CAN messages
                    //Console.WriteLine("Polled for frames");
                    Thread.Sleep(0);
                    bool endofFrames = false;
                    while(!endofFrames)
                    {

                        try
                        {
                            if (m_port.BytesToRead > 0)
                            {
                                line = m_port.ReadLine();
                                Console.Write("RX: ");
                                for (int tel = 0; tel < line.Length; tel++)
                                {
                                    byte b = (byte)line[tel];
                                    Console.Write(b.ToString("X2") + " " );
                                }
                                Console.WriteLine("") ;
                                Thread.Sleep(0);
                                if (line.Length > 0)
                                {
                                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                                    {
                                        //Console.WriteLine("End of messages");
                                        endofFrames = true;
                                    }
                                    else
                                    {
                                        //t00C8C6003E0000000000
                                        //Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                                        if (line.Length == 21)
                                        {
                                            // three bytes identifier
                                            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);
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 1 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 2 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 3 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 4 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 5 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 6 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16) << 7 * 8;
                                            r_canMsg.data = data;
                                            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);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception E)
                        {
                            //Console.WriteLine("Failed to read frames from CANbus: " + E.Message);
                        }
                        Thread.Sleep(0);

                    }
                }
                Thread.Sleep(1);
            }
        }
Esempio n. 8
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>
                }
            }
        }
Esempio n. 9
0
        // int thrdcnt = 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)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                if (m_port.IsOpen)
                {
                    // read the status?
                    string line = string.Empty;
                    m_port.Write("\r");
                    m_port.Write("A\r"); // poll for all pending CAN messages
                    //Console.WriteLine("Polled for frames");
                    Thread.Sleep(0);
                    bool endofFrames = false;
                    while (!endofFrames)
                    {
                        try
                        {
                            if (m_port.BytesToRead > 0)
                            {
                                line = m_port.ReadLine();
                                Console.Write("RX: ");
                                for (int tel = 0; tel < line.Length; tel++)
                                {
                                    byte b = (byte)line[tel];
                                    Console.Write(b.ToString("X2") + " ");
                                }
                                Console.WriteLine("");
                                Thread.Sleep(0);
                                if (line.Length > 0)
                                {
                                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                                    {
                                        //Console.WriteLine("End of messages");
                                        endofFrames = true;
                                    }
                                    else
                                    {
                                        //t00C8C6003E0000000000
                                        //Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                                        if (line.Length == 21)
                                        {
                                            // three bytes identifier
                                            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);
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 1 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 2 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 3 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 4 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 5 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 6 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16) << 7 * 8;
                                            r_canMsg.data = data;
                                            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);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception E)
                        {
                            //Console.WriteLine("Failed to read frames from CANbus: " + E.Message);
                        }
                        Thread.Sleep(0);
                    }
                }
                Thread.Sleep(1);
            }
        }
Esempio n. 10
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;

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

            /* Stopwatch
             * while (true)
             * {
             *   lock (m_synchObject)
             *   {
             *       if (m_endThread)
             *           return;
             *   }
             *   readResult = EASYSYNC.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 == EASYSYNC.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 = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult > 0)
                {
                    canMessage.setID((uint)r_canMsg.id);
                    canMessage.setLength(r_canMsg.len);
                    canMessage.setTimeStamp((uint)r_canMsg.timestamp);
                    canMessage.setFlags(r_canMsg.flags);
                    //TODO: data hier nog vullen 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 == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact
                }
            }
        }
Esempio n. 11
0
        //-------------------------------------------------------------------------
        /**
        Waits for any message to be received.

        @param      timeout     timeout
        @param      msg         message

        @return                 message ID if received, 0 otherwise
        */
        private uint wait_message(uint timeout, out CANMessage msg)
        {
            msg = new CANMessage();
            Debug.Assert(msg != null);

            int wait_cnt = 0;
            uint id;
            byte length;
            ulong data;
            while (wait_cnt < timeout)
            {
            if (MctAdapter_ReceiveMessage(out id, out length, out data))
            {
                // message received
                msg.setID(id);
                msg.setLength(length);
                msg.setData(data);

                return id;
            }

            // wait a bit
            Thread.Sleep(1);
            ++wait_cnt;
            }

            // nothing was received
            return 0;
        }
Esempio n. 12
0
        //-------------------------------------------------------------------------
        /**
        Handles incoming messages.
        */
        private void read_messages()
        {
            uint id;
            byte length;
            ulong data;

            CANMessage msg = new CANMessage();
            Debug.Assert(msg != null);

            // main loop
            while (true)
            {
            // check tor thread termination request
            Debug.Assert(this.term_mutex != null);
            lock (this.term_mutex)
            {
                if (this.term_requested)
                {
                    return;
                }
            }

            // receive messages
            while (MctAdapter_ReceiveMessage(out id, out length, out data))
            {
                // convert message
                msg.setID(id);
                msg.setLength(length);
                msg.setData(data);

                // pass message to listeners
                lock (this.m_listeners)
                {
                    foreach (ICANListener listener in this.m_listeners)
                    {
                        listener.handleMessage(msg);
                    }
                }
            }

            // give up CPU for a moment
            Thread.Sleep(1);
            }
        }