Exemplo n.º 1
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);
                }
            }
        }
Exemplo n.º 2
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()
        {
            byte[] msg = new byte[8];
            int    dlc;
            int    flag, id;
            long   time;

            Canlib.canStatus status;
            CANMessage       canMessage = new CANMessage();

            logger.Debug("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        logger.Debug("readMessages thread ended");
                        return;
                    }
                }
                status = Canlib.canReadWait(handleRead, out id, msg, out dlc, out flag, out time, 250);
                if ((flag & Canlib.canMSG_ERROR_FRAME) == 0)
                {
                    if (status == Canlib.canStatus.canOK)
                    {
                        canMessage.setID((uint)id);
                        canMessage.setTimeStamp((uint)time);
                        canMessage.setFlags((byte)flag);
                        canMessage.setCanData(msg, (byte)dlc);

                        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
                        }
                    }
                    else if (status == Canlib.canStatus.canERR_NOMSG)
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {
                    logger.Debug("error frame");
                }
            }
        }
Exemplo 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);
            }
            }
        }