pcanusb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/lib/PCAN_USB.rb +158 -0
  2. data/lib/Pcan_usb.dll +0 -0
  3. data/rakefile +55 -0
  4. metadata +48 -0
@@ -0,0 +1,158 @@
1
+ =begin
2
+ This class is a wrapper for the PCAN USB DLL
3
+ =end
4
+
5
+ require "dl"
6
+ require "dl/import"
7
+ require "dl/struct"
8
+
9
+ class PCAN_USB
10
+
11
+ # BAUD rates used by "init"
12
+ BAUD_1M = 0x0014
13
+ BAUD_500K = 0x001C
14
+ BAUD_250K = 0x011C
15
+ BAUD_125K = 0x031C
16
+ BAUD_100K = 0x432F
17
+ BAUD_50K = 0x472F
18
+ BAUD_20K = 0x532F
19
+ BAUD_10K = 0x672F
20
+ BAUD_5K = 0x7F7F
21
+
22
+ # used by "init" and "set_receive_filter"
23
+ CAN_INIT_TYPE_ST = 0 # 11 Bit-ID handling - Standard frame
24
+ CAN_INIT_TYPE_EX = 1 # 29 Bit ID handling - Extended frame
25
+
26
+ # used by "write" and "read"
27
+ MSGTYPE_STANDARD = 0x00
28
+ MSGTYPE_RTR = 0x01
29
+ MSGTYPE_EXTENDED = 0x02
30
+ MSGTYPE_STATUS = 0x80
31
+
32
+ # return values
33
+ CAN_OK = 0x0000 # No error.
34
+ CAN_XMTFULL = 0x0001 # Transmission buffer of the controller is full.
35
+ CAN_OVERRUN = 0x0002 # CAN controller has been read out too late.
36
+ CAN_BUSLIGHT = 0x0004 # Bus error: An error counter has reached the 'Light' limit.
37
+ CAN_BUSHEAVY = 0x0008 # Bus error: An error counter has reached the 'Heavy' limit.
38
+ CAN_BUSOFF = 0x0010 # Bus error:Actual state from the CAN controller is 'Bus Off'.
39
+ CAN_QRCVEMPTY = 0x0020 # Receive queue is empty.
40
+ CAN_QOVERRUN = 0x0040 # Receive queue has been read out too late.
41
+ CAN_QXMTFULL = 0x0080 # Transmission queue is full.
42
+ CAN_REGTEST = 0x0100 # Register test of the 82C200/SJA1000 has failed.
43
+ CAN_NOVXD = 0x0200 # Driver is not loaded.
44
+ CAN_ILLHW = 0x1400 # Hardware handle is invalid.
45
+ CAN_ILLNET = 0x1800 # Net handle is invalid.
46
+ CAN_MASK_ILLHANDLE = 0x1C00 # Mask for all handle errors.
47
+ CAN_ILLCLIENT = 0x1C00 # Client handle is invalid.
48
+ CAN_RESOURCE = 0x2000 # Resource (FIFO, client, timeout) cannot be created.
49
+ CAN_ILLPARAMTYPE = 0x4000 # Parameter is not permitted/applicable here.
50
+ CAN_ILLPARAMVAL = 0x8000 # Parameter value is invalid.
51
+
52
+ def self.init(baud_value, message_type = CAN_INIT_TYPE_EX)
53
+ err = Core::cAN_Init(baud_value, message_type)
54
+
55
+ # allow the hardware to initialize
56
+ sleep 0.125
57
+
58
+ return err
59
+ end
60
+
61
+ def self.close
62
+ return Core::cAN_Close
63
+ end
64
+
65
+ def self.status
66
+ return Core::cAN_Status
67
+ end
68
+
69
+ def self.write(id, data, message_type = MSGTYPE_EXTENDED)
70
+ message = Core::TPCANMsg.malloc
71
+ message.id = id
72
+ message.message_type = message_type
73
+ message.length = data.length
74
+ message.data = data.dup
75
+
76
+ return Core::cAN_Write(message)
77
+ end
78
+
79
+ # Set the range of message ID that will be accepted.
80
+ # CAN_ResetFilter is used first to close the filter range,
81
+ # then CAN_MsgFilter is used to open it up.
82
+ def self.set_receive_filter(fromID, toID, message_type = MSGTYPE_EXTENDED)
83
+ Core::cAN_ResetFilter
84
+ return Core::cAN_MsgFilter(fromID, toID, message_type)
85
+ end
86
+
87
+ def self.read
88
+ message = Core::TPCANMsg.malloc
89
+
90
+ err = Core::cAN_Read(message)
91
+
92
+ return err, message.message_type, message.id, message.data[0..message.length - 1]
93
+ end
94
+
95
+ def self.read_id(id, timeout=1)
96
+ read_timeout = Time.now + timeout
97
+
98
+ begin
99
+ err, rx_type, rx_id, rx_data = self.read
100
+
101
+ if err == CAN_OK && rx_id == id then
102
+ return rx_data
103
+ end
104
+ end while Time.now < read_timeout
105
+
106
+ return false
107
+ end
108
+
109
+ def self.reset_client
110
+ return Core::cAN_ResetClient
111
+ end
112
+
113
+ def self.version_info
114
+ info = Core::Version_Info.malloc
115
+
116
+ err = Core::cAN_VersionInfo(info)
117
+
118
+ # info.value is an array of characters, convert it to string
119
+ return info.value.pack("c128")
120
+ end
121
+
122
+ def self.get_usb_device_number
123
+ number = Core::Device_Number.malloc
124
+
125
+ err = Core::getUSBDeviceNr(number)
126
+
127
+ return err, number.value
128
+ end
129
+
130
+ module Core
131
+ extend DL::Importable
132
+
133
+ dlload File.dirname(__FILE__) + "/Pcan_usb.dll"
134
+
135
+ TPCANMsg = struct [
136
+ "long id",
137
+ "char message_type",
138
+ "char length",
139
+ "UCHAR data[8]",
140
+ ]
141
+
142
+ Device_Number = struct ["long value"]
143
+ Version_Info = struct ["char value[128]"]
144
+
145
+ extern "long CAN_Init(long, int)"
146
+ extern "long CAN_Close()"
147
+ extern "long CAN_Status()"
148
+ extern "long CAN_Write(TPCANMsg *)"
149
+ extern "long CAN_Read(TPCANMsg *)"
150
+ extern "long CAN_VersionInfo(Version_Info *)"
151
+ extern "long CAN_ResetClient()"
152
+ extern "long CAN_MsgFilter(DWORD, DWORD, int)"
153
+ extern "long CAN_ResetFilter()"
154
+ extern "long SetUSBDeviceNr(long)"
155
+ extern "long GetUSBDeviceNr(long *)"
156
+ end # module Core
157
+
158
+ end # class PCAN_USB
Binary file
@@ -0,0 +1,55 @@
1
+ #-*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/contrib/rubyforgepublisher'
8
+
9
+ PKG_NAME = 'pcanusb'
10
+ PKG_VERSION = '0.0.1'
11
+
12
+ # Create compressed packages
13
+ dist_dirs = [ 'lib' ]
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.platform = Gem::Platform::RUBY
17
+ s.name = PKG_NAME
18
+ s.version = PKG_VERSION
19
+ s.summary = 'PCAN DLL wrapper'
20
+ s.description = <<EOF
21
+ PCAN is a Controller Area Network (CAN) device that connects to a PC via USB.
22
+ It ships with a DLL and documented API.
23
+ This is a Ruby wrapper for that API allowing CAN messages to be sent and received from Ruby.
24
+ EOF
25
+
26
+ # s.has_rdoc = true
27
+ s.requirements << 'none'
28
+
29
+ # s.add_dependency('activesupport', '= 1.4.2' + PKG_BUILD)
30
+
31
+ s.require_path = 'lib'
32
+ s.autorequire = 'rake'
33
+
34
+ s.files = [ "rakefile" ]
35
+ dist_dirs.each do |dir|
36
+ s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
37
+ end
38
+ end
39
+
40
+ Rake::GemPackageTask.new(spec) do |pkg|
41
+ pkg.gem_spec = spec
42
+ pkg.need_zip = true
43
+ # pkg.need_tar = true
44
+ end
45
+
46
+ desc "Publish the release files to RubyForge."
47
+ task :release => [ :package ] do
48
+ require 'rubyforge'
49
+
50
+ packages = %w( gem zip ).collect{ |ext| "pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}" }
51
+
52
+ rubyforge = RubyForge.new
53
+ rubyforge.login
54
+ rubyforge.add_release(PKG_NAME, PKG_NAME, "REL #{PKG_VERSION}", *packages)
55
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: pcanusb
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2008-05-12 00:00:00 -04:00
8
+ summary: PCAN DLL wrapper
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description: PCAN is a Controller Area Network (CAN) device that connects to a PC via USB. It ships with a DLL and documented API. This is a Ruby wrapper for that API allowing CAN messages to be sent and received from Ruby.
15
+ autorequire: rake
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors: []
30
+
31
+ files:
32
+ - rakefile
33
+ - lib/Pcan_usb.dll
34
+ - lib/PCAN_USB.rb
35
+ test_files: []
36
+
37
+ rdoc_options: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ requirements:
46
+ - none
47
+ dependencies: []
48
+