public override bool sendMessage(CANMessage a_message) { string sendString = "t"; sendString += a_message.getID().ToString("X3"); sendString += a_message.getLength().ToString("X1"); for (uint i = 0; i < a_message.getLength(); i++) // leave out the length field, the ELM chip assigns that for us { sendString += a_message.getCanData(i).ToString("X2"); } sendString += "\r"; if (m_serialPort.IsOpen) { AddToCanTrace("TX: " + a_message.getID().ToString("X3") + " " + a_message.getLength().ToString("X1") + " " + a_message.getData().ToString("X16")); m_serialPort.Write(sendString); //Console.WriteLine("TX: " + sendString); } // bitrate = 38400bps -> 3840 bytes per second // sending each byte will take 0.2 ms approx //Thread.Sleep(a_message.getLength()); // sleep length ms // Thread.Sleep(10); Thread.Sleep(1); return(true); // remove after implementation }
protected override bool sendMessageDevice(CANMessage a_message) { if (!m_serialPort.IsOpen) { return(false); } string sendString = "t"; sendString += a_message.getID().ToString("X3"); sendString += a_message.getLength().ToString("X1"); for (uint i = 0; i < a_message.getLength(); i++) // leave out the length field, the ELM chip assigns that for us { sendString += a_message.getCanData(i).ToString("X2"); } sendString += "\r"; m_serialPort.Write(sendString); //logger.Debug("TX: " + sendString); // bitrate = 38400bps -> 3840 bytes per second // sending each byte will take 0.2 ms approx //Thread.Sleep(a_message.getLength()); // sleep length ms // Thread.Sleep(10); Thread.Sleep(1); return(true); // remove after implementation }
/// <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; } }
private bool RequestSecurityAccessCIM(int millisecondsToWaitWithResponse) { int secondsToWait = millisecondsToWaitWithResponse / 1000; ulong cmd = 0x0000000000012702; // request security access CANMessage msg = new CANMessage(0x245, 0, 8); msg.setData(cmd); m_canListener.setupWaitMessage(0x645); CastInfoEvent("Requesting security access to CIM", ActivityType.ConvertingFile); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return false; } CANMessage response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); //ulong data = response.getData(); Console.WriteLine("---" + response.getData().ToString("X16")); if (response.getCanData(1) == 0x67) { if (response.getCanData(2) == 0x01) { CastInfoEvent("Got seed value from CIM", ActivityType.ConvertingFile); while (secondsToWait > 0) { CastInfoEvent("Waiting for " + secondsToWait.ToString() + " seconds...", ActivityType.UploadingBootloader); Thread.Sleep(1000); SendKeepAlive(); secondsToWait--; } byte[] seed = new byte[2]; seed[0] = response.getCanData(3); seed[1] = response.getCanData(4); if (seed[0] == 0x00 && seed[1] == 0x00) { return true; // security access was already granted } else { SeedToKey s2k = new SeedToKey(); byte[] key = s2k.calculateKeyForCIM(seed); CastInfoEvent("Security access CIM : Key (" + key[0].ToString("X2") + key[1].ToString("X2") + ") calculated from seed (" + seed[0].ToString("X2") + seed[1].ToString("X2") + ")", ActivityType.ConvertingFile); ulong keydata = 0x0000000000022704; ulong key1 = key[1]; key1 *= 0x100000000; keydata ^= key1; ulong key2 = key[0]; key2 *= 0x1000000; keydata ^= key2; msg = new CANMessage(0x245, 0, 8); msg.setData(keydata); m_canListener.setupWaitMessage(0x645); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return false; } response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); // is it ok or not if (response.getCanData(1) == 0x67 && response.getCanData(2) == 0x02) { CastInfoEvent("Security access to CIM granted", ActivityType.ConvertingFile); return true; } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27) { CastInfoEvent("Error: " + TranslateErrorCode(response.getCanData(3)), ActivityType.ConvertingFile); } } } else if (response.getCanData(2) == 0x02) { CastInfoEvent("Security access to CIM granted", ActivityType.ConvertingFile); return true; } } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27) { CastInfoEvent("Error: " + TranslateErrorCode(response.getCanData(3)), ActivityType.ConvertingFile); } return false; }
private bool addDTC(CANMessage response) { // Read until response: EndOfDTCReport if (response.getCanData(1) == 0 && response.getCanData(2) == 0 && response.getCanData(3) == 0) { listDTC.Add("No more errors!"); return false; } else { string dtcDescription = GetDtcDescription(response); logger.Debug(dtcDescription); listDTC.Add(dtcDescription); return true; } }
// How to read DTC codes //A7 A6 First DTC character //-- -- ------------------- // 0 0 P - Powertrain // 0 1 C - Chassis // 1 0 B - Body // 1 1 U - Network //A5 A4 Second DTC character //-- -- -------------------- // 0 0 0 // 0 1 1 // 1 0 2 // 1 1 3 //A3 A2 A1 A0 Third/Fourth/Fifth DTC characters //-- -- -- -- ------------------- // 0 0 0 0 0 // 0 0 0 1 1 // 0 0 1 0 2 // 0 0 1 1 3 // 0 1 0 0 4 // 0 1 0 1 5 // 0 1 1 0 6 // 0 1 1 1 7 // 1 0 0 0 8 // 1 0 0 1 9 // 1 0 1 0 A // 1 0 1 1 B // 1 1 0 0 C // 1 1 0 1 D // 1 1 1 0 E // 1 1 1 1 F // Example // E1 03 -> // 1110 0001 0000 0011 // 11=U // 10=2 // 0001=1 // 0000=0 // 0011=3 //---------------------- // U2103 private static string GetDtcDescription(CANMessage responseDTC) { int firstDtcNum = (0xC0 & Convert.ToInt32(responseDTC.getCanData(1))) >> 6; char firstDtcChar = '-'; if (firstDtcNum == 0) { firstDtcChar = 'P'; } else if (firstDtcNum == 1) { firstDtcChar = 'C'; } else if (firstDtcNum == 2) { firstDtcChar = 'B'; } else if (firstDtcNum == 3) { firstDtcChar = 'U'; } int secondDtcNum = (0x30 & Convert.ToInt32(responseDTC.getCanData(1))) >> 4; int thirdDtcNum = (0x0F & Convert.ToInt32(responseDTC.getCanData(1))); int forthDtcNum = (0xF0 & Convert.ToInt32(responseDTC.getCanData(2))) >> 4; int fifthDtcNum = (0x0F & Convert.ToInt32(responseDTC.getCanData(2))); // It seems Trionic8 return 00 //byte failureTypeByte = responseDTC.getCanData(3); byte statusByte = responseDTC.getCanData(4); //Removed this output //String statusDescription = string.Empty; //if (0x80 == (0x80 & statusByte)) statusDescription += "warningIndicatorRequestedState "; //if (0x40 == (0x40 & statusByte)) statusDescription += "currentDTCSincePowerUp "; //if (0x20 == (0x20 & statusByte)) statusDescription += "testNotPassedSinceCurrentPowerUp "; //if (0x10 == (0x10 & statusByte)) statusDescription += "historyDTC "; //if (0x08 == (0x08 & statusByte)) statusDescription += "testFailedSinceDTCCleared "; //if (0x04 == (0x04 & statusByte)) statusDescription += "testNotPassedSinceDTCCleared "; //if (0x02 == (0x02 & statusByte)) statusDescription += "currentDTC "; //if (0x01 == (0x01 & statusByte)) statusDescription += "DTCSupportedByCalibration "; return "DTC: " + firstDtcChar + secondDtcNum.ToString("d") + thirdDtcNum.ToString("X") + forthDtcNum.ToString("X") + fifthDtcNum.ToString("X") + " StatusByte: " + statusByte.ToString("X2"); }
public float GetE85Percentage() { float retval = 0; GetDiagnosticDataIdentifier(); // ReadDataByPacketIdentifier ($AA) Service CANMessage msg = new CANMessage(0x7E0, 0, 4); ulong cmd = 0x000000007A01AA03;// <dpid=7A> <level=sendOneResponse> <service=AA> <length> msg.setData(cmd); m_canListener.setupWaitMessage(0x5E8); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return 0f; } CANMessage response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); // 7A 00 52 04 00 16 DC 00 // <dpid><6 datavalues> if (response.getCanData(0) == 0x7A) { retval = Convert.ToInt32(response.getCanData(2)); } // Negative Response 0x7F Service <nrsi> <service> <returncode> // Bug: this is never handled because its sent with id=0x7E8 else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xAA) { string info = TranslateErrorCode(response.getCanData(3)); CastInfoEvent("Error: " + info, ActivityType.ConvertingFile); } return retval; }
public void setECUparameterVIN(string vin) { // 62 DPID + 01 sendOneResponse + $AA ReadDataByPacketIdentifier CANMessage msg62 = new CANMessage(0x7E0, 0, 4); //<GS-18052011> ELM327 support requires the length byte msg62.setData(0x000000006201AA03); m_canListener.setupWaitMessage(0x5E8); CastInfoEvent("Wait for response 5E8 62 00 00", ActivityType.ConvertingFile); if (!canUsbDevice.sendMessage(msg62)) { Console.WriteLine("Couldn't send message"); } CANMessage response62 = new CANMessage(); response62 = m_canListener.waitMessage(1000); Console.WriteLine("---" + response62.getData().ToString("X16")); //05E8 62 00 00 02 A7 01 7F 01 if (response62.getCanData(0) == 0x62) { if (response62.getCanData(1) == 0x00) { if (response62.getCanData(2) == 0x00) { CastInfoEvent("Got response 5E8 62 00 00", ActivityType.ConvertingFile); } } } if (GetManufacturersEnableCounter() == 0x00) CastInfoEvent("GetManufacturersEnableCounter == 0x00", ActivityType.ConvertingFile); CastInfoEvent("ECM EOL Parameter Settings-part1", ActivityType.ConvertingFile); // 02 DPID + 01 sendOneResponse + $AA ReadDataByPacketIdentifier CANMessage msg = new CANMessage(0x7E0, 0, 4); //<GS-18052011> ELM327 support requires the length byte msg.setData(0x000000000201AA03); m_canListener.setupWaitMessage(0x5E8); CastInfoEvent("Wait for response 5E8 02 02", ActivityType.ConvertingFile); if (!canUsbDevice.sendMessage(msg)) { Console.WriteLine("Couldn't send message"); } CANMessage response = new CANMessage(); response = m_canListener.waitMessage(1000); Console.WriteLine("---" + response.getData().ToString("X16")); //05E8 02 02 A0 42 80 A0 00 00 if (response.getCanData(0) == 0x02) { if (response.getCanData(1) == 0x02) { CastInfoEvent("Got response 5E8 02 02", ActivityType.ConvertingFile); } } if (ProgramVIN(vin)) CastInfoEvent("ProgramVIN true", ActivityType.ConvertingFile); Thread.Sleep(200); //RequestSecurityAccess(2000); //CastInfoEvent("End programming?", ActivityType.ConvertingFile); //SendCommandNoResponse(0x7E0, 0x0000000000012702); // 01 SPSrequestSeed + $27 SecurityAccess //CastInfoEvent("Unidentified, security access again?", ActivityType.ConvertingFile); //SendCommandNoResponse(0x7E0, 0x000000EFA4022704); // EFA4 securitykey + 02 SPSsendKey + $27 SecurityAccess //SendCommandNoResponse(0x7E0, 0x0000000000901A02); string newVIN = GetVehicleVIN(); CastInfoEvent("New VIN: " + newVIN, ActivityType.ConvertingFile); }
public string RequestECUInfo(uint _pid, string description, int expectedResponses) { string retval = string.Empty; byte[] rx_buffer = new byte[128]; int rx_pnt = 0; if (canUsbDevice.isOpen()) { ulong cmd = 0x0000000000001A02 | _pid << 16; //SendMessage(data); // software version CANMessage msg = new CANMessage(0x7E0, 0, 3); // test GS was 8 msg.setData(cmd); msg.elmExpectedResponses = expectedResponses; m_canListener.setupWaitMessage(0x7E8); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return string.Empty; } int msgcnt = 0; bool _success = false; CANMessage response = new CANMessage(); ulong data = 0; while (!_success && msgcnt < 2) { response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); data = response.getData(); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x1A && response.getCanData(3) == 0x78) {} //RequestCorrectlyReceived-ResponsePending else if (response.getCanData(1) != 0x7E) { _success = true; } msgcnt++; } if (response.getCanData(1) == 0x5A) { // only one frame in this repsonse for (uint fi = 3; fi < 8; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi); retval = Encoding.ASCII.GetString(rx_buffer, 0, rx_pnt - 1); } else if (response.getCanData(2) == 0x5A) { SendAckMessageT8(); byte len = response.getCanData(1); int m_nrFrameToReceive = ((len - 4) / 8); if ((len - 4) % 8 > 0) m_nrFrameToReceive++; int lenthisFrame = len; if (lenthisFrame > 4) lenthisFrame = 4; for (uint fi = 4; fi < 4 + lenthisFrame; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi); // wait for more records now while (m_nrFrameToReceive > 0) { m_canListener.setupWaitMessage(0x7E8); response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x1A && response.getCanData(3) == 0x78) { } //RequestCorrectlyReceived-ResponsePending else if (response.getCanData(1) != 0x7E) { m_nrFrameToReceive--; data = response.getData(); // add the bytes to the receive buffer for (uint fi = 1; fi < 8; fi++) { if (rx_pnt < rx_buffer.Length) // prevent overrun { rx_buffer[rx_pnt++] = response.getCanData(fi); } } } } retval = Encoding.ASCII.GetString(rx_buffer, 0, rx_pnt - 1); } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27) { CastInfoEvent("Error: " + TranslateErrorCode(response.getCanData(3)), ActivityType.ConvertingFile); } } Thread.Sleep(25); return retval; }
public string RequestCIMInfo(uint _pid) { string retval = string.Empty; byte[] rx_buffer = new byte[128]; int rx_pnt = 0; if (canUsbDevice.isOpen()) { // pid=2 // 17-12-2012 17:15:51.239 - TX: 0245 0000000000021A02 // 17-12-2012 17:15:51.298 - RX: 0645 00000000311A7F03 // pid=3 // 17-12-2012 17:16:41.190 - TX: 0245 0000000000031A02 // 17-12-2012 17:16:41.238 - RX: 0645 00000000311A7F03 ulong cmd = 0x0000000000001A02 | _pid << 16; //SendMessage(data); // software version CANMessage msg = new CANMessage(0x245, 0, 8); msg.setData(cmd); m_canListener.setupWaitMessage(0x645); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return string.Empty; } int msgcnt = 0; bool _success = false; CANMessage response = new CANMessage(); ulong data = 0; while (!_success && msgcnt < 2) { response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); data = response.getData(); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x1A && response.getCanData(3) == 0x78) { } //RequestCorrectlyReceived-ResponsePending else if (response.getCanData(1) != 0x7E) _success = true; msgcnt++; } //CANMessage response = new CANMessage(); //response = m_canListener.waitMessage(timeoutPTct); //ulong data = response.getData(); if (response.getCanData(1) == 0x5A) { // only one frame in this repsonse for (uint fi = 3; fi < 8; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi); retval = Encoding.ASCII.GetString(rx_buffer, 0, rx_pnt); } else if (response.getCanData(2) == 0x5A) { SendAckMessageCIM(); byte len = response.getCanData(1); int m_nrFrameToReceive = ((len - 4) / 8); if ((len - 4) % 8 > 0) m_nrFrameToReceive++; int lenthisFrame = len; if (lenthisFrame > 4) lenthisFrame = 4; for (uint fi = 4; fi < 4 + lenthisFrame; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi); // wait for more records now while (m_nrFrameToReceive > 0) { m_canListener.setupWaitMessage(0x645); response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x1A && response.getCanData(3) == 0x78) { } //RequestCorrectlyReceived-ResponsePending else if (response.getCanData(1) != 0x7E) { m_nrFrameToReceive--; data = response.getData(); // add the bytes to the receive buffer for (uint fi = 1; fi < 8; fi++) { if (rx_pnt < rx_buffer.Length) // prevent overrun { rx_buffer[rx_pnt++] = response.getCanData(fi); } } } } retval = Encoding.ASCII.GetString(rx_buffer, 0, rx_pnt); } else if (response.getCanData(1) == 0x7F && response.getCanData(1) == 0x27) { string info = TranslateErrorCode(response.getCanData(3)); CastInfoEvent("Error: " + info, ActivityType.ConvertingFile); } } Thread.Sleep(25); return retval; }
public bool ClearDTCCodes() { bool retval = false; // ClearDiagnosticInformation ($04) Service ulong cmd = 0x0000000000000401; // 7DF 01 04 00 00 00 00 00 00 CANMessage msg = new CANMessage(0x7DF, 0, 2); msg.setData(cmd); msg.elmExpectedResponses = 15; m_canListener.setupWaitMessage(0x7E8); canUsbDevice.SetupCANFilter("7E8", "000"); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return false; } CANMessage response = new CANMessage(); ulong data = 0; // Wait for response // 7E8 01 44 00 00 00 00 00 00 response = m_canListener.waitMessage(timeoutP2ct); data = response.getData(); // Positive Response if (response.getID() == 0x7E8 && response.getCanData(1) == 0x44) { retval = true; } // RequestCorrectlyReceived-ResponsePending ($78, RC_RCR-RP) else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x04 && response.getCanData(3) == 0x78) { // Wait one more second m_canListener.setupWaitMessage(0x7E8); m_canListener.waitMessage(timeoutP2ct); if (response.getID() == 0x7E8 && response.getCanData(1) == 0x44) { retval = true; } } // Other errors else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x04) { string info = TranslateErrorCode(response.getCanData(3)); CastInfoEvent("Error: " + info, ActivityType.ConvertingFile); } Send0120(); return retval; }
// MattiasC, this one is probably not working, need a car to test // look at readDTCCodes() how it was improved. public string[] readDTCCodesCIM() { // test code //ulong c = 0x0000006F00070181;//81 01 07 00 6F 00 00 00 //ulong c = 0x000000FD00220181; //81 01 22 00 FD 00 00 00 //CANMessage test = new CANMessage(); //test.setData(c); //AddToCanTrace(GetDtcDescription(test)); // send message to read DTC StartSession10(); List<string> list = new List<string>(); ulong cmd = 0x000000001281A903; // 245 03 A9 81 12 00 00 00 00 CANMessage msg = new CANMessage(0x245, 0, 8); msg.setData(cmd); m_canListener.setupWaitMessage(0x545); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return list.ToArray(); } CANMessage response = new CANMessage(); ulong data = 0; // Wait for response // 545 03 7F A9 78 00 00 00 00 response = m_canListener.waitMessage(timeoutP2ct); data = response.getData(); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xA9 && response.getCanData(3) == 0x78) { // Now wait for all DTCs m_canListener.setupWaitMessage(0x545); bool more_errors = true; while (more_errors) { CANMessage responseDTC = new CANMessage(); responseDTC = m_canListener.waitMessage(timeoutP2ct); // Read until response: No more errors, status == 0xFF int dtcStatus = Convert.ToInt32(responseDTC.getCanData(4)); if (dtcStatus == 0xFF) { more_errors = false; list.Add("0xFF No more errors!"); } else if (dtcStatus == 0x97) { more_errors = false; list.Add("0x17 No more errors!"); } else { string dtcDescription = GetDtcDescription(responseDTC); list.Add(dtcDescription); } } } Send0120(); return list.ToArray(); }
public string[] ReadDTC() { // test code //ulong c = 0x0000006F00070181;//81 01 07 00 6F 00 00 00 //ulong c = 0x000000FD00220181; //81 01 22 00 FD 00 00 00 //CANMessage test = new CANMessage(); //test.setData(c); //AddToCanTrace(GetDtcDescription(test)); // send message to read DTC StartSession10(); listDTC = new List<string>(); // ReadDiagnosticInformation $A9 Service // readStatusOfDTCByStatusMask $81 Request // DTCStatusMask $12= 0001 0010 // 0 Bit 7 warningIndicatorRequestedState // 0 Bit 6 currentDTCSincePowerUp // 0 Bit 5 testNotPassedSinceCurrentPowerUp // 1 Bit 4 historyDTC // 0 Bit 3 testFailedSinceDTCCleared // 0 Bit 2 testNotPassedSinceDTCCleared // 1 Bit 1 currentDTC // 0 Bit 0 DTCSupportedByCalibration ulong cmd = 0x000000001281A903; // 7E0 03 A9 81 12 00 00 00 00 CANMessage msg = new CANMessage(0x7E0, 0, 4); msg.setData(cmd); msg.elmExpectedResponses = 15; m_canListener.setupWaitMessage(0x7E8,0x5e8); canUsbDevice.SetupCANFilter("7E8", "DFF"); // Mask will allow 7E8 and 5E8 if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return listDTC.ToArray(); } CANMessage response = new CANMessage(); ulong data = 0; // Wait for response // 7E8 03 7F A9 78 00 00 00 00 // or the first DTC // 5E8 81 07 03 00 7F 00 00 00 response = m_canListener.waitMessage(timeoutP2ct); data = response.getData(); if (response.getID() == 0x5E8 && response.getCanData(0) == 0x81) { // Now wait for all DTCs m_canListener.setupWaitMessage(0x5E8); bool more_errors = addDTC(response); while (more_errors) { CANMessage responseDTC = new CANMessage(); responseDTC = m_canListener.waitMessage(timeoutP2ct); more_errors = addDTC(responseDTC); } } // RequestCorrectlyReceived-ResponsePending ($78, RC_RCR-RP) else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xA9 && response.getCanData(3) == 0x78) { // Now wait for all DTCs m_canListener.setupWaitMessage(0x5E8); bool more_errors = true; while (more_errors) { CANMessage responseDTC = new CANMessage(); responseDTC = m_canListener.waitMessage(timeoutP2ct); more_errors = addDTC(responseDTC); } } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xA9) { string info = TranslateErrorCode(response.getCanData(3)); CastInfoEvent("Error: " + info, ActivityType.ConvertingFile); } Send0120(); return listDTC.ToArray(); }
private bool RequestSecurityAccess011(int millisecondsToWaitWithResponse) { int secondsToWait = millisecondsToWaitWithResponse / 1000; ulong cmd = 0x0000000000FD2702; // request security access if (_securityLevel == AccessLevel.AccessLevel01) { cmd = 0x0000000000012702; // request security access } else if (_securityLevel == AccessLevel.AccessLevelFB) { cmd = 0x0000000000FB2702; // request security access } CANMessage msg = new CANMessage(0x11, 0, 3); //<GS-18052011> ELM327 support requires the length byte msg.setData(cmd); m_canListener.setupWaitMessage(0x311); CastInfoEvent("Requesting security access", ActivityType.ConvertingFile); if (!canUsbDevice.sendMessage(msg)) { Console.WriteLine("Couldn't send message"); } CANMessage response = new CANMessage(); response = m_canListener.waitMessage(1000); //ulong data = response.getData(); Console.WriteLine("---" + response.getData().ToString("X16")); if (response.getCanData(1) == 0x67) { if (response.getCanData(2) == 0xFD || response.getCanData(2) == 0xFB || response.getCanData(2) == 0x01) { CastInfoEvent("Got seed value from ECU", ActivityType.ConvertingFile); while (secondsToWait > 0) { CastInfoEvent("Waiting for " + secondsToWait.ToString() + " seconds...", ActivityType.UploadingBootloader); Thread.Sleep(1000); SendKeepAlive(); secondsToWait--; } byte[] seed = new byte[2]; seed[0] = response.getCanData(3); seed[1] = response.getCanData(4); if (seed[0] == 0x00 && seed[1] == 0x00) { return true; // security access was already granted } else { SeedToKey s2k = new SeedToKey(); byte[] key = s2k.calculateKey(seed, _securityLevel); CastInfoEvent("Security access : Key (" + key[0].ToString("X2") + key[1].ToString("X2") + ") calculated from seed (" + seed[0].ToString("X2") + seed[1].ToString("X2") + ")", ActivityType.ConvertingFile); ulong keydata = 0x0000000000FE2704; if (_securityLevel == AccessLevel.AccessLevel01) { keydata = 0x0000000000022704; } else if (_securityLevel == AccessLevel.AccessLevelFB) { keydata = 0x0000000000FC2704; } ulong key1 = key[1]; key1 *= 0x100000000; keydata ^= key1; ulong key2 = key[0]; key2 *= 0x1000000; keydata ^= key2; msg = new CANMessage(0x11, 0, 5);//<GS-18052011> ELM327 support requires the length byte msg.setData(keydata); m_canListener.setupWaitMessage(0x311); if (!canUsbDevice.sendMessage(msg)) { Console.WriteLine("Couldn't send message"); } response = new CANMessage(); response = m_canListener.waitMessage(1000); // is it ok or not if (response.getCanData(1) == 0x67 && (response.getCanData(2) == 0xFE || response.getCanData(2) == 0xFC || response.getCanData(2) == 0x02)) { CastInfoEvent("Security access granted", ActivityType.ConvertingFile); return true; } } } else if (response.getCanData(2) == 0xFE || response.getCanData(2) == 0x02) { CastInfoEvent("Security access granted", ActivityType.ConvertingFile); return true; } } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27) { Console.WriteLine("Casting error"); string info = TranslateErrorCode(response.getCanData(3)); Console.WriteLine("Casting error: " + info); CastInfoEvent("Error: " + info, ActivityType.ConvertingFile); } return false; }
public override bool sendMessage(CANMessage a_message) { string sendString = "t"; sendString += a_message.getID().ToString("X3"); sendString += a_message.getLength().ToString("X1"); for (uint i = 0; i < a_message.getLength(); i++) // leave out the length field, the ELM chip assigns that for us { sendString += a_message.getCanData(i).ToString("X2"); } sendString += "\r"; if (m_serialPort.IsOpen) { AddToCanTrace("TX: " + a_message.getID().ToString("X3") + " " + a_message.getLength().ToString("X1") + " " + a_message.getData().ToString("X16")); m_serialPort.Write(sendString); //Console.WriteLine("TX: " + sendString); } // bitrate = 38400bps -> 3840 bytes per second // sending each byte will take 0.2 ms approx //Thread.Sleep(a_message.getLength()); // sleep length ms // Thread.Sleep(10); Thread.Sleep(1); return true; // remove after implementation }
public byte[] RequestECUInfo0101(uint _pid) { byte[] retval = new byte[2]; byte[] rx_buffer = new byte[128]; int rx_pnt = 0; if (canUsbDevice.isOpen()) { ulong cmd = 0x0000000000001A02 | _pid << 16; //SendMessage(data); // software version CANMessage msg = new CANMessage(0x11, 0, 3); //<GS-18052011> support for ELM requires length byte msg.setData(cmd); m_canListener.setupWaitMessage(0x7E8); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return retval; } int msgcnt = 0; bool _success = false; CANMessage response = new CANMessage(); ulong data = 0; while (!_success && msgcnt < 2) { response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); data = response.getData(); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x1A && response.getCanData(3) == 0x78) { } //RequestCorrectlyReceived-ResponsePending else if (response.getCanData(1) != 0x7E) { _success = true; } msgcnt++; } //CANMessage response = new CANMessage(); //response = m_canListener.waitMessage(timeoutPTct); //ulong data = response.getData(); if (response.getCanData(1) == 0x5A) { // only one frame in this repsonse for (uint fi = 3; fi < 8; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi); retval = new byte[rx_pnt]; for (int i = 0; i < rx_pnt; i++) retval[i] = rx_buffer[i]; } else if (response.getCanData(2) == 0x5A) { SendAckMessageT8(); byte len = response.getCanData(1); int m_nrFrameToReceive = ((len - 4) / 8); if ((len - 4) % 8 > 0) m_nrFrameToReceive++; int lenthisFrame = len; if (lenthisFrame > 4) lenthisFrame = 4; for (uint fi = 4; fi < 4 + lenthisFrame; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi); // wait for more records now while (m_nrFrameToReceive > 0) { m_canListener.setupWaitMessage(0x7E8); response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x1A && response.getCanData(3) == 0x78) { } //RequestCorrectlyReceived-ResponsePending else if (response.getCanData(1) != 0x7E) { m_nrFrameToReceive--; data = response.getData(); // add the bytes to the receive buffer for (uint fi = 1; fi < 8; fi++) { if (rx_pnt < rx_buffer.Length) // prevent overrun { rx_buffer[rx_pnt++] = response.getCanData(fi); } } } } retval = new byte[rx_pnt]; for (int i = 0; i < rx_pnt; i++) retval[i] = rx_buffer[i]; } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27) { CastInfoEvent("Error: " + TranslateErrorCode(response.getCanData(3)), ActivityType.ConvertingFile); } } Thread.Sleep(5); return retval; }
public bool SetVIN(string vin) { bool retval = false; // 62 DPID + 01 sendOneResponse + $AA ReadDataByPacketIdentifier CANMessage msg62 = new CANMessage(0x7E0, 0, 4); msg62.setData(0x000000006201AA03); m_canListener.setupWaitMessage(0x5E8); CastInfoEvent("Wait for response 5E8 62 00 00", ActivityType.ConvertingFile); if (!canUsbDevice.sendMessage(msg62)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return false; } CANMessage response62 = new CANMessage(); response62 = m_canListener.waitMessage(timeoutP2ct); Console.WriteLine("---" + response62.getData().ToString("X16")); //05E8 62 00 00 02 A7 01 7F 01 if (response62.getCanData(0) == 0x62) { if (response62.getCanData(1) == 0x00) { if (response62.getCanData(2) == 0x00) { CastInfoEvent("Got response 5E8 62 00 00", ActivityType.ConvertingFile); } } } if (GetManufacturersEnableCounter() == 0x00) CastInfoEvent("GetManufacturersEnableCounter == 0x00", ActivityType.ConvertingFile); CastInfoEvent("ECM EOL Parameter Settings-part1", ActivityType.ConvertingFile); // 02 DPID + 01 sendOneResponse + $AA ReadDataByPacketIdentifier CANMessage msg = new CANMessage(0x7E0, 0, 4); msg.setData(0x000000000201AA03); m_canListener.setupWaitMessage(0x5E8); CastInfoEvent("Wait for response 5E8 02 02", ActivityType.ConvertingFile); if (!canUsbDevice.sendMessage(msg)) { CastInfoEvent("Couldn't send message", ActivityType.ConvertingFile); return false; } CANMessage response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); Console.WriteLine("---" + response.getData().ToString("X16")); //05E8 02 02 A0 42 80 A0 00 00 if (response.getCanData(0) == 0x02) { if (response.getCanData(1) == 0x02) { CastInfoEvent("Got response 5E8 02 02", ActivityType.ConvertingFile); } } retval = ProgramVIN(vin); Thread.Sleep(200); // Persist RequestSecurityAccess(0); SendDeviceControlMessage(0x16); return retval; }
protected override bool sendMessageDevice(CANMessage a_message) { lock (lockObj) { sendDataSempahore.WaitOne(timeoutWithoutReadyChar); interfaceBusy = true; if (a_message.getID() != _ECUAddress) { _ECUAddress = a_message.getID(); string command = "ATSH" + a_message.getID().ToString("X3") + "\r"; SendControlMessage(command, false); } //check if it is beneficial to send ATR0 and ATR1 for ELM clones if (!supports8ByteResponse) { if ((a_message.elmExpectedResponses == 0) != request0Responses) { if (a_message.elmExpectedResponses == 0) { SendControlMessage("ATR0\r", false); request0Responses = true; } else { SendControlMessage("ATR1\r", false); request0Responses = false; } } } lastSentCanMessage = a_message.Clone(); string sendString = GetELMRequest(a_message); if (sendString.Length == 16 && !supports8ByteResponse) { bool canIgnoreLastByte = a_message.getCanData(7) == 0; if (canIgnoreLastByte) { sendString = sendString.Substring(0, 14); } } if (sendString.Length <= 14 || supports8ByteResponse) //ELM 2.0 supports 8 bytes + response count, previous versions dont { //add expected responses, but this has to be one char only :( if (a_message.elmExpectedResponses != -1 && a_message.elmExpectedResponses < 16) { sendString += " " + a_message.elmExpectedResponses.ToString("X1"); } } sendString += "\r"; if (m_serialPort.IsOpen) { lastSentTimestamp = Environment.TickCount; WriteToSerialWithTrace(sendString); } return(true); // remove after implementation } }
public string[] readDTCCodes() { // test code //ulong c = 0x0000006F00070181;//81 01 07 00 6F 00 00 00 //ulong c = 0x000000FD00220181; //81 01 22 00 FD 00 00 00 //CANMessage test = new CANMessage(); //test.setData(c); //AddToCanTrace(GetDtcDescription(test)); // send message to read DTC StartSession10(); List<string> list = new List<string>(); // ReadDiagnosticInformation $A9 Service // readStatusOfDTCByStatusMask $81 Request // DTCStatusMask $12= 0001 0010 // 0 Bit 7 warningIndicatorRequestedState // 0 Bit 6 currentDTCSincePowerUp // 0 Bit 5 testNotPassedSinceCurrentPowerUp // 1 Bit 4 historyDTC // 0 Bit 3 testFailedSinceDTCCleared // 0 Bit 2 testNotPassedSinceDTCCleared // 1 Bit 1 currentDTC // 0 Bit 0 DTCSupportedByCalibration ulong cmd = 0x000000001281A903; // 7E0 03 A9 81 12 00 00 00 00 CANMessage msg = new CANMessage(0x7E0, 0, 4);//<GS-18052011> ELM327 support requires the length byte msg.setData(cmd); msg.elmExpectedResponses = 15; m_canListener.setupWaitMessage(0x7E8); canUsbDevice.SetupCANFilter("7E8", "DFF"); // Mask will allow 7E8 and 5E8 if (!canUsbDevice.sendMessage(msg)) { Console.WriteLine("Couldn't send message"); } CANMessage response = new CANMessage(); ulong data = 0; // Wait for response // 7E8 03 7F A9 78 00 00 00 00 response = m_canListener.waitMessage(1000); data = response.getData(); if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xA9 && response.getCanData(3) == 0x78) // RequestCorrectlyReceived-ResponsePending ($78, RC_RCR-RP) { // Now wait for all DTCs m_canListener.setupWaitMessage(0x5E8); bool more_errors = true; while (more_errors) { CANMessage responseDTC = new CANMessage(); responseDTC = m_canListener.waitMessage(1000); // Read until response: EndOfDTCReport if (responseDTC.getCanData(1) == 0 && responseDTC.getCanData(2) == 0 && responseDTC.getCanData(3) == 0) { more_errors = false; list.Add("No more errors!"); } else { string dtcDescription = GetDtcDescription(responseDTC); AddToCanTrace(dtcDescription); list.Add(dtcDescription); } } } else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xA9) { string info = TranslateErrorCode(response.getCanData(3)); CastInfoEvent("Error: " + info, ActivityType.ConvertingFile); } Send0120(); return list.ToArray(); }