This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

Lua J1939 filters are not working on Win10

0

ON a MAC, I am using Wireshark (tshark.exe) to do som CAN - J1939 filtering. I have done it with a script in LUA.

This is working fine.

But when I try it in Win10, I have problems by doing the field.new on a j1939

This is part of my LUA file.

j1939_src = Field.new("j1939.src_addr")
src = tostring(j1939_src())

In the line:

src = tostring(j1939_src())

I get the error

tshark: Lua: on packet 1 Error During execution of Listener Packet Callback:
[string "scan_varids.lua"]:58: bad argument #1 to 'tostring' (value expected)

asked 19 Apr '17, 07:55

Klaus%20Jakobsen's gravatar image

Klaus Jakobsen
6112
accept rate: 0%

edited 04 May '17, 06:38

grahamb's gravatar image

grahamb ♦
19.8k330206

1

While this may not be your issue, when comparing two different builds of Wireshark it's helpful to state the version numbers of the two versions.

(19 Apr '17, 10:59) grahamb ♦

On my MAC it is Wireshark 2.2.1. On my Win10 I tried initially with newest (2.2.6) but to be sure I also tried the 2.2.1 on my Win10. It is the same. Both win installations are not working ane are giving the same error.

(19 Apr '17, 23:58) Klaus Jakobsen
1

Can you post the full contents of your Lua script.

(20 Apr '17, 00:35) grahamb ♦
--Househam boom LHS (PWM in Test & dianostic)
filter_test = { "master volt", "slave volt", "tractor volt", "fule level", "rpm inc", "speed", "dis count", "max gear", "trans type", "engine houers on tractor", "break press", "RPM", "test speed" }
filter_varids = { 284, 285, 286, 201, 105, 1, 44, 807, 1153, 321, 1204, 5, 256 }

– Udvælg J1939 felter tap = Listener.new("frame") frame_time = Field.new("frame.time_relative") j1939_src = Field.new("j1939.src_addr") j1939_dst = Field.new("j1939.dst_addr") j1939_canid = Field.new("j1939.can_id") j1939_pgn = Field.new("j1939.pgn") j1939_data = Field.new("j1939.data")

– Udskriv CSV header og beregn kolonnenummer for hver varid varids_to_columns = {} column_count = 0

io.write("Time;") for i, varid in ipairs(filter_varids) do column_count = column_count + 1 varids_to_columns[varid] = column_count io.write(filter_test[i] .. " (" .. varid .. ");") end io.write("\n")

– Gennemgå alle pakkerne function tap.packet (pinfo,tvb,ip) time = tostring(frame_time()) src = tostring(j1939_src()) canid = tostring(j1939_canid()) pgn = tostring(j1939_pgn()) data = tostring(j1939_data()):gsub(":", ""):fromhex() var_id = 0; var_value = 0;

if pgn == "126720" or pgn == "61184" then – Proprietary A or A2 if u8(data,0) == 0x25 then – VarID var_id = u16(data, 2) if var_id >3900 then – if test VAR_ids var_value = u16(data, 6) else var_value = s32(data, 4) end

  -- Check om varid'en er en af dem vi skal kigge på
  if varids_to_columns[var_id] then
      io.write(time:gsub("%.", ",")) -- Erstat punktum med komma i tidsstempel for at Excel forstår det
      -- Udskriv semikolonner indtil vi når den kolonne, vi skal have værdien ind på
      for i=1, varids_to_columns[var_id], 1 do
  io.write( ";" )
      end
io.write( var_value .. "\n" )
    end
end

end

– if src == "9" then – Vi ser kun på beskeder fra CAN-ID 9 – io.write( "VarID: \n" ) – end end

(20 Apr ‘17, 01:05) Klaus Jakobsen
– DIVERSE HJÆLPEFUNKTIONER
function string.fromhex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end

function string.tohex(str) return (str:gsub('.', function (c) return string.format('%02X', string.byte(c)) end)) end

— Get an 8-bit integer at a 0-based byte offset in a byte string. – @param b A byte string. – @param i Offset. – @return An 8-bit integer. function u8(b, i) return string.byte(b, i+1) end

— Get a 16-bit integer at a 0-based byte offset in a byte string. – @param b A byte string. – @param i Offset. – @return A 16-bit integer. function u16(b, i) local b1,b2 b1, b2 = string.byte(b, i+1), string.byte(b, i+2) – 2^8 2^0 return b1*256 + b2 end

— Get a 32-bit integer at a 0-based byte offset in a byte string. – @param b A byte string. – @param i Offset. – @return A 32-bit integer. function u32(b,i) local b1,b2,b3,b4 b1, b2 = string.byte(b, i+1), string.byte(b, i+2) b3, b4 = string.byte(b, i+3), string.byte(b, i+4) – 2^24 2^16 2^8 2^0 return b116777216 + b265536 + b3*256 + b4 end

— Get a 32-bit integer at a 0-based byte offset in a byte string. – @param b A byte string. – @param i Offset. – @return A 32-bit signed integer. function s32(b,i) local b1,b2,b3,b4 b1, b2 = string.byte(b, i+1), string.byte(b, i+2) b3, b4 = string.byte(b, i+3), string.byte(b, i+4) – 2^24 2^16 2^8 2^0 b = b116777216 + b265536 + b3256 + b4 if b >= 2^31 then return b - 2^32 else return b end end

(20 Apr ‘17, 01:06) Klaus Jakobsen

This is not nice. Is it possible to add a file instead

(20 Apr ‘17, 01:07) Klaus Jakobsen

Use the <pre></pre> markup around code.

(20 Apr ‘17, 03:47) grahamb ♦

First part of the file:

– Kommandoer til at starte programmet med:
– tshark -i Househam_boom_reg_canlog/010-1770571252_bus_1.pcap -X lua_script:hej.lua
– tshark -q -c10 -r 010-1770571252_bus_1.pcap -X lua_script:hej.lua
– tshark -q -r 010-1770571252_bus_1.pcap -X lua_script:hej.lua > output.csv
– VarID'er som skal trækkes frem:
–Househam boom LHS (PWM in Test & dianostic)
filter_test = { "master volt", "slave volt", "tractor volt", "fule level", "rpm inc", "speed", "dis count", "max gear", "trans type", "engine houers on tractor", "break press", "RPM", "test speed" }
filter_varids = { 284, 285, 286, 201, 105, 1, 44, 807, 1153, 321, 1204, 5, 256 }
– Udvælg J1939 felter
tap = Listener.new("frame")
frame_time = Field.new("frame.time_relative")
j1939_src = Field.new("j1939.src_addr")
j1939_dst = Field.new("j1939.dst_addr")
j1939_canid = Field.new("j1939.can_id")
j1939_pgn = Field.new("j1939.pgn")
j1939_data = Field.new("j1939.data")
– Udskriv CSV header og beregn kolonnenummer for hver varid
varids_to_columns = {}
column_count = 0
io.write("Time;")
for i, varid in ipairs(filter_varids) do
column_count = column_count + 1
varids_to_columns[varid] = column_count
io.write(filter_test[i] .. " (" .. varid .. ");")
end
io.write("\n")
– Gennemgå alle pakkerne
function tap.packet (pinfo,tvb,ip)
time = tostring(frame_time())
src = tostring(j1939_src())
canid = tostring(j1939_canid())
pgn = tostring(j1939_pgn())
data = tostring(j1939_data()):gsub(":", ""):fromhex()
var_id = 0;
var_value = 0;
if pgn == "126720" or pgn == "61184" then – Proprietary A or A2
if u8(data,0) == 0x25 then – VarID
var_id = u16(data, 2)
if var_id >3900 then  – if test VAR_ids
var_value = u16(data, 6)
else
var_value = s32(data, 4)
end
– Check om varid'en er en af dem vi skal kigge på
if varids_to_columns[var_id] then
io.write(time:gsub("%.", ",")) – Erstat punktum med komma i tidsstempel for at Excel forstår det
– Udskriv semikolonner indtil vi når den kolonne, vi skal have værdien ind på
for i=1, varids_to_columns[var_id], 1 do
io.write( ";" )
end
io.write( var_value .. "\n" )
end
end

end

– if src == “9” then – Vi ser kun på beskeder fra CAN-ID 9 – io.write( “VarID: \n” ) – end end

(20 Apr ‘17, 03:54) Klaus Jakobsen

Second part of the file

– DIVERSE HJÆLPEFUNKTIONER
function string.fromhex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
function string.tohex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
— Get an 8-bit integer at a 0-based byte offset in a byte string.
– @param b A byte string.
– @param i Offset.
– @return An 8-bit integer.
function u8(b, i)
return string.byte(b, i+1)
end
— Get a 16-bit integer at a 0-based byte offset in a byte string.
– @param b A byte string.
– @param i Offset.
– @return A 16-bit integer.
function u16(b, i)
local b1,b2
b1, b2 = string.byte(b, i+1), string.byte(b, i+2)
–        2^8     2^0
return b1256 + b2
end
— Get a 32-bit integer at a 0-based byte offset in a byte string.
– @param b A byte string.
– @param i Offset.
– @return A 32-bit integer.
function u32(b,i)
local b1,b2,b3,b4
b1, b2 = string.byte(b, i+1), string.byte(b, i+2)
b3, b4 = string.byte(b, i+3), string.byte(b, i+4)
–        2^24          2^16       2^8     2^0
return b116777216 + b265536 + b3256 + b4
end
— Get a 32-bit integer at a 0-based byte offset in a byte string.
– @param b A byte string.
– @param i Offset.
– @return A 32-bit signed integer.
function s32(b,i)
local b1,b2,b3,b4
b1, b2 = string.byte(b, i+1), string.byte(b, i+2)
b3, b4 = string.byte(b, i+3), string.byte(b, i+4)
–        2^24          2^16       2^8     2^0
b = b116777216 + b265536 + b3256 + b4
if b >= 2^31 then
return b - 2^32
else
return b
end
end
(20 Apr ‘17, 03:55) Klaus Jakobsen

I am also facing a similar issue with Field.new. LUA script was working in previous wireshark builds, but after updating to 1.12.7 for win7 its now failing. I have checked this field and it does exist. Whats changed?

[string “GSMMAP.lua”]:244: bad argument #1 to ‘?’ (Field_new: a field with this name must exist)

tcap_abort = Field.new(“tcap.abort_element”) — line 244

(20 Apr ‘17, 05:37) niismail

Hi there, would you be able to share a sample capture of the J1939 data?

(03 May ‘17, 06:22) mfcss

Hi WIreshark Q&A I have placed a fil in dropbox:

https://www.dropbox.com/s/oxmxnkyefw2tcs9/010-2129680398_bus_1.pcap?dl=0

(04 May ‘17, 01:36) Klaus Jakobsen

Thanks Klaus - in case you happen to have a sample version of the file including some of the more standard/non proprietary PGN data (e.g. wheel speed, RPM, …) that would be interesting. But otherwise appreciate the sharing of above.

(04 May ‘17, 04:47) mfcss
showing 5 of 13 show 8 more comments