Skip to content

Commit

Permalink
Tweak mock dtc
Browse files Browse the repository at this point in the history
  • Loading branch information
kkonteh97 committed Mar 28, 2024
1 parent 6d98750 commit 62e98b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 20 additions & 3 deletions Sources/SwiftOBD2/Communication/mockManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,27 @@ class MOCKComm: CommProtocol {

} else if command == "03" {
// 03 is a request for DTCs
let dtc = ["P0301"]
let dtcs = ["P0104", "U0207"]
var response = ""
// convert to hex
let hexDTC = dtc.map { $0.utf8.map { String(format: "%02X", $0) }.joined() }
let response = header + " 02 " + hexDTC.joined(separator: " ") + "00"
for dtc in dtcs {
var hexString = String(dtc.suffix(4))
// 2 by 2
hexString = hexString.chunked(by: 2).joined(separator: " ")
response += hexString
print("hexString", hexString)
}
if ecuSettings.headerOn {
header = "7E8"
}
let mode = "43"
response = mode + " " + response
let length = String(format: "%02X", response.count / 3 + 1)
response = header + " " + length + " " + response
while response.count < 28 {
response.append(" 00")
}
print("response", response)
return [response]
} else {
guard var response = OBDCommand.mockResponse(forCommand: command) else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftOBD2/elm327.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class ELM327 {
let statusResponse = try await sendCommand(statusCommand.properties.command)
let statueMessages = OBDParcer(statusResponse, idBits: obdProtocol.idBits)?.messages

guard let statusData = statueMessages?[0].data,
guard let statusData = statueMessages?.first?.data,
let decodedStatus = statusCommand.properties.decode(data: statusData)
else {
return nil
Expand All @@ -263,7 +263,7 @@ class ELM327 {

let dtcMessages = OBDParcer(dtcResponse, idBits: obdProtocol.idBits)?.messages

guard let dtcData = dtcMessages?[0].data else {
guard let dtcData = dtcMessages?.first?.data else {
return []
}
guard let decodedDtc = dtcCommand.properties.decode(data: dtcData) else {
Expand Down

0 comments on commit 62e98b3

Please sign in to comment.