Forum

Notifications
Clear all

BMW x5 50e SoC

1 Posts
1 Users
0 Reactions
125 Views
(@tomelectronics)
Member Admin
Joined: 3 months ago
Posts: 4
Topic starter   [#22]

Example on getting precise State of Charge (SoC) data from the Battery Management System (SME) on a BMW X5 50e (G-Series).

Notes:

  • Addressing: If you get a timeout on 0x792/0x79A, your specific model/firmware might use different IDs. Try sniffing the bus while the car is active to verify your transmission IDs.

  • Safety: Always ensure the vehicle is in a safe state. Do not run active diagnostic scripts while the vehicle is in motion.

Below is the Lua script:

-- CAN Init (Best Practice)
local rc, currentBR = can.getCurrentBaudrate()
if (currentBR == 0) then
    currentBR = ui.inputValue('Set CAN Baudrate', 'u', 1, 1000000, 500000)

    if (currentBR == nil) then error('No baudrate was given, script terminated.') end
end

rc = can.init(currentBR, true) -- forceActive = true for secutiry
if (rc ~= 0) then
    error("CAN Init failed! Error: " .. rc)
end

local RID = 0x792
local LID = 0x79A
local DID = 0x4170
local timeout = 100

-- 1. Extended session request
rc = uds.xmit(RID, 0x10, 0x03, "", LID, 50, true)
if (rc ~= 0) then
    error("Extended session failed! Error: " .. rc)
end

-- 2. Little pause for the ECU to enter state
ui.sleep(10)

-- Tester Present
rc = uds.xmit(RID, 0x3E, 0x00, "", LID, 50, true)
if (rc ~= 0) then
    error("Tester Present failed! Error: " .. rc)
end

-- 3. SoC request
rc, data = uds.xmit(RID, 0x22, DID, "", LID, timeout, true)

if rc == 0 then
    local soc_val = string.byte(data, 1)
    print("SoC: " .. tostring(soc_val) .. "%")
else
    print("SoC failed! Error: " .. tostring(rc))
end

can.uninit()

This topic was modified 1 month ago 3 times by TomElectronics

   
Quote
Share: