Module: AgriController::MCH383

Defined in:
lib/agri-controller/device/mch383.rb

Overview

MCH383(MCH-383,MCH-383SD) is Tenperature,Humidity,CO2-ppm recorder

Usage

include AgriController::MCH383

  • res=read(“COM2”) # =>“00241040100000643r00242010100000310r00243190000000612r”

  • res=read(“/dev/ttyUSB0”)# =>“00241040100000643r00242010100000310r00243190000000612r”

  • parse(res) # =>[“%”], “C”], “ppm”]]

Class Method Summary collapse

Class Method Details

.data_hash(num) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/agri-controller/device/mch383.rb', line 59

def data_hash(num)
  case num
  when "01"
    #"Celsius"
    "C"
  when "02"
    #"Fahrenheit"
    "F"
  when "04"
    #"Humidity"
    "%"
  when "19"
    #"CO2 of ppm"
    "ppm"
  else
    nil
  end
end

.list(code) ⇒ Object



142
143
144
# File 'lib/agri-controller/device/mch383.rb', line 142

def list(code) 
  parse(code)
end

.of_10(of_ten) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/agri-controller/device/mch383.rb', line 78

def of_10(of_ten)
  case of_ten
  when "0"
    1
  when "1"
    0.1
  when "2"
    0.01
  when "3"
    0.001
  else
    nil
  end
end

.parse(code) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/agri-controller/device/mch383.rb', line 127

def parse(code)
  result={}
  #begin
  data=code.split("\r")
  data.each{|str| x=to_f(str);result[x.keys.first]=x[x.keys.first]}
    #if data[0].size>1
    #  x=to_f(data.last+data[0])
    #  result << x if x
    #end
  #rescue
  #  return nil
  #end
  return result
end

.read(port) ⇒ Object

This require “serialport” #RUBYGEMS



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/agri-controller/device/mch383.rb', line 19

def read(port)
  require "rubygems" if RUBY_VERSION < "1.9.0"
  require "serialport"

  #t=Time.now
  res=nil
  begin

   # SerialPort.open(port,9600,8,1,SerialPort::NONE){|sp| sp.read_timeout=100
   # while res=sp.read(1)
   #   if res=="\r"
   #     break
   #   end
   # end
    #}
    #
   #read data
   #SerialPort.open(port,9600,8,1,SerialPort::NONE){|sp| sp.read_timeout=500; res=sp.read(48)}

  sp=SerialPort.new(port,9600,8,1,SerialPort::NONE)
  sp.read_timeout=500
  while res=sp.read(1)
    if res=="\002"
      res=res+sp.read(47)
      break
    end
  end
  sp.close
    #p Time.now-t
    return res # =>"\00241040100000643\r\00242010100000310\r\00243190000000612\r"
  rescue
    return nil
  end
  
end

.sampleObject



55
56
57
# File 'lib/agri-controller/device/mch383.rb', line 55

def sample
  "\00241040100000643\r\00242010100000310\r\00243190000000612\r"
end

.samplesObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/agri-controller/device/mch383.rb', line 146

def samples
  ["\00241040100000643\r\00242010100000310\r\00243190000000612\r",
  "\00242010100000311\r\00243190000000612\r\00241040100000643\r",
  "\00243190000000612\r\00241040100000643\r\00242010100000312\r",
  "\r\00243190000000590\r\00241040100000639\r\00242010100000309",
  
  "\00243190000000612\r",
  "not_data",
  nil]
end

.to_f(x) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/agri-controller/device/mch383.rb', line 102

def to_f(x)
  #x="\00243190000000612\r"
  result={}
  
  if x.chomp.size==15
    begin 
      #first_num=x.slice(1,1)#always "4"
      data_number=x.slice(2,1)
      unit_sign  =x.slice(3,2)
      positive   =x.slice(5,1)
      of_ten     =x.slice(6,1)
      num        =x.slice(7,8)
      
      val=value(num,of_ten,positive)
      
      result[data_number]=[val,data_hash(unit_sign)]
      result
    rescue
      return nil
    end
  else
    nil
  end
end

.value(num, of_ten, positive) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/agri-controller/device/mch383.rb', line 93

def value(num,of_ten,positive)
  result=num.to_i * of_10(of_ten)
  if positive=="0"#plus +
    return result
  else#minus -
    return result*(-1)
  end
end