Loading

Paste #plzdmmzle

  1. # -*- coding: utf-8 -*-
  2. # original: https://raw.githubusercontent.com/UedaTakeyuki/slider/master/mh_z19.py
  3. #
  4. # © Takeyuki UEDA 2015 -
  5.  
  6. import serial
  7. import time
  8. import subprocess
  9. import traceback
  10. import struct
  11. import platform
  12. import argparse
  13. import sys
  14. import json
  15. import os.path
  16.  
  17. # setting
  18. version = "0.3.9"
  19.  
  20. serial_dev = 'COM3'
  21.  
  22. # major version of running python
  23. p_ver = platform.python_version_tuple()[0]
  24.  
  25. def set_serialdevice(serialdevicename):
  26.   global serial_dev
  27.   serial_dev = serialdevicename
  28.  
  29. def connect_serial():
  30.   return serial.Serial(serial_dev,
  31.                         baudrate=9600,
  32.                         bytesize=serial.EIGHTBITS,
  33.                         parity=serial.PARITY_NONE,
  34.                         stopbits=serial.STOPBITS_ONE,
  35.                         timeout=1.0)
  36.  
  37. def mh_z19(ser):
  38.   try:
  39.     while 1:
  40.       result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79")
  41.       s=ser.read(9)
  42.  
  43.       if p_ver == '2':
  44.         if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86":
  45.           return {'co2': ord(s[2])*256 + ord(s[3])}
  46.         break
  47.       else:
  48.         if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86:
  49.           return {'co2': s[2]*256 + s[3]}
  50.         break
  51.   except:
  52.      traceback.print_exc()
  53.  
  54. def read(ser):
  55.   result = mh_z19(ser)
  56.   if result is not None:
  57.     return result
  58.  
  59. def read_all(ser):
  60.   try:
  61.     while 1:
  62.       result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79")
  63.       s=ser.read(9)
  64.  
  65.       if p_ver == '2':
  66.         if len(s) >= 9 and s[0] == "\xff" and s[1] == "\x86":
  67.           return {'co2': ord(s[2])*256 + ord(s[3]),
  68.                   'temperature': ord(s[4]) - 40,
  69.                   'TT': ord(s[4]),
  70.                   'SS': ord(s[5]),
  71.                   'UhUl': ord(s[6])*256 + ord(s[7])
  72.                   }
  73.         break
  74.       else:
  75.         if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86:
  76.           return {'co2': s[2]*256 + s[3],
  77.                   'temperature': s[4] - 40,
  78.                   'TT': s[4],
  79.                   'SS': s[5],
  80.                   'UhUl': s[6]*256 + s[7]
  81.                   }
  82.         break
  83.   except:
  84.      traceback.print_exc()
  85.  
  86.   if result is not None:
  87.     return result
  88.  
  89. def abc_on():
  90.   ser = connect_serial()
  91.   result=ser.write(b"\xff\x01\x79\xa0\x00\x00\x00\x00\xe6")
  92.   ser.close()
  93.  
  94. def abc_off():
  95.   ser = connect_serial()
  96.   result=ser.write(b"\xff\x01\x79\x00\x00\x00\x00\x00\x86")
  97.   ser.close()
  98.  
  99. def span_point_calibration(span):
  100.   ser = connect_serial()
  101.   if p_ver == '2':
  102.     b3 = span / 256;
  103.   else:
  104.     b3 = span // 256;  
  105.   byte3 = struct.pack('B', b3)
  106.   b4 = span % 256; byte4 = struct.pack('B', b4)
  107.   c = checksum([0x01, 0x88, b3, b4])
  108.   request = b"\xff\x01\x88" + byte3 + byte4 + b"\x00\x00\x00" + c
  109.   result = ser.write(request)
  110.   ser.close()
  111.  
  112. def zero_point_calibration():
  113.   ser = connect_serial()
  114.   request = b"\xff\x01\x87\x00\x00\x00\x00\x00\x78"
  115.   result = ser.write(request)
  116.   ser.close()
  117.  
  118. def detection_range_5000():
  119.   ser = connect_serial()
  120.   request = b"\xff\x01\x99\x00\x00\x00\x13\x88\xcb"
  121.   result = ser.write(request)
  122.   ser.close()
  123.  
  124. def detection_range_2000():
  125.   ser = connect_serial()
  126.   request = b"\xff\x01\x99\x00\x00\x00\x07\xd0\x8F"
  127.   result = ser.write(request)
  128.   ser.close()
  129.  
  130.  
  131. def checksum(array):
  132.   return struct.pack('B', 0xff - (sum(array) % 0x100) + 1)
  133.  
  134. if __name__ == '__main__':
  135. #  value = read()
  136. #  print (value)
  137.   parser = argparse.ArgumentParser(
  138.     description='''return CO2 concentration as object as {'co2': 416}''',
  139.   )
  140.   group = parser.add_mutually_exclusive_group()
  141.  
  142.   group.add_argument("--serial_device",
  143.                       type=str,
  144.                       help='''Use this serial device file''')
  145.  
  146.   group.add_argument("--version",
  147.                       action='store_true',
  148.                       help='''show version''')
  149.   group.add_argument("--all",
  150.                       action='store_true',
  151.                       help='''return all (co2, temperature, TT, SS and UhUl) as json''')
  152.   group.add_argument("--abc_on",
  153.                       action='store_true',
  154.                       help='''Set ABC functionality on model B as ON.''')
  155.   group.add_argument("--abc_off",
  156.                       action='store_true',
  157.                       help='''Set ABC functionality on model B as OFF.''')
  158.   parser.add_argument("--span_point_calibration",
  159.                       type=int,
  160.                       help='''Call calibration function with SPAN point''')
  161.   parser.add_argument("--zero_point_calibration",
  162.                       action='store_true',
  163.                       help='''Call calibration function with ZERO point''')
  164.   parser.add_argument("--detection_range_5000",
  165.                       action='store_true',
  166.                       help='''Set detection range as 5000''')
  167.   parser.add_argument("--detection_range_2000",
  168.                       action='store_true',
  169.                       help='''Set detection range as 2000''')
  170.  
  171.   args = parser.parse_args()
  172.  
  173.   if args.serial_device is not None:
  174.     set_serialdevice(args.serial_device)
  175.  
  176.   if args.abc_on:
  177.     abc_on()
  178.     print ("Set ABC logic as on.")
  179.   elif args.abc_off:
  180.     abc_off()
  181.     print ("Set ABC logic as off.")
  182.   elif args.span_point_calibration is not None:
  183.     span_point_calibration(args.span_point_calibration)
  184.     print ("Call Calibration with SPAN point.")
  185.   elif args.zero_point_calibration:
  186.     print ("Call Calibration with ZERO point.")
  187.     zero_point_calibration()
  188.   elif args.detection_range_5000:
  189.     detection_range_5000()
  190.     print ("Set Detection range as 5000.")
  191.   elif args.detection_range_2000:
  192.     detection_range_2000()
  193.     print ("Set Detection range as 2000.")
  194.   elif args.version:
  195.     print (version)
  196.   elif args.all:
  197.     value = read_all(ser)
  198.     print (json.dumps(value))
  199.   else:
  200.       ser = connect_serial()
  201.       while True:
  202.           value = read_all(ser)
  203.           print (json.dumps(value))
  204.           time.sleep(5)
  205.  
  206.   sys.exit(0)

Comments