def DBWriteByOffset(dev, db_num, offset, _type, value): offset = str(float(offset)) byte = int(offset.split('.')[0]) bit = int(offset.split('.')[1]) _type = _type.lower() _val_len = byte_array.val_len(_type, None) raw_bytearray = bytearray([0] * _val_len) if _type == 'Real'.lower(): snap7.util.set_real(raw_bytearray, 0, value) elif _type == 'Bool'.lower(): raw_bytearray = dev.read_area(snap7.snap7types.areas['DB'], db_num, byte, _val_len) snap7.util.set_bool(raw_bytearray, 0, bit, value) elif _type == 'DINT'.lower(): byte_array.set_dint(raw_bytearray, 0, value) elif _type == 'UDINT'.lower() or _type == 'DWORD'.lower(): byte_array.set_udint(raw_bytearray, 0, value) elif _type == 'Int'.lower(): snap7.util.set_int(raw_bytearray, 0, value) elif _type == 'UINT'.lower() or _type == 'WORD'.lower(): byte_array.set_uint(raw_bytearray, 0, value) elif _type == 'USINT'.lower(): byte_array.set_byte(raw_bytearray, 0, value) elif _type == 'SINT'.lower(): byte_array.set_sint(raw_bytearray, 0, value) elif _type == 'BYTE'.lower(): byte_array.set_byte(raw_bytearray, 0, value) elif 'WString'.lower() in _type: byte_array.set_wstring(raw_bytearray, 0, value, int((byte_array.val_len(_type, None) / 2) - 2)) elif 'String'.lower() in _type: # snap7.util.set_string(raw_bytearray, 0, value, byte_array.val_len(_type, None)) byte_array.set_string(raw_bytearray, 0, value, byte_array.val_len(_type, None) - 2) elif _type == 'DTL'.lower(): byte_array.set_DTL(raw_bytearray, 0, value) else: print('Unkown type', _type) dev.db_write(db_num, byte, raw_bytearray)
def DBRead(dev, db_num, length, dbitems): data = dev.read_area(snap7.snap7types.areas['DB'], db_num, 0, length) obj = DBObject() for item in dbitems.values(): value = None offset = int(item[offsetKey].split('.')[0]) _type = item[dataTypeKey].lower() if _type == 'Real'.lower(): value = snap7.util.get_real(data, offset) elif _type == 'Bool'.lower(): bit = int(item[offsetKey].split('.')[1]) value = snap7.util.get_bool(data, offset, bit) elif _type == 'DINT'.lower(): value = byte_array.get_dint(data, offset) elif _type == 'UDINT'.lower() or _type == 'DWORD'.lower(): value = byte_array.get_udint(data, offset) elif _type == 'Int'.lower(): value = snap7.util.get_int(data, offset) elif _type == 'UINT'.lower() or _type == 'WORD'.lower(): value = byte_array.get_uint(data, offset) elif _type == 'USINT'.lower(): value = byte_array.get_byte(data, offset) elif _type == 'SINT'.lower(): value = byte_array.get_sint(data, offset) elif _type == 'BYTE'.lower(): value = byte_array.get_byte(data, offset) elif 'WString'.lower() in _type: value = byte_array.get_wstring(data, offset, byte_array.val_len(_type, None)) elif 'String'.lower() in _type: # value = snap7.util.get_string(data, offset, m_dictOffsets["String"]) # value = snap7.util.get_string(data, offset, byte_array.val_len(_type, None)) value = byte_array.get_string(data, offset, byte_array.val_len(_type, None)) elif _type == 'DTL'.lower(): value = byte_array.get_DTL(data, offset) else: print('Unkown type', _type) value = None obj.__setattr__(item[nameKey], value) return obj
def DBReadTag(dev, db_num, offset, _type): offset = str(float(offset)) byte = int(offset.split('.')[0]) bit = int(offset.split('.')[1]) length = byte_array.val_len(_type, None) data = dev.read_area(snap7.snap7types.areas['DB'], db_num, byte, length) value = None _type = _type.lower() if _type == 'Real'.lower(): value = snap7.util.get_real(data, 0) elif _type == 'Bool'.lower(): value = snap7.util.get_bool(data, 0, bit) elif _type == 'DINT'.lower(): value = byte_array.get_dint(data, 0) elif _type == 'UDINT'.lower() or _type == 'DWORD'.lower(): value = byte_array.get_udint(data, 0) elif _type == 'Int'.lower(): value = snap7.util.get_int(data, 0) elif _type == 'UINT'.lower() or _type == 'WORD'.lower(): value = byte_array.get_uint(data, 0) elif _type == 'USINT'.lower(): value = byte_array.get_byte(data, 0) elif _type == 'SINT'.lower(): value = byte_array.get_sint(data, 0) elif _type == 'BYTE'.lower(): value = byte_array.get_byte(data, 0) elif 'WString'.lower() in _type: value = byte_array.get_wstring(data, 0, byte_array.val_len(_type, None)) elif 'String'.lower() in _type: # value = snap7.util.get_string(data, offset, m_dictOffsets["String"]) # value = snap7.util.get_string(data, 0, byte_array.val_len(_type, None)) value = byte_array.get_string(data, 0, byte_array.val_len(_type, None)) elif _type == 'DTL'.lower(): value = byte_array.get_DTL(data, 0) else: print('Unkown type', _type) value = None return value
def DBWriteByTag(dev, db_num, item, value): offset = int(item[offsetKey].split('.')[0]) _type = item[dataTypeKey].lower() _val_len = byte_array.val_len(_type, None) raw_bytearray = bytearray([0] * _val_len) if _type == 'Real'.lower(): snap7.util.set_real(raw_bytearray, 0, value) elif _type =='Bool'.lower(): raw_bytearray = dev.read_area(snap7.snap7types.areas['DB'], db_num, offset, _val_len) bit = int(item[offsetKey].split('.')[1]) snap7.util.set_bool(raw_bytearray, 0, bit, value) elif _type == 'DINT'.lower(): byte_array.set_dint(raw_bytearray, 0, value) elif _type == 'UDINT'.lower() or _type == 'DWORD'.lower(): byte_array.set_udint(raw_bytearray, 0, value) elif _type == 'Int'.lower(): snap7.util.set_int(raw_bytearray, 0, value) elif _type == 'UINT'.lower() or _type == 'WORD'.lower() : byte_array.set_uint(raw_bytearray, 0, value) elif _type == 'USINT'.lower(): byte_array.set_byte(raw_bytearray, 0, value) elif _type == 'SINT'.lower(): byte_array.set_sint(raw_bytearray, 0, value) elif _type == 'BYTE'.lower(): byte_array.set_byte(raw_bytearray, 0, value) elif 'String'.lower() in _type: # value = snap7.util.get_string(data, offset, m_dictOffsets["String"]) # snap7.util.set_string(raw_bytearray, 0, value, val_len(_type, None)) byte_array.set_string(raw_bytearray, 0, value, 250) elif _type == 'DTL'.lower(): byte_array.set_DTL(raw_bytearray, 0, value) else: print('Unkown type', _type) dev.db_write(db_num, offset, raw_bytearray)
def get_db_size(_dictItems, _dataTypeKey, _offsetKey): ''' Example: lstOffsets: ['0.0', '4.0', '6.0', '8.0'] lstTypes: ['Real', 'Bool', 'Int', 'String'] idx: 3 ''' lstOffsets, lstTypes = [float(x[_offsetKey]) for x in _dictItems.values()], [x[_dataTypeKey] for x in _dictItems.values()] idx = lstOffsets.index(max(lstOffsets)) # print(f"lstOffsets: {lstOffsets}, lstTypes: {lstTypes}, idx: {idx}") # lastByte = int(max(lstOffsets).split('.')[0])+(m_dictOffsets[lstTypes[idx]]) lastByte = int(str(max(lstOffsets)).split('.')[0])+(byte_array.val_len(lstTypes[idx], None)) return lastByte