/// <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 EASYSYNC.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.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 == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new EASYSYNC.CANMsg(); return(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> public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { EASYSYNC.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new EASYSYNC.CANMsg(); readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.ERROR_CANUSB_OK) { if (r_canMsg.id == 0x00) { nrOfWait++; } else if (r_canMsg.id != a_canID) { continue; } canMsg.setData(r_canMsg.data); canMsg.setID(r_canMsg.id); canMsg.setLength(r_canMsg.len); return((uint)r_canMsg.id); } else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new EASYSYNC.CANMsg(); 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 EASYSYNC.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.ERROR_CANUSB_OK) { return((uint)r_canMsg.id); } else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact nrOfWait++; } } r_canMsg = new EASYSYNC.CANMsg(); return(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(); while (true) { lock (m_synchObject) { if (m_endThread) { return; } } readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.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 == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } } }
/// <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 } } }