From 38869bc514fe591aade187c4fa7990a9030d7322 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Mon, 18 Jul 2016 16:13:56 -0500 Subject: [PATCH] Resolves IPP- 59. Enables writing to E300 after the tag value is set. Needs testing --- POCloud_Driver/ipp.py | 42 ++++++++++------------ POCloud_Driver/micro800.py | 72 +++++++++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 36 deletions(-) diff --git a/POCloud_Driver/ipp.py b/POCloud_Driver/ipp.py index 154b9a5..d597116 100644 --- a/POCloud_Driver/ipp.py +++ b/POCloud_Driver/ipp.py @@ -56,17 +56,24 @@ class Channel(): def write(self, val, handshake=None, handshake_val=None): if self.writeable: - h = handshake - hval = handshake_val - if h is None: - if u800.writeMicroTag(self.device_addr, self.tag, val, handshake=self.tag, handshake_val=val): - self.sendFn(self.name, val, time.time()) - self.last_value = val - return True + if not self.e300_param: + if handshake is None: + if u800.writeMicroTag(self.device_addr, self.tag, val): + self.sendFn(self.name, val, time.time()) + self.last_value = val + return True + else: + return False else: - return False + return u800.writeMicroTag(self.device_addr, self.tag, val, handshake=handshake, handshake_val=handshake_val) else: - return u800.writeMicroTag(self.device_addr, self.tag, val, handshake=h, handshake_val=hval) + if u800.writeMicroTag(self.device_addr, self.tag, val): + if u800.writeMicroTag(self.device_addr, "write_E300", 1): + self.sendFn(self.name, val, time.time()) + self.last_value = val + return True + return False + else: print("NOT ALLOWED TO WRITE TO {}".format(self.name)) return False @@ -74,20 +81,9 @@ class Channel(): class start(threading.Thread, deviceBase): def writeTag_WriteE300(self, addr, tag, val): - c = u800.Driver() - if c.open(addr): - try: - cv = c.read_tag(tag) - wt = c.write_tag(tag, val, cv[1]) - write_E300 = c.write_tag("cfg_WRITE", 1, "BOOL") - print("wt= {}, write_E300 = {}".format(wt, write_E300)) - return wt and write_E300 - except Exception: - err = c.get_status() - c.close() - print err - return False - c.close() + write_tag = u800.writeMicroTag(addr, tag, val) + write_e300 = u800.writeMicroTag(addr, "write_E300", 1) + return write_tag and write_e300 def updateGPS(self): gps = self.mcu.gps diff --git a/POCloud_Driver/micro800.py b/POCloud_Driver/micro800.py index 5c5311a..fd9e885 100644 --- a/POCloud_Driver/micro800.py +++ b/POCloud_Driver/micro800.py @@ -1,11 +1,11 @@ -from pycomm.ab_comm.clx import Driver as u800Driver +from pycomm.ab_comm.clx import Driver as plcDriver import sys def readMicroTag(addr, tag): addr = str(addr) tag = str(tag) - c = u800Driver() + c = plcDriver() if c.open(addr, True): try: v = c.read_tag(tag) @@ -14,13 +14,15 @@ def readMicroTag(addr, tag): except Exception: err = c.get_status() c.close() - print("Error with {}: {}".format(tag, err)) + print "{} on reading {} from {}".format(err, tag, addr) pass c.close() def getTagType(addr, tag): - c = u800Driver() + addr = str(addr) + tag = str(tag) + c = plcDriver() if c.open(addr, True): try: return c.read_tag(tag)[1] @@ -32,25 +34,69 @@ def getTagType(addr, tag): c.close() -def writeMicroTag(addr, tag, val): - c = u800Driver() +def write(addr, tag, val, t): + addr = str(addr) + tag = str(tag) + c = plcDriver() if c.open(addr, True): try: - # typ = getTagType(addr, tag) - cv = c.read_tag(tag) - wt = c.write_tag(tag, val, cv[1]) - # print(wt) + wt = c.write_tag(tag, val, t) return wt except Exception: err = c.get_status() c.close() - print err - pass + print("Write Error: {} setting {} at {} to {} type {}".format(err, tag, addr, val, t)) + return False c.close() +def closeEnough(a, b): + return abs(a - b) <= 0.001 + + +def writeMicroTag(addr, tag, val, handshake=None, handshake_val=None): + addr = str(addr) + tag = str(tag) + print("handshake: {}, handshake_val: {}".format(handshake, handshake_val)) + chk_tag = tag + if not(handshake is None) and not(handshake == "None"): + chk_tag = str(handshake) + print("Handshake tag passed, using {}".format(chk_tag)) + chk_val = val + if not (handshake_val is None) and not(handshake_val == "None"): + chk_val = handshake_val + print("Handshake value passed, using {}".format(chk_val)) + attempts_allowed = 5 + attempts = 1 + + while attempts <= attempts_allowed: + try: + attempts = attempts + 1 + cv = readMicroTag(addr, tag) + print("Val Before Write: {}".format(cv)) + if cv: + if cv[1] == "REAL": + val = float(val) + chk_val = float(chk_val) + else: + val = int(val) + chk_val = int(chk_val) + wt = write(addr, tag, val, cv[1]) + if wt: + print("write: {}".format(wt)) + chk = readMicroTag(addr, chk_tag) + if chk: + print("chk: {}, chk_val: {}".format(chk, chk_val)) + if closeEnough(chk[0], chk_val): + return True + except Exception as e: + print e + return False + + def readMicroTagList(addr, tList): - c = u800Driver() + addr = str(addr) + c = plcDriver() if c.open(addr, True): vals = [] try: