/// <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> /// 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 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); } }