/* public CANMessage waitForMessage(uint a_canID, int a_timeout) * { * CANMessage retMsg; * m_canMessage.setID(0); // init so we cannot receive the same frame twice <GS-10022010> * lock (m_canMessage) * { * m_waitMsgID = a_canID; * } * m_resetEvent.WaitOne(a_timeout, true); * lock (m_canMessage) * { * retMsg = m_canMessage; * } * * return retMsg; * } */ override public void handleMessage(CANMessage a_message) { lock (m_canMessage) { if (a_message.getID() == m_waitMsgID) { m_canMessage.setData(a_message.getData()); m_canMessage.setFlags(a_message.getFlags()); m_canMessage.setID(a_message.getID()); m_canMessage.setLength(a_message.getLength()); m_canMessage.setTimeStamp(a_message.getTimeStamp()); m_resetEvent.Set(); } } }
/// <summary> /// Send an acknowledgement message. /// </summary> /// <param name="a_rowNr">The row number that should be acknowledged.</param> private void sendAck(uint a_rowNr) { CANMessage msg = new CANMessage(0x266, 0, 5); uint i = 0; ulong data = 0; data = setCanData(data, (byte)0x40, i++); data = setCanData(data, (byte)0xA1, i++); data = setCanData(data, (byte)0x3F, i++); data = setCanData(data, (byte)(0x80 | (int)(a_rowNr)), i++); msg.setData(data); if (!m_canDevice.sendMessage(msg)) { throw new Exception("Error sending ack"); } }
/// <summary> /// Start a KWP session. /// </summary> /// <remarks> /// A KWP session must be started before any requests can be sent. /// </remarks> /// <returns>True if the session was started, otherwise false.</returns> public bool startSession() { CANMessage msg = new CANMessage(0x220, 0, 8); msg.setData(0x000040021100813F); AddToCanTrace("Sending 0x000040021100813F message"); m_kwpCanListener.setupWaitMessage(0x238); if (!m_canDevice.sendMessage(msg)) { AddToCanTrace("Unable to send 0x000040021100813F message"); return(false); } Console.WriteLine("Init msg sent"); if (m_kwpCanListener.waitMessage(timeoutPeriod).getID() == 0x238) { AddToCanTrace("Successfully sent 0x000040021100813F message and received reply 0x238"); return(true); } else { AddToCanTrace("Didn't receive 0x238 message as reply on 0x000040021100813F message"); return(false); } /* if (!m_canDevice.sendMessage(msg)) * { * AddToCanTrace("Unable to send 0x000040021100813F message"); * return false; * } * Console.WriteLine("Init msg sent"); * if (m_kwpCanListener.waitForMessage(0x238, timeoutPeriod).getID() == 0x238) * { * AddToCanTrace("Successfully sent 0x000040021100813F message and received reply 0x238"); * return true; * } * else * { * AddToCanTrace("Didn't receive 0x238 message as reply on 0x000040021100813F message"); * return false; * } */ }
//------------------------------------------------------------------------- /** * Waits for arrival of a specific CAN message or any message if ID = 0. * * @param a_canID message ID * @param timeout timeout, ms * @param canMsg message * * @return message ID */ public uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { canMsg = new CANMessage(); Debug.Assert(canMsg != null); canMsg.setID(0); caCombiAdapter.caCANFrame frame = new caCombiAdapter.caCANFrame(); if (this.combi.CAN_GetMessage(ref frame, timeout) && (frame.id == a_canID || a_canID == 0)) { // message received canMsg.setID(frame.id); canMsg.setLength(frame.length); canMsg.setData(frame.data); return(frame.id); } // timed out return(0); }
/// <summary> /// Send a message that starts a session. This is used to test if there is /// a connection. /// </summary> /// <returns></returns> private bool sendSessionRequest() { CANMessage msg1 = new CANMessage(0x220, 0, 8); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); msg1.setData(0x000040021100813f); if (!sendMessage(msg1)) { return(false); } if (waitForMessage(0x238, 1000, out msg) == 0x238) { //Ok, there seems to be a ECU somewhere out there. //Now, sleep for 10 seconds to get a session timeout. This is needed for //applications on higher level. Otherwise there will be no reply when the //higher level application tries to start a session. Thread.Sleep(10000); return(true); } return(false); }
/// <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) { return; } } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.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 == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } } }
/// <summary> /// Send a KWP request. /// </summary> /// <param name="a_request">A KWP request.</param> /// <param name="r_reply">A KWP reply.</param> /// <returns>The status of the request.</returns> public RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply) { CANMessage msg = new CANMessage(0x240, 0, 8); uint row = nrOfRowsToSend(a_request.getData()); m_kwpCanListener.setupWaitMessage(0x258); // Send one or several request messages. for (; row > 0; row--) { msg.setData(createCanMessage(a_request.getData(), row - 1)); if (!m_canDevice.sendMessage(msg)) { r_reply = new KWPReply(); return(RequestResult.ErrorSending); } } msg = m_kwpCanListener.waitMessage(timeoutPeriod); // msg = m_kwpCanListener.waitForMessage(0x258, timeoutPeriod); // Receive one or several replys and send an ack for each reply. if (msg.getID() == 0x258) { uint nrOfRows = (uint)(msg.getCanData(0) & 0x3F) + 1; row = 0; if (nrOfRows == 0) { throw new Exception("Wrong nr of rows"); } //Assume that no KWP reply contains more than 0x200 bytes byte[] reply = new byte[0x200]; reply = collectReply(reply, msg.getData(), row); sendAck(nrOfRows - 1); nrOfRows--; m_kwpCanListener.setupWaitMessage(0x258); while (nrOfRows > 0) { // msg = m_kwpCanListener.waitForMessage(0x258, timeoutPeriod); msg = m_kwpCanListener.waitMessage(timeoutPeriod); if (msg.getID() == 0x258) { row++; reply = collectReply(reply, msg.getData(), row); sendAck(nrOfRows - 1); nrOfRows--; } else { r_reply = new KWPReply(); return(RequestResult.Timeout); } } r_reply = new KWPReply(reply, a_request.getNrOfPID()); return(RequestResult.NoError); } else { r_reply = new KWPReply(); return(RequestResult.Timeout); } }
/// <summary> /// Send a KWP request. /// </summary> /// <param name="a_request">A KWP request.</param> /// <param name="r_reply">A KWP reply.</param> /// <returns>The status of the request.</returns> public override RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply) { uint row; uint all_rows = row = nrOfRowsToSend(a_request.getData()); m_kwpCanListener.setupWaitMessage(0x258); // Send one or several request messages. for (; row > 0; row--) { CANMessage msg = new CANMessage(0x240, 0, 8); msg.elmExpectedResponses = a_request.ElmExpectedResponses; msg.setData(createCanMessage(a_request.getData(), row - 1)); if ((msg.getData() & 0xFFFFUL) == 0xA141UL) { msg.elmExpectedResponses = 0; } if (all_rows == 22) { msg.elmExpectedResponses = row == 1 ? 1 : 0; // on last message (expect 1 reply) } if (!m_canDevice.sendMessage(msg)) { r_reply = new KWPReply(); return(RequestResult.ErrorSending); } } var response = m_kwpCanListener.waitMessage(getTimeout()); // Receive one or several replys and send an ack for each reply. if (response.getID() == 0x258) { uint nrOfRows = (uint)(response.getCanData(0) & 0x3F) + 1; row = 0; if (nrOfRows == 0) { throw new Exception("Wrong nr of rows"); } //Assume that no KWP reply contains more than 0x200 bytes byte[] reply = new byte[0x200]; reply = collectReply(reply, response.getData(), row); sendAck(nrOfRows - 1); nrOfRows--; m_kwpCanListener.setupWaitMessage(0x258); while (nrOfRows > 0) { response = m_kwpCanListener.waitMessage(getTimeout()); if (response.getID() == 0x258) { row++; reply = collectReply(reply, response.getData(), row); sendAck(nrOfRows - 1); nrOfRows--; } else { logger.Debug("1response.getID == " + response.getID()); r_reply = new KWPReply(); return(RequestResult.Timeout); } } r_reply = new KWPReply(reply, a_request.getNrOfPID()); return(RequestResult.NoError); } else { logger.Debug("2response.getID == " + response.getID()); r_reply = new KWPReply(); return(RequestResult.Timeout); } }
/// <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) { /* * int readResult = 0; * int nrOfWait = 0; * while (nrOfWait < timeout) * { * LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); * canMsg = new CANMessage(); * readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); * if (readResult == LAWICEL.ERROR_CANUSB_OK) * { * //Console.WriteLine("rx id: 0x" + r_canMsg.id.ToString("X4")); * if (r_canMsg.id != a_canID) * { * nrOfWait++; * continue; * } * else * { * canMsg.setID(r_canMsg.id); * canMsg.setData(r_canMsg.data); * canMsg.setFlags(r_canMsg.flags); * return (uint)r_canMsg.id; * } * } * else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) * { * Thread.Sleep(1); * nrOfWait++; * } * } * canMsg = new CANMessage(); * return 0;*/ LAWICEL.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new LAWICEL.CANMsg(); readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { Thread.Sleep(1); AddToCanTrace("rx: 0x" + r_canMsg.id.ToString("X4") + r_canMsg.data.ToString("X16")); 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 == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new LAWICEL.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; LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { //Console.WriteLine(r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16")); //if (MessageContainsInformationForRealtime(r_canMsg.id)) { 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) { bitsPerSecond += 109; AddToCanTrace("RX: " + r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16")); rxCount++; foreach (ICANListener listener in m_listeners) { //while (listener.messagePending()) ; // dirty, make this better listener.handleMessage(canMessage); } CastInformationEvent("", rxCount, txCount, errCount); // <GS-05042011> re-activated this function } //Thread.Sleep(1); } // cast event to application to process message //if (MessageContainsInformationForRealtime(r_canMsg.id)) //{ //TODO: process all other known msg id's into the realtime view // CastInformationEvent(canMessage); // <GS-05042011> re-activated this function //} } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { // Console.WriteLine("No message"); Thread.Sleep(1); } else { Console.WriteLine("Result: " + readResult.ToString("X8")); } /*int stat = LAWICEL.canusb_Status(m_deviceHandle); * if (stat != 0) * { * Console.WriteLine("status: " + stat.ToString("X4")); * }*/ } }