def readM(client,nameStr): if nameStr[0]=='M' or nameStr[0]=='m': if nameStr[1]>='0' and nameStr[1]<='9': addr=int(nameStr.split('.')[0][1:]) bit=int(nameStr.split('.')[1]) if bit>7: print "M bit out of range" return m_data = client.read_area(0x83, 0, addr, 1) return (struct.unpack('!B', m_data)[0]>>bit)&1 elif nameStr[1]=="B" or nameStr[1]=='b': addr=int(nameStr[2:]) m_data=client.read_area(0x83, 0, addr, 1) return struct.unpack('!b', m_data)[0] elif nameStr[1]=="W" or nameStr[1]=='w': addr=int(nameStr[2:]) m_data=client.read_area(0x83, 0, addr, 2) return struct.unpack('!h', m_data)[0] elif nameStr[1]=="D" or nameStr[1]=='d': addr=int(nameStr[2:]) m_data=client.read_area(0x83, 0, addr, 4) return struct.unpack('!i', m_data)[0] else: print "input error" return
def __read_QB(self, client: snap7.client.Client, offset, bitnumber): v_data = client.read_area(snap7types.S7AreaPA, 0, offset, 1) s = struct.unpack('!B', v_data)[0] if (int(struct.unpack('!B', v_data)[0]) & pow(2, bitnumber) != 0): return 1 else: return 0
def readM(client, nameStr): if nameStr[0] == 'M' or nameStr[0] == 'm': if nameStr[1] >= '0' and nameStr[1] <= '9': addr = int(nameStr.split('.')[0][1:]) if addr >= 0 and addr <= 31: bit = int(nameStr.split('.')[1]) if bit > 7: print "M bit out of range" return m_data = client.read_area(0x83, 0, addr, 1) return (struct.unpack('!B', m_data)[0] >> bit) & 1 else: print "address out of range" return elif nameStr[1] == "B" or nameStr[1] == 'b': addr = int(nameStr[2:]) if addr >= 0 and addr <= 31: m_data = client.read_area(0x83, 0, addr, 1) return struct.unpack('!B', m_data)[0] else: print "address out of range" return elif nameStr[1] == "W" or nameStr[1] == 'w': addr = int(nameStr[2:]) if addr >= 0 and addr <= 30: m_data = client.read_area(0x83, 0, addr, 2) return struct.unpack('!H', m_data)[0] else: print "address out of range" return elif nameStr[1] == "D" or nameStr[1] == 'd': if nameStr[-1] == "F" or nameStr[-1] == "f": addr = int(nameStr[2:-1]) else: addr = int(nameStr[2:]) if addr >= 0 and addr <= 28: m_data = client.read_area(0x83, 0, addr, 4) if nameStr[-1] == "F" or nameStr[-1] == "f": return struct.unpack('!f', m_data)[0] else: return struct.unpack('!I', m_data)[0] else: print "address out of range" return else: print "input error" return
def __read_VW(self, client: snap7.client.Client, offset): try: v_data = client.read_area(snap7types.areas.DB, 1, offset, 2) data = int.from_bytes(v_data, byteorder='big', signed=False) return data #logging.info('read_VW(%s) succeed' % ) except Exception as e: time.sleep(0.003)
def __read_VB(self, client: snap7.client.Client, offset: int, bitnumber: int): v_data = client.read_area(snap7types.areas.DB, 1, offset, 1) #data = int.from_bytes(v_data,byteorder='big',signed=False) data = v_data[0] & (1 << bitnumber) if data > 0: return 1 else: return 0
def readV(client, nameStr): if nameStr[0] == 'V' or nameStr[0] == 'v': if nameStr[1] >= '0' and nameStr[1] <= '9': addr = int(nameStr.split('.')[0][1:]) # if addr >=0 and addr<=16359: bit = int(nameStr.split('.')[1]) if bit > 7: print "M bit out of range" return m_data = client.read_area(0x84, 1, addr, 1) return (struct.unpack('!B', m_data)[0] >> bit) & 1 # else: # print "address out of range" # return elif nameStr[1] == "B" or nameStr[1] == 'b': addr = int(nameStr[2:]) m_data = client.read_area(0x84, 1, addr, 1) #area, dbnumber, start, amount return struct.unpack('!B', m_data)[0] elif nameStr[1] == "W" or nameStr[1] == 'w': addr = int(nameStr[2:]) m_data = client.read_area(0x84, 1, addr, 2) return struct.unpack('!H', m_data)[0] elif nameStr[1] == "D" or nameStr[1] == 'd': if nameStr[-1] == "F" or nameStr[-1] == "f": addr = int(nameStr[2:-1]) else: addr = int(nameStr[2:]) m_data = client.read_area(0x84, 1, addr, 4) if nameStr[-1] == "F" or nameStr[-1] == "f": return struct.unpack('!f', m_data)[0] else: return struct.unpack('!I', m_data)[0] else: print "input error" return
def readQ(client,nameStr): if nameStr[0]=='Q' or nameStr[0]=='q': if nameStr[1]>='0' and nameStr[1]<='3': addr=int(nameStr.split('.')[0][1:]) bit=int(nameStr.split('.')[1]) if bit>7: print "Q bit out of range" return m_data = client.read_area(0x82, 0, addr, 1) return (struct.unpack('!B', m_data)[0]>>bit)&1 else: print "output out of range" return else: print "input error" return
def __read_QD(self, client: snap7.client.Client, offset: int): v_data = client.read_area(snap7types.S7AreaPA, 0, offset, 4) data = struct.unpack("!f", v_data)[0] return data
def __read_QW(self, client: snap7.client.Client, offset: int): v_data = client.read_area(snap7types.S7AreaPA, 1, offset, 2) data = int.from_bytes(v_data, byteorder='big', signed=False) return data
def __read_VD(self, client, offset): v_data = client.read_area(snap7types.areas.DB, 1, offset, 4) data = struct.unpack("!f", v_data)[0] return data
def __read_DB_Float(self, client: snap7.client.Client, dbnumber, offset): v_data = client.read_area(snap7types.S7AreaDB, dbnumber, offset, 4) data = struct.unpack("!f", v_data)[0] return data
def __read_DB_Int(self, client, dbnumber, offset): v_data = client.read_area(snap7types.S7AreaDB, dbnumber, offset, 2) data = int.from_bytes(v_data, byteorder='big', signed=False) return data
def __read_I(self, client, dataType, offset, bitnumber=None): v_data = client.read_area(snap7types.S7AreaPE, 0, offset, 1) if (int(struct.unpack('!B', v_data)[0]) & pow(2, bitnumber) != 0): return 1 else: return 0