/// <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);
        }
Пример #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");
                }
            }
        }
Пример #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)
        {
            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;
        }
Пример #4
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);
            }
        }
Пример #5
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>
                }
            }
        }
Пример #6
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);
            }
        }
Пример #7
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
                }
            }
        }