Пример #1
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.CANUSB.CANMsg r_canMsg)
 {
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == Lawicel.CANUSB.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.CANUSB.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
             nrOfWait++;
         }
     }
     r_canMsg = new Lawicel.CANUSB.CANMsg();
     return 0;
 }
Пример #2
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.CANUSB.CANMsg r_canMsg)
        {
            CANMessage canMessage = new CANMessage();

            string line = string.Empty;

            int readResult = 0;
            int nrOfWait = 0;
            while (nrOfWait < timeout)
            {
                m_serialPort.Write("\r");
                m_serialPort.Write("P\r");
                bool endofFrames = false;
                while (!endofFrames)
                {
                    Console.WriteLine("reading line");
                    line = m_serialPort.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.CANUSB.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.setFlags(0);
                            canMessage.setData(r_canMsg.data);

                            return (uint)r_canMsg.id;

                        }
                    }

                }
                //Thread.Sleep(0);
                nrOfWait++;
            }
            r_canMsg = new Lawicel.CANUSB.CANMsg();
            return 0;
        }