modbus 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5559848be6158b9fdf9c0d2a8d030b08769f181510cfba3a59f90a7a8af9e723
4
- data.tar.gz: aade6afbf693c033ccb3bd4b021592d0dc09ceff7247ce4a9f66de3491e0bba2
3
+ metadata.gz: c71dd92fc7f440348037b3078fc7c107abe9c2dca3bea369ab23564c50127e0f
4
+ data.tar.gz: 9fd3040057c57989d602561d0b4f089ed8153a3fdb21aba14dc01fee36565603
5
5
  SHA512:
6
- metadata.gz: 5b2ff3ee1a9dfcc6ce804879ec11158db23c1581ce4e9134d439fbc1035e7d8b150cd65a291d91dac3a6a2e150459768dcf83a10967f6469eb0c07a3fd55c11a
7
- data.tar.gz: f2409419ec8923a69dccc82e6d340138e320126befa6f7f9add418419a24aa376ce1147f0b33d4f8f3e7451e905b9ea226b1a21f655628b1c0d8703f97510d5b
6
+ metadata.gz: 30fa15861003cbff24a03d1c4cb3f4e02e283c0c8fbb6635aacf4914f20d9f0eb007ee5b50aa93bb3891e45f129f23916a7450a0df1eaff9c8f8a3a4559e0e92
7
+ data.tar.gz: fa2138873ca114f9cdf37b94862c9edc1b83d0ce1c4f71363358e0a03a8644fb7df9682686f86d271f6123da5dc38074b22f397ca698b879569e829aa268a234
data/README.md CHANGED
@@ -34,11 +34,20 @@ modbus.read(byte_string) do |adu|
34
34
 
35
35
  # Response PDU returned
36
36
  if adu.exception?
37
+ # Get error message
38
+ puts adu.value
39
+ # Error code
37
40
  puts adu.pdu.exception_code
38
41
  else
39
- case adu.function_code
40
- when 0x01
41
- puts adu.pdu.read.data.bytes
42
+ case adu.function_name
43
+ when :read_coils
44
+ # raw response data
45
+ puts adu.pdu.get.data.bytes
46
+ # or values
47
+ puts adu.value # => [true, false, true, false, false, false, false, false]
48
+ when :read_input_registers
49
+ # Grab the 16 bit values
50
+ puts adu.value # => [1234, 8822]
42
51
  end
43
52
  end
44
53
  end
@@ -47,8 +47,13 @@ class Modbus
47
47
  # Decodes an ADU from wire format and sets the attributes of this object.
48
48
  #
49
49
  # @param data [String] The bytes to decode.
50
- def read(data)
50
+ def read(data, serial: false)
51
51
  @buffer ||= String.new
52
+ # add mock TCP header
53
+ if serial
54
+ @buffer << "\x0\x0\x0\x0\x0"
55
+ @buffer << (data.bytesize - 2)
56
+ end
52
57
  @buffer << data
53
58
 
54
59
  error = nil
@@ -1,6 +1,8 @@
1
1
  # encoding: ASCII-8BIT
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'crc'
5
+
4
6
  class Modbus
5
7
  # TCP ADU are sent via TCP to registered port 502
6
8
  Modbus::ADU = Struct.new :header, :function_code, :pdu do
@@ -8,10 +10,17 @@ class Modbus
8
10
  pdu.is_a?(ExceptionPDU)
9
11
  end
10
12
 
11
- def to_binary_s
13
+ def to_binary_s(serial: false)
12
14
  data = pdu.to_binary_s
13
15
  header.request_length = data.bytesize
14
- "#{header.to_binary_s}#{data}"
16
+ if serial
17
+ # For serial line communication - no header
18
+ crc = ::CRC::CRC16_MODBUS.new
19
+ crc.update data
20
+ "#{data}#{crc.digest}"
21
+ else
22
+ "#{header.to_binary_s}#{data}"
23
+ end
15
24
  end
16
25
 
17
26
  def function_name
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "modbus"
5
- s.version = '1.0.0'
5
+ s.version = '1.0.1'
6
6
  s.authors = ["Stephen von Takach"]
7
7
  s.email = ["steve@aca.im"]
8
8
  s.licenses = ["MIT"]
@@ -12,13 +12,12 @@ Gem::Specification.new do |s|
12
12
  Constructs Modbus standard datagrams that make it easy to communicate with devices on Modbus networks
13
13
  EOF
14
14
 
15
-
16
15
  s.add_dependency 'bindata', '~> 2.3'
16
+ s.add_dependency 'crc', '~> 0.4'
17
17
 
18
18
  s.add_development_dependency 'rspec', '~> 3.5'
19
19
  s.add_development_dependency 'rake', '~> 12'
20
20
 
21
-
22
21
  s.files = Dir["{lib}/**/*"] + %w(modbus.gemspec README.md)
23
22
  s.test_files = Dir["spec/**/*"]
24
23
  s.extra_rdoc_files = ["README.md"]
@@ -70,4 +70,15 @@ describe 'modbus protocol helper' do
70
70
  expect(@response.value).to eq([3, 256])
71
71
  expect(@response.function_name).to eq(:read_holding_registers)
72
72
  end
73
+
74
+ it "should generate a serial line request" do
75
+ data = @modbus.read_inputs(0).to_binary_s serial: true
76
+ expect(data).to eq("\xFF\x02\x00\x00\x00\x01\x14\xAC")
77
+ end
78
+
79
+ it "should parse a serial line response" do
80
+ serial_data = "\x1\x2\x3\xac\xdb\x35\x88\x22"
81
+ @modbus.read(serial_data, serial: true) { |adu| @response = adu }
82
+ expect(@response.to_binary_s serial: true).to eq(serial_data)
83
+ end
73
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-17 00:00:00.000000000 Z
11
+ date: 2018-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: crc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.4'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement