/// <summary> /// This method sends a KWP request and returns a KWPReply. The method returns /// when a reply has been received, after a failure or after a timeout. /// The open and startSession methods must be called and returned possitive result /// before this method is used. /// </summary> /// <param name="a_request">The KWPRequest.</param> /// <param name="r_reply">The reply to the KWPRequest.</param> /// <returns>RequestResult.</returns> public RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply) { string sendString = ""; string receiveString = ""; for (int i = 1; i < a_request.getData().Length; i++) { sendString += a_request.getData()[i].ToString("X") + " "; } sendString += "\r"; m_serialPort.Write(sendString); //receiveString = "49 01 01 00 00 00 31 \n\r49 02 02 44 34 47 50 \n\r49 02 03 30 30 52 35 \n\r49 02 04 25 42";// m_serialPort.ReadTo(">"); receiveString = m_serialPort.ReadTo(">"); char[] chrArray = receiveString.ToCharArray(); byte[] reply = new byte[0xFF]; int insertPos = 1; int index = 0; string subString = ""; while (receiveString.Length > 4) { //Remove first three bytes //TODO. Remove Mode and PIDs for (int i = 0; i < 3; i++) { index = receiveString.IndexOf(" "); receiveString = receiveString.Remove(0, index + 1); } //Read data for the rest of the row. for (int i = 0; i < 4; i++) { index = receiveString.IndexOf(" "); if (index == 0) //Last row not 4 bytes of data. { continue; } subString = receiveString.Substring(0, index); reply[insertPos] = (byte)Convert.ToInt16("0x" + subString, 16); insertPos++; receiveString = receiveString.Remove(0, index + 1); } } reply[0] = (byte)insertPos; //Length r_reply = new KWPReply(reply, a_request.getNrOfPID()); return(RequestResult.NoError); }
/// <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> /// This method sends a KWP request and returns a KWPReply. The method returns /// when a reply has been received, after a failure or after a timeout. /// The open and startSession methods must be called and returned possitive result /// before this method is used. /// </summary> /// <param name="a_request">The KWPRequest.</param> /// <param name="r_reply">The reply to the KWPRequest.</param> /// <returns>RequestResult.</returns> public override RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply) { string sendString = ""; string receiveString = ""; byte[] reply = new byte[0xFF]; try { for (int i = 1; i < a_request.getData().Length; i++) { string tmpStr = a_request.getData()[i].ToString("X"); if (tmpStr.Length == 1) sendString += "0" + tmpStr + " "; else sendString += tmpStr + " "; } sendString += "\r"; m_serialPort.Write(sendString); //receiveString = "5A 90 59 53 33 45 46 35 39 45 32 33 33 30 32 30 38 32 37 \r\n\r\n";// m_serialPort.ReadTo(">"); receiveString = m_serialPort.ReadTo(">"); string tmpString = receiveString; int insertPos = 1; string subString = ""; while (receiveString.Length > 4) { int index = receiveString.IndexOf(" "); subString = receiveString.Substring(0, index); reply[insertPos] = (byte)Convert.ToInt16("0x" + subString, 16); insertPos++; receiveString = receiveString.Remove(0, index + 1); } insertPos--; reply[0] = (byte)insertPos; //Length r_reply = new KWPReply(reply, a_request.getNrOfPID()); return RequestResult.NoError; } catch (Exception) { r_reply = new KWPReply(reply, a_request.getNrOfPID()); return RequestResult.ErrorSending; } }
/// <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> /// This method sends a KWP request and returns a KWPReply. The method returns /// when a reply has been received, after a failure or after a timeout. /// The open and startSession methods must be called and returned possitive result /// before this method is used. /// </summary> /// <param name="a_request">The KWPRequest.</param> /// <param name="r_reply">The reply to the KWPRequest.</param> /// <returns>RequestResult.</returns> public RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply) { string sendString = ""; string receiveString = ""; for (int i = 1; i < a_request.getData().Length; i++) sendString += a_request.getData()[i].ToString("X") + " "; sendString += "\r"; m_serialPort.Write(sendString); //receiveString = "49 01 01 00 00 00 31 \n\r49 02 02 44 34 47 50 \n\r49 02 03 30 30 52 35 \n\r49 02 04 25 42";// m_serialPort.ReadTo(">"); receiveString = m_serialPort.ReadTo(">"); char[] chrArray = receiveString.ToCharArray(); byte[] reply = new byte[0xFF]; int insertPos = 1; int index = 0; string subString = ""; while(receiveString.Length > 4) { //Remove first three bytes //TODO. Remove Mode and PIDs for (int i = 0; i < 3; i++) { index = receiveString.IndexOf(" "); receiveString = receiveString.Remove(0, index + 1); } //Read data for the rest of the row. for (int i = 0; i < 4; i++) { index = receiveString.IndexOf(" "); if (index == 0) //Last row not 4 bytes of data. { continue; } subString = receiveString.Substring(0, index); reply[insertPos] = (byte)Convert.ToInt16("0x"+subString, 16); insertPos++; receiveString = receiveString.Remove(0, index + 1); } } reply[0] = (byte)insertPos; //Length r_reply = new KWPReply(reply, a_request.getNrOfPID()); return RequestResult.NoError; }
/// <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; } }