/// <summary> /// This method writes to a symbol in RAM. /// The ECU must not be write protected for this to work. /// </summary> /// <param name="a_symbolNumber">Symbol number to write to.</param> /// <param name="a_data">Data to write.</param> /// <returns></returns> public bool writeSymbolRequest(uint a_symbolNumber, byte[] a_data) { LogDataString("writeSymbolRequest: " + a_symbolNumber.ToString()); KWPReply reply = new KWPReply(); KWPResult result; int LengthToTx = a_data.Length; if (LengthToTx > 0x41) LengthToTx = 0x41; // FA byte[] symbolNumberAndData = new byte[3 + /*a_data.Length*/ LengthToTx]; //First two bytes should be the symbol number symbolNumberAndData[0] = (byte)(a_symbolNumber >> 8); symbolNumberAndData[1] = (byte)(a_symbolNumber); symbolNumberAndData[2] = (byte)(0); // len len // adr adr adr for (int i = 0; i < /*a_data.Length*/ LengthToTx; i++) symbolNumberAndData[i + 3] = a_data[i]; string requestString = "RequestString: "; foreach (byte b in symbolNumberAndData) { requestString += b.ToString("X2") + " "; } Console.WriteLine(requestString); // end dump to console Console.WriteLine("SymbolNumberAndData length: " + symbolNumberAndData.Length.ToString("X8")); KWPRequest t_request = new KWPRequest(0x3D, 0x80, symbolNumberAndData); Console.WriteLine(t_request.ToString()); result = sendRequest(t_request, out reply); if (result != KWPResult.OK) { Console.WriteLine("Result != KWPResult.OK"); return false; } Console.WriteLine("Result = " + reply.getData()[0].ToString("X2")); Console.WriteLine("Resulttotal = " + reply.ToString()); if (reply.getData()[0] == 0x7D) { return true; } else { return false; } }
/// <summary> /// This method writes to a symbol in RAM. /// The ECU must not be write protected for this to work. /// </summary> /// <param name="a_symbolNumber">Symbol number to write to.</param> /// <param name="a_data">Data to write.</param> /// <returns></returns> public bool writeSymbolRequestAddress(uint a_address, byte[] a_data) { LogDataString("writeSymbolRequest: " + a_address.ToString("X8") + "len: " + a_data.Length.ToString("X4")); KWPReply reply = new KWPReply(); KWPResult result; byte[] symbolNumberAndData = new byte[4 + a_data.Length]; //symbolNumberAndData[0] = (byte)(a_address >> 24); symbolNumberAndData[0] = (byte)(a_address >> 16); symbolNumberAndData[1] = (byte)(a_address >> 8); symbolNumberAndData[2] = (byte)(a_address); symbolNumberAndData[3] = (byte)(a_data.Length); // len len // adr adr adr for (int i = 0; i < a_data.Length; i++) symbolNumberAndData[i + 4] = a_data[i]; string requestString = "RequestString: "; foreach (byte b in symbolNumberAndData) { requestString += b.ToString("X2") + " "; } Console.WriteLine(requestString); // end dump to console Console.WriteLine("SymbolNumberAndData length: " + symbolNumberAndData.Length.ToString("X8")); KWPRequest t_request = new KWPRequest(0x3D, /*0x81, */symbolNumberAndData); Console.WriteLine(t_request.ToString()); result = sendRequest(t_request, out reply); if (result != KWPResult.OK) { Console.WriteLine("Result != KWPResult.OK"); return false; } Console.WriteLine("Result = " + reply.getData()[0].ToString("X2")); Console.WriteLine("Result-total = " + reply.ToString()); if (reply.getData()[0] == 0x7D) { return true; } else { return false; } }
/// <summary> /// This method send a request to receive data from flash. The sendReadRequest /// method must be called before this. /// </summary> /// <param name="r_data">The requested data.</param> /// <returns></returns> public bool sendRequestDataByOffset(out byte[] r_data) { LogDataString("sendRequestDataByOffset"); KWPReply reply = new KWPReply(); KWPResult result; result = sendRequest(new KWPRequest(0x21, 0xF0), out reply); r_data = reply.getData(); if (result == KWPResult.OK) return true; else return false; }
/// <summary> /// This method sets the E85 level. /// </summary> /// <param name="a_level">The E85 level.</param> /// <returns>KWPResult</returns> public KWPResult setE85Level(int a_level) { KWPReply reply = new KWPReply(); KWPResult result; int sendlevel = a_level * 10; byte[] level = new byte[2]; level[0] = (byte)(sendlevel >> 8); level[1] = (byte)sendlevel; result = sendRequest(new KWPRequest(0x3B, 0xA7, level), out reply); if(reply.getMode() == 0x7B && reply.getPid() == 0xA7) { return KWPResult.OK; } else if(reply.getMode() == 0x7F && reply.getPid() == 0x3B && reply.getLength() == 3) { Console.WriteLine(TranslateErrorCode(reply.getData()[0])); } return KWPResult.NOK; }
public bool ReadDTCCodes(out List<string> list) { list = new List<string>(); LogDataString("ReadDTCCodes"); KWPReply reply = new KWPReply(); KWPResult result; byte[] data = new byte[2]; data[0] = (byte)0xFF; data[1] = (byte)0xFF; //data[2] = (byte)0x00; // Status byte // 7 Warning lamp illuminated for this code // 6 Warning lamp pending for this code, not illuminate but malfunction was detected // 5 Warning lamp was previously illuminated for this code, malfunction not currently detected, code not yet erased // 4 Stored trouble code // 3 Manufacturer specific status // 2 Manufacturer specific status // 1 Current code - present at time of request // 0 Maturing/intermittent code - insufficient data to consider as a malfunction KWPRequest req = new KWPRequest(0x18 , 0x02); // Request Diagnostic Trouble Codes by Status Console.WriteLine(req.ToString()); result = sendRequest(req, out reply); Console.WriteLine(reply.ToString()); // J2190 // Multiple Mode $58 response messages may be reported to a single request, depending on the number of diagnostic // trouble codes stored in the module. Each response message will report up to three DTCs for // which at least one of the requested status bits is set. If no codes are stored in the module that meet the // requested status, then the module will respond with the following: // Reply: 58,00 if (reply.getMode() == 0x58) { if (reply.getPid() == 0x00) { Console.WriteLine("No DTC's"); list.Add("No DTC's"); return true; } else { //P0605 //P1231 //P1230 //P1530 //P1606 //P1460 //+ reply {Reply: 14,58, // 06, // 06,05,E4, // 12,31,48, // 12,30,E8, // 15,30,E1, // 16,06,E8, // 14,60,41} TrionicCANLib.KWP.KWPReply uint number = reply.getPid(); byte[] dtc = new byte[number*2]; byte[] read = reply.getData(); int j = 0; int i = 0; while(i < read.Length) { dtc[j++] = read[i++]; dtc[j++] = read[i++]; i++; } for (int n = 0; n < dtc.Length; n = n + 2) { list.Add("DTC: P" + dtc[n].ToString("X2") + dtc[n+1].ToString("X2")); } } } return false; }
/// <summary> /// This method requests data to be transmitted. /// </summary> /// <param name="a_data">The data to be transmitted.</param> /// <returns>KWPResult</returns> public KWPResult sendDataTransferRequest(out byte[] a_data) { LogDataString("sendDataTransferRequest"); KWPReply reply = new KWPReply(); KWPResult result; //Send request //Mode = 0x36 //PID = no PID used by this request //Data = no data //Expected result = 0x76 result = sendRequest(new KWPRequest(0x36), out reply); a_data = reply.getData(); if (reply.getMode() != 0x76) return KWPResult.NOK; else return result; }
/// <summary> /// Get E85 adaption status. /// </summary> /// <param name="r_status">The adaptino status for E85.</param> /// <returns>KWPResult</returns> public KWPResult getE85AdaptionStatus(out string r_status) { KWPReply reply = new KWPReply(); KWPResult result; r_status = "Error"; result = sendRequest(new KWPRequest(0x21, 0xA5), out reply); if (result == KWPResult.OK) { byte[] res = reply.getData(); if(reply.getData()[0] == 1) r_status = "Forced"; if(reply.getData()[0] == 2) r_status = "Ongoing"; if(reply.getData()[0] == 3) r_status = "Completed"; if(reply.getData()[0] == 4) r_status = "Unknown"; if(reply.getData()[0] == 5) r_status = "Not started"; return KWPResult.OK; } else { r_status = ""; return KWPResult.Timeout; } }
/// <summary> /// This method requests the E85 level. /// </summary> /// <param name="r_level">The requested E85 level.</param> /// <returns>KWPResult</returns> public KWPResult getE85Level(out float r_level) { KWPReply reply = new KWPReply(); KWPResult result; float level; result = sendRequest(new KWPRequest(0x21, 0xA7), out reply); // Request Diagnostic Data Mode $21 - Offset (1 byte) if (reply.getMode() == 0x61 && reply.getPid() == 0xA7 && reply.getLength() == 4) { level = (reply.getData()[0] << 8) | reply.getData()[1]; r_level = level / 10; return KWPResult.OK; } else if (reply.getMode() == 0x7F && reply.getPid() == 0x21 && reply.getLength() == 3) { Console.WriteLine(TranslateErrorCode(reply.getData()[0])); } r_level = 0; return KWPResult.NOK; }
/// <summary> /// Send a request for a sequrity access with one out of two methods to /// calculate the key. /// </summary> /// <param name="a_method">Key calculation method [1,2]</param> /// <returns>true if sequrity access was granted, otherwise false</returns> private bool requestSequrityAccessLevel(uint a_method) { LogDataString("requestSequrityAccessLevel: " + a_method.ToString()); KWPReply reply = new KWPReply(); KWPResult result; byte[] seed = new byte[2]; byte[] key = new byte[2]; // Send a seed request. KWPRequest requestForKey = new KWPRequest(0x27, 0x05); Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() + " request for key: " + requestForKey.ToString()); result = sendRequest(requestForKey, out reply); Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() + " request for key result: " + reply.ToString()); if (result != KWPResult.OK) return false; if (reply.getData().Length < 2) return false; seed[0] = reply.getData()[0]; seed[1] = reply.getData()[1]; if (a_method == 1) key = calculateKey(seed, 0); else key = calculateKey(seed, 1); // Send key reply. KWPRequest sendKeyRequest = new KWPRequest(0x27, 0x06, key); Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() + " send Key request: " + sendKeyRequest.ToString()); result = sendRequest(sendKeyRequest, out reply); Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() + " send Key reply: " + reply.ToString()); if (result != KWPResult.OK) { Console.WriteLine("Security access request was not send"); return false; } //Check if sequrity was granted. Console.WriteLine("Mode: " + reply.getMode().ToString("X2")); Console.WriteLine("Data: " + reply.getData()[0].ToString("X2")); if ((reply.getMode() == 0x67) && (reply.getData()[0] == 0x34)) // WAS [0] { Console.WriteLine("Security access granted: " + a_method.ToString()); return true; } Console.WriteLine("Security access was not granted: " + reply.ToString()); return false; }
private UInt16 getUint16(KWPReply a_reply) { UInt16 uinteger; uinteger = (UInt16)((a_reply.getData()[1] << 8) | (a_reply.getData()[0])); return uinteger; }
/// <summary> /// Helper method for transforming the information in a KWPReply to a string. /// </summary> /// <param name="a_reply">The KWPReply.</param> /// <returns>A string representing the information in the a_reply.</returns> private string getString(KWPReply a_reply) { if (a_reply.getData().Length == 0) return ""; Encoding ascii = Encoding.ASCII; ascii.GetChars(a_reply.getData(), 0, a_reply.getData().Length); return ascii.GetString(a_reply.getData(), 0, a_reply.getData().Length); }
/// <summary> /// This method send a request to receive data from flash. The sendReadRequest /// method must be called before this. /// </summary> /// <param name="r_data">The requested data.</param> /// <returns></returns> public bool sendRequestDataByOffset(out byte[] r_data) { logger.Trace("sendRequestDataByOffset"); KWPReply reply = new KWPReply(); KWPResult result; var request = new KWPRequest(0x21, 0xF0); request.ElmExpectedResponses=1; result = sendRequest(request, out reply); if (result == KWPResult.OK) { r_data = reply.getData(); return true; } else { r_data = new byte[0]; return false; } }
/// <summary> /// This method send a request for reading from ECU memory (both RAM and flash). /// It sets up start address and the length to read. The resulting response contains the requested values. /// </summary> /// <param name="a_address">The address to start reading from.</param> /// <param name="a_length">The total length to read.</param> /// <returns>true on success, otherwise false</returns> public bool sendReadRequest(uint a_address, uint a_length, out byte[] data) { logger.Debug("sendReadRequest"); KWPReply reply = new KWPReply(); KWPResult result; data = new byte[a_length]; byte[] lengthAndAddress = new byte[6]; //set address and length (byte 0) lengthAndAddress[0] = (byte)(a_address >> 16); lengthAndAddress[1] = (byte)(a_address >> 8); lengthAndAddress[2] = (byte)(a_address); lengthAndAddress[3] = (byte)(a_length); lengthAndAddress[4] = 0x01; lengthAndAddress[5] = 0; KWPRequest request = new KWPRequest(0x23, lengthAndAddress); request.ElmExpectedResponses = 1; result = sendRequest(request, out reply); data = reply.getData(); if (result == KWPResult.OK) return true; else return false; }