make scripts ruby 1.9 compatible
This commit is contained in:
parent
555aecf318
commit
9287f90d25
10
demo.rb
10
demo.rb
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
# ruby: 2.2
|
# ruby: 1.9
|
||||||
require 'serialport'
|
require 'serialport'
|
||||||
require './telegram'
|
require './telegram'
|
||||||
|
|
||||||
|
@ -11,11 +11,11 @@ DEBUG = false
|
||||||
# send query/send telegram and return answer
|
# send query/send telegram and return answer
|
||||||
def comm(query,data=nil)
|
def comm(query,data=nil)
|
||||||
telegram = Telegram.new(query,data)
|
telegram = Telegram.new(query,data)
|
||||||
puts("< "+(telegram.pack.bytes.collect{|b| "%02x" % b})*" ") if DEBUG
|
puts("< "+(telegram.pack.bytes.to_a.collect{|b| "%02x" % b})*" ") if DEBUG
|
||||||
@serial.write(telegram.pack)
|
@serial.write(telegram.pack)
|
||||||
answer = @serial.readpartial(3+16+1+2)
|
answer = @serial.readpartial(3+16+1+2)
|
||||||
raise "no answer received to query #{query}" if !answer or answer.empty?
|
raise "no answer received to query #{query}" if !answer or answer.empty?
|
||||||
puts("> "+(answer.bytes.collect{|b| "%02x" % b})*" ") if DEBUG
|
puts("> "+(answer.bytes.to_a.collect{|b| "%02x" % b})*" ") if DEBUG
|
||||||
telegram = Telegram.parse(answer)
|
telegram = Telegram.parse(answer)
|
||||||
raise "malformed answer" unless telegram
|
raise "malformed answer" unless telegram
|
||||||
raise telegram.to_s if (telegram.object==0 and telegram.data.length==2 and telegram.data[0]==0xff) or (telegram.object==255 and telegram.data[0]!=0)
|
raise telegram.to_s if (telegram.object==0 and telegram.data.length==2 and telegram.data[0]==0xff) or (telegram.object==255 and telegram.data[0]!=0)
|
||||||
|
@ -50,13 +50,13 @@ def set_values (voltage=nil,current=nil,protection=false)
|
||||||
raise "#{voltage} out of voltage range [0-#{query[2]*nominal[0]}]" if voltage<0 or voltage>query[2]*nominal[0]
|
raise "#{voltage} out of voltage range [0-#{query[2]*nominal[0]}]" if voltage<0 or voltage>query[2]*nominal[0]
|
||||||
comm(54,[0x10,0x10]) # enable remote
|
comm(54,[0x10,0x10]) # enable remote
|
||||||
value = voltage*25600.0/nominal[0]
|
value = voltage*25600.0/nominal[0]
|
||||||
comm(query[0],[value].pack('n').bytes)
|
comm(query[0],[value].pack('n').bytes.to_a)
|
||||||
end
|
end
|
||||||
if current then
|
if current then
|
||||||
comm(54,[0x10,0x10]) # enable remote
|
comm(54,[0x10,0x10]) # enable remote
|
||||||
raise "#{current} out of current range [0-#{query[2]*nominal[1]}]" if current<0 or current>query[2]*nominal[1]
|
raise "#{current} out of current range [0-#{query[2]*nominal[1]}]" if current<0 or current>query[2]*nominal[1]
|
||||||
value = current*25600.0/nominal[1]
|
value = current*25600.0/nominal[1]
|
||||||
comm(query[1],[value].pack('n').bytes)
|
comm(query[1],[value].pack('n').bytes.to_a)
|
||||||
end
|
end
|
||||||
voltage = comm(query[0]).data.pack("C*").unpack("n")[0]/25600.0
|
voltage = comm(query[0]).data.pack("C*").unpack("n")[0]/25600.0
|
||||||
current = comm(query[1]).data.pack("C*").unpack("n")[0]/25600.0
|
current = comm(query[1]).data.pack("C*").unpack("n")[0]/25600.0
|
||||||
|
|
2
mitm.rb
2
mitm.rb
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
# ruby: 2.2
|
# ruby: 1.9
|
||||||
require 'serialport'
|
require 'serialport'
|
||||||
require './telegram'
|
require './telegram'
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
# ruby: 2.2
|
# ruby: 1.9
|
||||||
|
|
||||||
class Telegram
|
class Telegram
|
||||||
attr_accessor :direction, :cast, :transmission, :node, :object, :data
|
attr_accessor :direction, :cast, :transmission, :node, :object, :data
|
||||||
|
@ -87,7 +87,7 @@ class Telegram
|
||||||
# check there are at least 5 bytes (minimum message size)
|
# check there are at least 5 bytes (minimum message size)
|
||||||
return nil if telegram==nil
|
return nil if telegram==nil
|
||||||
return nil if telegram.length<5
|
return nil if telegram.length<5
|
||||||
bytes = telegram.bytes # get bytes
|
bytes = telegram.bytes.to_a # get bytes
|
||||||
to_return = new(bytes[2]) # new Telegram
|
to_return = new(bytes[2]) # new Telegram
|
||||||
# parse start delimiter (SD)
|
# parse start delimiter (SD)
|
||||||
length = bytes[0]&0x0f # get length
|
length = bytes[0]&0x0f # get length
|
||||||
|
|
Loading…
Reference in New Issue