Skip to content

Requesting PIDs issues #45

@sabiland

Description

@sabiland

I get this for requesting (one of the) PIDs:

7EA0641208001A001\r7E80641209015B015\r\r>

But the library I use (iOS Swift) for OBD 2, has this:

var cleanedHex: String 
{
   let hex = self.uppercased().filter { "0123456789ABCDEF".contains($0) }
   return hex.count % 2 == 0 ? hex : String(hex.dropLast())  // ⚠️ drop odd trailing char
}

Which returns this (note: last character is now trimmmed):

Printing description of obdLines:
▿ 2 elements
  - 0 : "7EA0641208001A00"
  - 1 : "7E80641209015B01"

And Frame parsing fails. Any idea? ChatGpt told me, that ELM327-emulator is producing wrong "values"?

It fails here (I am still totally not familiar with OBD 2, so I cannot see yet, what is wrong):

struct Frame {
  var raw: String
  var data = Data()
  var priority: UInt8
  var addrMode: UInt8
  var rxID: UInt8
  var txID: ECUID
  var type: FrameType
  var seqIndex: UInt8 = 0  // Only used when type = CF
  var dataLen: UInt8?

  init(raw: String, idBits: Int) throws {
    self.raw = raw

    let paddedRawData = idBits == 11 ? "00000" + raw : raw

    let dataBytes = paddedRawData.hexBytes

    data = Data(dataBytes.dropFirst(4))

    guard dataBytes.count >= 6, dataBytes.count <= 12 else {
      print(
        "invalid frame size",
        dataBytes.compactMap { String(format: "%02X", $0) }.joined(
          separator: " "
        )
      )
      throw ParserError.error("Invalid frame size")
    }

    guard let dataType = data.first,
      let type = FrameType(rawValue: dataType & 0xF0)
    else {
      print(
        "invalid frame type",
        dataBytes.compactMap { String(format: "%02X", $0) }
      )
      throw ParserError.error("Invalid frame type")
    }

    priority = dataBytes[2] & 0x0F
    addrMode = dataBytes[3] & 0xF0
    rxID = dataBytes[2]
    txID = ECUID(rawValue: dataBytes[3] & 0x07) ?? .unknown
    self.type = type

    switch type {
    case .singleFrame:
      dataLen = (data[0] & 0x0F)
    case .firstFrame:
      dataLen = ((UInt8(data[0] & 0x0F) << 8) + UInt8(data[1]))
    case .consecutiveFrame:
      seqIndex = data[0] & 0x0F
    }
  }
}

ChatGpt reasoning (I cannot tell if true or not, yet):

TL;DR — Emulator Bug vs Real OBD
	•	The VIN response works because it’s a nicely packed, multi-frame response and your parser can tolerate some quirks.
	•	The PID responses from the emulator are incorrectly formatted or don’t follow ISO-TP, so your Frame parser rightfully throws errors.
	•	The emulator is not truly standards-compliant, especially for Mode 01 responses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions