make scripts ruby 1.9 compatible

This commit is contained in:
King Kévin 2015-07-30 08:21:20 +02:00
parent 555aecf318
commit 9287f90d25
3 changed files with 8 additions and 8 deletions

10
demo.rb
View File

@ -1,6 +1,6 @@
#!/usr/bin/env ruby
# encoding: utf-8
# ruby: 2.2
# ruby: 1.9
require 'serialport'
require './telegram'
@ -11,11 +11,11 @@ DEBUG = false
# send query/send telegram and return answer
def comm(query,data=nil)
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)
answer = @serial.readpartial(3+16+1+2)
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)
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)
@ -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]
comm(54,[0x10,0x10]) # enable remote
value = voltage*25600.0/nominal[0]
comm(query[0],[value].pack('n').bytes)
comm(query[0],[value].pack('n').bytes.to_a)
end
if current then
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]
value = current*25600.0/nominal[1]
comm(query[1],[value].pack('n').bytes)
comm(query[1],[value].pack('n').bytes.to_a)
end
voltage = comm(query[0]).data.pack("C*").unpack("n")[0]/25600.0
current = comm(query[1]).data.pack("C*").unpack("n")[0]/25600.0

View File

@ -1,6 +1,6 @@
#!/usr/bin/env ruby
# encoding: utf-8
# ruby: 2.2
# ruby: 1.9
require 'serialport'
require './telegram'

View File

@ -1,5 +1,5 @@
# encoding: utf-8
# ruby: 2.2
# ruby: 1.9
class Telegram
attr_accessor :direction, :cast, :transmission, :node, :object, :data
@ -87,7 +87,7 @@ class Telegram
# check there are at least 5 bytes (minimum message size)
return nil if telegram==nil
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
# parse start delimiter (SD)
length = bytes[0]&0x0f # get length