Произвольные настройки порта в конфигурационном файле.
[weathermon.git] / bin / weathermon-iio
index 9298f6f5a4aab760a6b6968f3ebbbebbd222a8ef..dcbca6430d3dc4bc0bfde364be9a1c0d55068043 100755 (executable)
@@ -11,6 +11,19 @@ require "wm_util"
 
 io.stdout:setvbuf('no')
 
+function dump(o)
+   if type(o) == 'table' then
+      local s = '{ '
+      for k,v in pairs(o) do
+         if type(k) ~= 'number' then k = '"'..k..'"' end
+         s = s .. '['..k..'] = ' .. dump(v) .. ','
+      end
+      return s .. '} '
+   else
+      return tostring(o)
+   end
+end
+
 function get_device_list(config_name)
 
   local devices
@@ -81,9 +94,40 @@ end
 
 function get_mhz(record)
   local p = record["rs232"]  
+  p:read(9,100)
   p:write(string.fromhex("ff0186000000000079"))
   local e, s = p:read(9,1000)
-  return s:byte(3)*256+s:byte(4)
+  if (e == 0) and (s:len() == 9) and (s:byte(1) == 255) then
+    local crc = 0
+    for i = 2, 8 do
+      crc = crc + s:byte(i)
+      if (crc>=256) then
+        crc = crc - 256
+      end  
+    end  
+    crc = 255 - crc
+    crc = crc + 1;
+    if crc == s:byte(9) then
+      return s:byte(3)*256+s:byte(4)
+    end  
+  end
+  
+  return nil
+   
+end
+
+function search_rs232_const(rs232,prefix,value)
+
+  for k,v in pairs(rs232) do
+  
+    if k == prefix..value:upper() then
+      return v
+    end  
+  
+  end
+
+  return nil
+
 end
 
 function init_serial_device(device,subsystem,parameters)
@@ -93,24 +137,33 @@ function init_serial_device(device,subsystem,parameters)
   pcall(function ()
 
     local e, port = rs232.open(device["port"])
-  
-    if subsystem == "mhz" then
+
+    local baud = device["baud"]; if baud == nil then baud = 9600; end
+    local bits = device["bits"]; if bits == nil then bits = 8; end
+    local stop_bits = device["stop_bits"]; if stop_bits == nil then stop_bits = 1; end
+    local parity = device["parity"]; if parity == nil then parity = "NONE"; end 
+    local flowctl = device["flowctl"]; if flowctl == nil then flowctl = "OFF"; end
     
-      assert(port:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_flow_control(rs232.RS232_FLOW_OFF)  == rs232.RS232_ERR_NOERROR)
+    assert(port:set_baud_rate(search_rs232_const(rs232,"RS232_BAUD_",baud)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_data_bits(search_rs232_const(rs232,"RS232_DATA_",bits)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_parity(search_rs232_const(rs232,"RS232_PARITY_",parity)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_stop_bits(search_rs232_const(rs232,"RS232_STOP_",stop_bits)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_flow_control(search_rs232_const(rs232,"RS232_FLOW_",flowctl)) == rs232.RS232_ERR_NOERROR)
     
-      getparameter = {}
+    getparameter = {}
 
-      getparameter["rs232"] = port
+    getparameter["rs232"] = port
+  
+    if subsystem == "mhz" then
+    
       getparameter["function"] = get_mhz 
       getparameter["name"] = "CO2_PPM"
       getparameter["sensor"] = "MHZ19"
       parameters[#parameters+1] = getparameter
 
     end
+
+    parameters[#parameters+1] = getparameter
     
   end)