#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 5 14:52:57 2020 @author: nits """ import minimalmodbus import time import configfile conf = configfile.Configuration() import Adafruit_BBIO.UART as UART from datetime import datetime class WindSensor(): def __init__(self): #initialise network connection and parameters conf = configfile.Configuration() UART.setup(conf.uart) self.inst = minimalmodbus.Instrument(conf.port_name, conf.slave_address) # port name, slave address (in decimal) self.inst.serial.baudrate = conf.baudrate # Baud self.inst.serial.parity = conf.parity self.inst.serial.stopbits = conf.stopbits self.inst.mode = conf.mode self.inst.close_port_after_each_call=conf.close_port_after_each_call self.inst.debug=conf.debug_serial self.inst.serial.timeout = conf.timeout self.inst.handle_local_echo = conf.handle_local_echo self.error=0 self.errorcounter = 0 self.cyclecounter = 0 #read temperature from sensor def readTemp(self): try: temp = self.inst.read_long(30405,4,True)/10 except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 #time.sleep(0.1) return 0 return temp #read speed from sensor def readWindSpeed(self): try: temp = self.inst.read_long(30003,4,False)/10 except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 self.error=1 return 0 return temp #read wind direction from sensor def readWindDirection(self): try: temp = self.inst.read_long(30203,4,False)/10 except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 # time.sleep(0.1) error=1 return 0 return temp #read date and time from sensor def readDateTime(self): try: date = self.inst.read_long(34601,4,False) uhr = self.inst.read_long(34603,4,False) date_time = datetime.strptime(str(date)+str(uhr), '%Y%m%d%H%M%S'); date_info=date_time.strftime('%Y-%m-%d %H:%M:%S'); except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 return "" return date_info #read compass angle from sensor def readCompassAngle(self): try: temp = self.inst.read_long(35081,4,False)/10 except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 error=1 return 0 return temp #function to read parameter def readP(self,address): try: param = self.inst.read_long(address,3,False) print("The value of the address ",address,"is: ",param) except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 error=1 return 0 #function to set parameter def setP(self,address,value): try: param = self.inst.write_register(address,value,functioncode=16) print("The value of the address ",address,"has been changed to: ",value) except Exception as error: print ("[!] Exception occurred: ", error) self.errorcounter = self.errorcounter + 1 error=1 return 0 return 1