USB温度・湿度計モジュール関数リファレンス http://www2.strawberry-linux.com/products/usbrh/index.php?%5B%5B%B4%D8%BF%F4%A5%EA%A5%D5%A5%A1%A5%EC%A5%F3%A5%B9%5D%5D によると、FindUSBの引数は、「引数indexは変数で、はじめに初期値として0をセットしておきます。(中略)ユーザプログラムでindexを変更しないでください。」とあるので、FindUSB関数の呼び出しの部分を変更しました。

また、GetTempHumidTrueの戻り値を検査するステートメントを追加しました。

require 'dl/win32'

debug = 0

p ARGV if debug > 0

print "Usage: rh11set LED1 LED2 Heater (0:OFF, 1:ON)\n" if ARGV[0] == nil

LED1 = 0
LED2 = 1
ON = 1
OFF = 0

SetLED1 = ARGV[0].to_i > 0 ? ON : OFF

SetLED2 = ARGV[1].to_i > 0 ? ON : OFF

SetHeater = ARGV[2].to_i > 0 ? ON : OFF

p 'SetLED1', SetLED1, 'SetLED2', SetLED2, 'SetHeater', SetHeater if debug > 0

#Attribute VB_Name = "Module1"
#Option Explicit

#Public Declare Function GetVers Lib "USBMeter.dll" _
# Alias "_GetVers@4" (ByVal dev As String) As String

RH11GetVers = Win32API.new("USBMeter", "_GetVers@4", ['P'], 'P')

#Public Declare Function FindUSB Lib "USBMeter.dll" _
# Alias "_FindUSB@4" (ByRef index As Long) As String

RH11FindUSB = Win32API.new("USBMeter", "_FindUSB@4", ['P'], 'P')

#Public Declare Function GetTempHumid Lib "USBMeter.dll" _
# Alias "_GetTempHumid@12" (ByVal dev As String, ByRef temp As Double, ByRef humid As Double) As Long

RH11GetTempHumid = Win32API.new("USBMeter", "_GetTempHumid@12", ['P', 'P', 'P'], 'I')

#Public Declare Function ControlIO Lib "USBMeter.dll" _
# Alias "_ControlIO@12" (ByVal dev As String, ByVal port As Long, ByVal val As Long) As Long

RH11ControlIO = Win32API.new("USBMeter", "_ControlIO@12", ['P', 'I', 'I'], 'I')

#Public Declare Function SetHeater Lib "USBMeter.dll" _
# Alias "_SetHeater@8" (ByVal dev As String, ByVal val As Long) As Long

RH11SetHeater = Win32API.new("USBMeter", "_SetHeater@8", ['P', 'I'], 'I')

#Public Declare Function GetTempHumidTrue Lib "USBMeter.dll" _
# Alias "_GetTempHumidTrue@12" (ByVal dev As String, ByRef temp As Double, ByRef humid As Double) As Long

RH11GetTempHumidTrue = Win32API.new("USBMeter", "_GetTempHumidTrue@12", ['P', 'P', 'P'], 'I')

#'
#' Device Name
#'
#Public Device As String

#MsgBox = Win32API.new("user32", "MessageBoxA", ['P', 'P', 'P', 'I'], 'I')
#ret = MsgBox.call(0, Time.now.to_s, "monitor.rb", 0)
#p 'ret', ret

dev = ""
index = "\0\0\0\0"
10.times { break if (dev = RH11FindUSB.call(index)) != "" }

p 'dev', dev if debug > 1

if dev == ""
print "Sensor not found!\n"
exit 1
else
Device = dev
ret = RH11SetHeater.call(Device, OFF)
print "Sensor connected\n"
end

vers = RH11GetVers.call(Device)

print 'Version='+vers+"\n"

ret = RH11ControlIO.call(Device, LED1, ON)

temp = "12345678"
rh = "12345678"
ret = RH11GetTempHumidTrue.call(Device, temp, rh)

if ret != 0
print "Sensor read error!\n"
exit 1
end

p 'temp', temp.unpack('h16'), temp.unpack('E') if debug > 1
p 'rh', rh.unpack('h16'), rh.unpack('E') if debug > 1

dTemp = temp.unpack('E')
dRh = rh.unpack('E')

print 'TempTrue='+dTemp.to_s+"\n"
print 'HumidTrue='+dRh.to_s+"\n"

temp = "12345678"
rh = "12345678"
ret = RH11GetTempHumid.call(Device, temp, rh)

if ret != 0
print "Sensor read error!\n"
exit 1
end

p 'temp', temp.unpack('h16'), temp.unpack('E') if debug > 1
p 'rh', rh.unpack('h16'), rh.unpack('E') if debug > 1

dTemp = temp.unpack('E')
dRh = rh.unpack('E')

print 'Temp='+dTemp.to_s+"\n"
print 'Humid='+dRh.to_s+"\n"

#sleep 3

#ret = RH11ControlIO.call(Device, LED1, OFF)

ret = RH11ControlIO.call(Device, LED1, SetLED1)

print 'LED1 is '+(SetLED1 > 0 ? 'ON' : 'OFF')+"\n"

ret = RH11ControlIO.call(Device, LED2, SetLED2)

print 'LED2 is '+(SetLED2 > 0 ? 'ON' : 'OFF')+"\n"

ret = RH11SetHeater.call(Device, SetHeater)

print 'Heater is '+(SetHeater > 0 ? 'ON' : 'OFF')+"\n"

exit 0