Last active
April 22, 2022 21:24
-
-
Save brentpicasso/6a79b853d69c93c821a3532d2797976b to your computer and use it in GitHub Desktop.
Simple CAN bus analyzer for RaceCapture using Lua scripting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Simple CAN bus analyzer | |
-- This script is useful for reverse engineering CAN bus data | |
-- and detecting patterns. | |
-- | |
-- Up to 100 individual 8 bit channels can be created. Additional channels will not be tracked. | |
-- CAN bus data will appear in the data stream with the channel name: <CAN ID>_<OFFSET> | |
-- Once script is running, you can monitor the data under Dashboard / Raw Channels View. | |
---------------------------------------- | |
--Configuration | |
--specify CAN bus; 0 if CAN1, 1 if CAN2 | |
can_bus = 0 | |
--------------------------------------- | |
channel_count = 0 | |
channels = {} | |
setTickRate(1000) | |
-- update the current channel value, | |
-- creating the channel as needed | |
function updateChannel(name, value) | |
local cId = channels[name] | |
if cId == nil then | |
if channel_count >= 100 then | |
println('too many channels, could not add ' ..name) | |
return | |
end | |
println('Adding new channel ' ..name) | |
cId = addChannel(name, 10, 0, 0, 255) | |
channel_count = channel_count + 1 | |
if cId ~= nil then | |
-- stash the channel Id for next time | |
channels[name] = cId | |
else | |
println('Could not add channel: ' ..name) | |
end | |
end | |
setChannel(cId, value) | |
end | |
-- strips the trailing '.0' from formatted numbers | |
function fmtInt(val) | |
return string.sub(val,1,-3) | |
end | |
-- loops through a CAN message, updating individual | |
-- channels for each byte | |
function updateCAN(id, data) | |
for i=1, #data do | |
updateChannel(fmtInt(id) ..'_' ..fmtInt((i-1)), data[i]) | |
end | |
end | |
-- receive and process a CAN message | |
function processCAN() | |
local id, ext, data = rxCAN(can_bus) | |
if id ~= nil then updateCAN(id, data) end | |
end | |
function onTick() | |
processCAN() | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment