obd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,10 @@
1
+ require "serialport"
2
+
3
+ require_relative "obd/command"
4
+ require_relative "obd/connection"
5
+
6
+ module OBD
7
+ def self.connect *args
8
+ self::Connection.new(*args)
9
+ end
10
+ end
@@ -0,0 +1,73 @@
1
+ module OBD
2
+ class Command
3
+
4
+ def initialize
5
+
6
+ end
7
+
8
+ def self.format_result command, result
9
+ if is_command?(command) && result != "NO DATA"
10
+ pids[command.to_sym].call h(result)
11
+ else
12
+ result
13
+ end
14
+ end
15
+
16
+ def self.to_hex command
17
+ if is_command? command
18
+ "01%02x" % pids.keys.index(command.to_sym)
19
+ else
20
+ command
21
+ end
22
+ end
23
+
24
+ def self.is_command? command
25
+ pids.keys.include? command.to_sym
26
+ end
27
+
28
+ def self.pids
29
+ {
30
+ pids_supported_1: lambda {|x| x.to_s(2).split('').each_with_index.map{|b,i| pids.keys[i] if b == '1'}},
31
+ monitor_status_since_clear: lambda {|x| x.to_s},
32
+ freeze_dtc: lambda {|x| x.to_s},
33
+ fuel_system_status: lambda {|x| x.to_s},
34
+ calculated_engine_load: lambda {|x| "%0.2f" % (x * 100.0 / 255.0) + '%'},
35
+ engine_coolent_temperature: lambda {|x| "%0.2f" % (x * 1.8 - 104) + '*F'},
36
+ short_term_fuel_trim_bank_1: lambda {|x| "%0.2f" % (x * 0.78125 - 100) + '%'},
37
+ long_term_fuel_trim_bank_1: lambda {|x| "%0.2f" % (x * 0.78125 - 100) + '%'},
38
+ short_term_fuel_trim_bank_2: lambda {|x| "%0.2f" % (x * 0.78125 - 100) + '%'},
39
+ long_term_fuel_trim_bank_2: lambda {|x| "%0.2f" % (x * 0.78125 - 100) + '%'},
40
+ fuel_pressure: lambda {|x| "%0.2f" % (x * 3 * 0.145) + 'psi'},
41
+ intake_manifold_absolute_pressure: lambda {|x| "%0.2f" % (x * 0.145) + 'psi'},
42
+ engine_rpm: lambda {|x| "%0.2f" % (x / 4.0) + 'rpm'},
43
+ vehicle_speed: lambda {|x| "%0.2f" % (x * 0.621371192) + 'mph'},
44
+ timing_advance: lambda {|x| "%0.2f" % (x / 2.0 - 64) + '*'},
45
+ intake_air_temperature: lambda {|x| "%0.2f" % (x * 1.8 - 104) + '*F'},
46
+ maf_air_flow_rate: lambda {|x| "%0.2f" % (x / 100.0) + 'grams/sec'},
47
+ throttle_position: lambda {|x| "%0.2f" % (x * 100 / 255.0) + '%'},
48
+ commanded_secondary_air_status: lambda {|x| x}, # bit encoded
49
+ oxygen_sensors_present: lambda {|x| x}, # [A0..A3] == Bank 1,Sensors 1-4.[A4..A7]
50
+ bank_1_sensor_1_oxygen_sensor_voltage: lambda {|x| x},
51
+ bank_1_sensor_2_oxygen_sensor_voltage: lambda {|x| x},
52
+ bank_1_sensor_3_oxygen_sensor_voltage: lambda {|x| x},
53
+ bank_1_sensor_4_oxygen_sensor_voltage: lambda {|x| x},
54
+ bank_2_sensor_1_oxygen_sensor_voltage: lambda {|x| x},
55
+ bank_2_sensor_2_oxygen_sensor_voltage: lambda {|x| x},
56
+ bank_2_sensor_3_oxygen_sensor_voltage: lambda {|x| x},
57
+ bank_2_sensor_4_oxygen_sensor_voltage: lambda {|x| x},
58
+ obd_standards_vehicle_conforms_to: lambda {|x| x}, # bit encoded
59
+ oxygen_sensors_present_2: lambda {|x| x}, # complicated...
60
+ aux_input_status: lambda {|x| (x == 1).inspect}, # Power Take Off (PTO) status is active?
61
+ run_time_since_engine_start: lambda {|x| x}, # seconds
62
+ pids_supported_2: lambda {|x| x.to_s(2).split('').each_with_index.map{|b,i| pids.keys[i+33] if b == '1'}}, # bit encoded
63
+ distance_traveled_with_mil_on: lambda {|x| x.to_s + 'km'}
64
+ }
65
+ end
66
+
67
+ def self.h response
68
+ response[4..-1].to_i(16)
69
+ end
70
+
71
+
72
+ end
73
+ end
@@ -0,0 +1,44 @@
1
+ module OBD
2
+ class Connection
3
+ def initialize port, baud = 9600
4
+ @port = port
5
+ @baud = baud
6
+
7
+ connect
8
+ end
9
+
10
+ def voltage
11
+ send("AT RV")
12
+ end
13
+
14
+ def connect
15
+ @serial_port = SerialPort.new @port, @baud # , data_bits: 8, stop_bits: 1, parity: SerialPort::NONE
16
+ @serial_port.read_timeout = 5000
17
+ read
18
+ send("AT E0") # turn echo off
19
+ send("AT L0") # turn linefeeds off
20
+ send("AT S0") # turn spaces off
21
+ send("AT AT2") # respond to commands faster
22
+ send("AT SP 00") # automatically select protocol
23
+ end
24
+
25
+ def [] command
26
+ OBD::Command.format_result(command, send(OBD::Command.to_hex(command)))
27
+ end
28
+
29
+ def send data
30
+ write data
31
+ read
32
+ end
33
+
34
+ private
35
+
36
+ def read
37
+ return @serial_port.gets("\r\r>").to_s.chomp("\r\r>")
38
+ end
39
+
40
+ def write data
41
+ @serial_port.write data.to_s + "\r"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,18 @@
1
+ module OBD
2
+ class Response
3
+ attr_accessor :type
4
+
5
+ def initialize _hex_response, last_command
6
+ @hex_response = _hex_response
7
+ end
8
+
9
+ def to_s
10
+ value.to_s
11
+ end
12
+
13
+ def value
14
+ @hex_response.[4..-1]
15
+ end
16
+
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: obd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Peterson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: serialport
16
+ requirement: &70153807947280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70153807947280
25
+ description: A Ruby API that interfaces with ELM327 compatible devices over serial.
26
+ email: jeff@petersonj.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - README.markdown
32
+ - lib/obd.rb
33
+ - lib/obd/command.rb
34
+ - lib/obd/connection.rb
35
+ - lib/obd/response.rb
36
+ homepage: http://petersonj.com/obd
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.13
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: A Ruby OBD-II API
60
+ test_files: []