From d16665cbaa8fa6699e66d11c8bc019b06af6d44a Mon Sep 17 00:00:00 2001
From: Roman Bazalevsky <rvb@rvb.name>
Date: Tue, 31 Dec 2019 17:16:37 +0300
Subject: [PATCH] =?utf8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?=
 =?utf8?q?=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE?=
 =?utf8?q?=D0=B9=20=D1=81=D1=83=D0=BC=D0=BC=D1=83=20=D0=B4=D0=BB=D1=8F=20M?=
 =?utf8?q?HZ-19.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 bin/weathermon-iio | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/bin/weathermon-iio b/bin/weathermon-iio
index 9298f6f..d288edf 100755
--- a/bin/weathermon-iio
+++ b/bin/weathermon-iio
@@ -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,26 @@ 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 init_serial_device(device,subsystem,parameters)
-- 
2.34.1