Exemplo n.º 1
0
def read_value(data_type, result, byte_index=0, bool_index=0):
    # 'FLOAT'
    if data_type == 1:
        return get_real(result, byte_index)

    # 'INT'
    elif data_type == 2:
        return get_int(result, byte_index)

    # 'DINT'
    elif data_type == 3:
        return get_dint(result, byte_index)

    # 'WORD'
    elif data_type == 4:
        return get_word(result, byte_index)

    # 'DWORD'
    elif data_type == 5:
        return get_dword(result, byte_index)

    # 'BYTE'
    elif data_type == 6:
        return get_byte(result, byte_index)

    # 'BOOL'
    elif data_type == 7:
        return get_bool(result, byte_index, bool_index)

    else:
        assert ValueError, 'data_type is not useful'
Exemplo n.º 2
0
def unpack(array_read, array_order, variables):
    buffer = []
    j = 0
    for loop in range(len(array_order)):
        a = array_order[loop][0]
        print(a)
        start_index = int(math.modf(variables[a]['address'])[1])
        i = 0
        while i < len(array_order[loop]):
            order = array_order[loop][i]
            byte_index = int(math.modf(variables[order]['address'])[1])
            type = variables[order]['data_type']
            if type == 7:
                bool_index = round(math.modf(variables[order]['address'])[0] * 10)
                buffer.append([get_bool(array_read[loop], byte_index - start_index, bool_index)])
            if type == 6:
                buffer.append([get_byte(array_read[loop], byte_index - start_index)])
            if type == 4:
                buffer.append([get_word(array_read[loop], byte_index - start_index)])
            if type == 5:
                buffer.append([get_dword(array_read[loop], byte_index - start_index)])
            if type == 2:
                buffer.append([get_int(array_read[loop], byte_index - start_index)])
            if type == 3:
                buffer.append([get_dint(array_read[loop], byte_index - start_index)])
            if type == 1:
                buffer.append([get_real(array_read[loop], byte_index - start_index)])
            buffer[j].append(order)
            j = j + 1
            i = i + 2

    order1(buffer)  # 按最后一列顺序排序
    return buffer
    def read(self,datatype,*args):
        self.plc2pc_data=self.plc.db_read(self.db_number,self.db_read_start,self.db_read_size)

        if datatype=='bool':
            return util.get_bool(self.plc2pc_data,args[0],args[1])
        elif datatype=='int':
            return util.get_int(self.plc2pc_data,args[0])
        elif datatype==('dint' or 'dword'):
            return util.get_dword(self.plc2pc_data,args[0])
        elif datatype=='real':
            return util.get_real(self.plc2pc_data,args[0])
        elif datatype=='string':
            return util.get_string(self.plc2pc_data,args[0],args[1])
Exemplo n.º 4
0
 def set_value(self, byte):
     self.valor_anterior = self.valor
     if self.type_var == "R":
         self.valor = round(util.get_real(byte, self.address),2)
     elif self.type_var == "I" or self.type_var == "W":
         self.valor = round(util.get_int(byte, self.address),2)
     elif self.type_var == "DW" or self.type_var == "DI":
         self.valor = round(util.get_dword(byte, self.address),2)
     elif self.type_var == "B":
         self.valor = util.get_bool(byte, self.address, self.bit)
     if self.valor_anterior != self.valor:
         self.write = True
     else:
         self.write = False
Exemplo n.º 5
0
def query(client):
    """"""
    # db read db, start, size
    # attention!!! size is byte
    data1 = client.db_read(1, 1, 2)
    print(data1)
    data_1 = util.get_int(data1, 0)
    print(data_1)

    data2 = client.db_read(1, 2, 4)
    print(data2)
    data_2 = util.get_dword(data2, 0)
    print(data_2)

    data2 = client.db_read(1, 1, 10)
    print(data2)
 def set_value(self, byte):
     self.valor_anterior = self.valor
     if self.type_var == "R":
         self.valor = util.get_real(byte, self.address)
     if self.type_var == "I" or self.type_var == "W":
         self.valor = util.get_int(byte, self.address)
     if self.type_var == "DW" or self.type_var == "DI":
         self.valor = int(util.get_dword(byte, self.address))
         # print str(self.db )+"."+str(self.address )+" DI: "+ str(self.valor)
     if self.type_var == "B":
         #print self.address
         value_t = util.get_bool(byte, int(self.address), int(self.bit))
         if value_t:
             self.valor = 1
         else:
             self.valor = 0
Exemplo n.º 7
0
def read_value(data_type, result, byte_index=0, bool_index=0):
    if data_type == 'FLOAT':
        return get_real(result, byte_index)

    elif data_type == 'INT':
        return get_int(result, byte_index)

    elif data_type == 'DINT':
        return get_dint(result, byte_index)

    elif data_type == 'WORD':
        return get_word(result, byte_index)

    elif data_type == 'BYTE':
        return get_byte(result, byte_index)

    elif data_type == 'BOOL':
        return get_bool(result, byte_index, bool_index)

    elif data_type == 'DWORD':
        return get_dword(result, byte_index)

    else:
        assert ValueError, 'data_type is not useful'
Exemplo n.º 8
0
 def test_read_double_word(self):
     data = self.client.db_read(0, 500, 8 * 4)
     self.assertEqual(get_dword(data, 0), 0x00000000)
     self.assertEqual(get_dword(data, 8), 0x12345678)
     self.assertEqual(get_dword(data, 16), 0x1234ABCD)
     self.assertEqual(get_dword(data, 24), 0xFFFFFFFF)
Exemplo n.º 9
0
def s7_multi_read(plc, tag_type, tag_address,data_type,tag_name):
    '''
    从s7_read_excel处理完的变量表中批量读取变量
    '''
    # print('s7multiread函数')
    # print(tag_type, tag_address)
    taglens = len(tag_type)
    # print(taglens)
    # l = taglens  # 变量表长度,如果大于20 必须分批读取 snp7
    # x = (l // 20) +1  # 取整加1 组数
    # y = l % 20  # 取余数
    # dataitemsgroup=[]
    # for i in range(x):
    #     dataitemsgroup.append("data_items"+str(i))

    # for i in dataitemsgroup:
    #     if i < x:
    #         dataitemsgroup = (S7DataItem * pass)()
    #     data_items[i].Area = areas[tag_type[i]]  # 数据类型
    #     data_items[i].WordLen = ctypes.c_int32(S7WLByte)
    #     data_items[i].Result = ctypes.c_int32(0)
    #     data_items[i].DBNumber = ctypes.c_int32(0)  # DB块 非DB写0
    #     data_items[i].Start = ctypes.c_int32(int(tag_address[i].split('.')[0]))  # byte地址
    #     data_items[i].Amount = ctypes.c_int32(8)  # 读取8位
        #     val = val + plc.read_multi_vars(data_items[20 * a:20 * (a + 1)])
        #     a += 1
        #     n += 1
        # if n == x and y != 0:
        #     val = val + plc.read_multi_vars(data_items[20 * a:20 * a + y])
        #

    # a = 0  # 每一组变量的上标
    # val = []  # 初始化列表 每一组变量值
    # for n in range(x):
    #     if n < x:
    #         val = val + plc.read_multi_vars(data_items[20 * a:20 * (a + 1)])
    #         a += 1
    #         n += 1
    #     if n == x and y != 0:
    #         val = val + plc.read_multi_vars(data_items[20 * a:20 * a + y])
    # val2 = val
    # return val2
    data_items = (S7DataItem * taglens)()  # 括号 数组
    # print(type(data_items))
    # fixme 如果只有一个变量的情况 可能会有bug
    # 生成 data_items 待读取的变量结构体
    for i in range(taglens):
        # print(i)
        data_items[i].Area = areas[tag_type[i]]  # 数据类型
        data_items[i].WordLen = ctypes.c_int32(S7WLByte)
        data_items[i].Result = ctypes.c_int32(0)
        data_items[i].DBNumber = ctypes.c_int32(0)  # DB块 非DB写0
        data_items[i].Start = ctypes.c_int32(int(tag_address[i].split('.')[0]))  # byte地址
        data_items[i].Amount = ctypes.c_int32(8)  # 读取8位

        # data_items[i].Bit = tag_address[i].split('.')[1] # 偏移量
    # print(type(data_items))
    # print(type(data_items[2]))
    # print(type(data_items[0:20]))
    # create buffers to receive the data
    # use the Amount attribute on each item to size the buffer
    for di in data_items:
        # create the buffer
        buffer = ctypes.create_string_buffer(di.Amount)

        # cast the pointer to the buffer to the required type
        pBuffer = ctypes.cast(ctypes.pointer(buffer),
                              ctypes.POINTER(ctypes.c_uint8))
        di.pData = pBuffer
        # di.Bit = di.Bit

    # fixme 分批读取 snap7单次20个以上报错
    def readten(data_items):
        """
        :type data_items: object
        """
        # print(type(data_items))
        # print(type(data_items[10:20]))
        # a=plc.read_multi_vars(data_items[10:20])
        # print(a)
        l = len(data_items)  # 变量表长度,如果大于20 必须分批读取 snp7
        x = l // 20  # 取整
        y = l % 20  # 取余数
        a = 0  # 每一组变量的上标
        val = []  # 初始化列表 每一组变量值
        for n in range(x):
            if n < x:
                val = val + plc.read_multi_vars(data_items)
                a += 1
                n += 1
            if n == x and y != 0:
                val = val + plc.read_multi_vars(data_items)
        val2 = val
        return val2

    result, data_items = plc.read_multi_vars(data_items)
    # print('读取的原始数据',data_items)
    ttt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    # for di in data_items:
    #     check_error(di.Result)

    result_values = []
    # function to cast bytes to match data_types[] above
    # byte_to_value = [util.get_bool, util.get_real, util.get_int,util.get_dword,util.get_string]
    # unpack and test the result of each read
    # todo 做一个函数列表,合并循环。逻辑上需要先读取,后get_bool
    for i in range(0, len(data_items)):
        # btv = byte_to_value[i]
        ddd = data_items[i]
        # print(ddd)
        d_bit = int(tag_address[i].split('.')[1])
        # value = btv(di.pData, 0)
        if data_type[i] == 'Bool':
            value = util.get_bool(ddd.pData, 0, d_bit)
        elif data_type[i] == 'Real':
            value = util.get_real(ddd.pData, d_bit)
        elif data_type[i] == 'Int':
            value = util.get_int(ddd.pData, d_bit)
        elif data_type[i] == 'Dword':
            value = util.get_dword(ddd.pData, d_bit)
        elif data_type[i] == 'String':
            value = util.get_string(ddd.pData, d_bit)
        # assert isinstance(value, object)
        result_values.append(value)
    # print(result_values)

    # todo
    # client.disconnect()
    # client.destroy()

    siemensdata = dict(zip(tag_name, result_values))
    print(siemensdata)
    return siemensdata, ttt
Exemplo n.º 10
0
    def s7_multi_read(plc, tag_type, tag_address, data_type, tag_name):
        '''
            从s7_read_excel处理完的变量表中批量读取变量
        '''
        taglens = len(tag_type)
        data_items = (S7DataItem * taglens)()  # 括号 数组

        # fixme 如果只有一个变量的情况 可能会有bug
        # 生成 data_items 待读取的变量结构体
        for i in range(taglens):
            # print(i)
            data_items[i].Area = areas[tag_type[i]]  # 数据类型
            data_items[i].WordLen = ctypes.c_int32(S7WLByte)
            data_items[i].Result = ctypes.c_int32(0)
            data_items[i].DBNumber = ctypes.c_int32(0)  # DB块 非DB写0
            data_items[i].Start = ctypes.c_int32(
                int(tag_address[i].split('.')[0]))  # byte地址
            data_items[i].Amount = ctypes.c_int32(8)  # 读取8位

        for di in data_items:
            # create the buffer
            buffer = ctypes.create_string_buffer(di.Amount)
            # cast the pointer to the buffer to the required type
            pBuffer = ctypes.cast(ctypes.pointer(buffer),
                                  ctypes.POINTER(ctypes.c_uint8))
            di.pData = pBuffer

        # fixme 分批读取 snap7单次20个以上报错 数组切片报错 暂时未使用
        def readten(data_items):
            """
            :type data_items: object
            """
            # print(type(data_items))
            # print(type(data_items[10:20]))
            # a=plc.read_multi_vars(data_items[10:20])
            # print(a)
            l = len(data_items)  # 变量表长度,如果大于20 必须分批读取 snp7
            x = l // 20  # 取整
            y = l % 20  # 取余数
            a = 0  # 每一组变量的上标
            val = []  # 初始化列表 每一组变量值
            for n in range(x):
                if n < x:
                    val = val + plc.read_multi_vars(data_items)
                    a += 1
                    n += 1
                if n == x and y != 0:
                    val = val + plc.read_multi_vars(data_items)
            val2 = val
            return val2

        result, data_items = plc.read_multi_vars(data_items)
        # print('读取的原始数据',data_items)
        ttt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        for di in data_items:
            check_error(di.Result)

        result_values = []
        # function to cast bytes to match data_types[] above
        # byte_to_value = [util.get_bool, util.get_real, util.get_int,util.get_dword,util.get_string]
        # unpack and test the result of each read
        # todo 做一个函数列表,合并循环。逻辑上需要先读取,后get_bool
        for i in range(0, len(data_items)):

            ddd = data_items[i]
            d_bit = int(tag_address[i].split('.')[1])
            if data_type[i] == 'Bool':
                value = util.get_bool(ddd.pData, 0, d_bit)
            elif data_type[i] == 'Real':
                value = util.get_real(ddd.pData, d_bit)
            elif data_type[i] == 'Int':
                value = util.get_int(ddd.pData, d_bit)
            elif data_type[i] == 'Dword':
                value = util.get_dword(ddd.pData, d_bit)
            elif data_type[i] == 'String':
                value = util.get_string(ddd.pData, d_bit)
            result_values.append(value)

        # fixme
        # client.disconnect()
        # client.destroy()

        siemensdata = dict(zip(tag_name, result_values))
        # print(siemensdata)
        return siemensdata, ttt
Exemplo n.º 11
0
    data_items[i].DBNumber = ctypes.c_int32(int(numero))
    data_items[i].Start = ctypes.c_int32(
        8 * int(byte) + int(bit)
    )  # al estar en modo bit es 8 bits * 5 bytes ya que en el DB0.DBX5.0
    data_items[i].Amount = ctypes.c_int32(getattr(snap7types, tipo))

for di in data_items:
    # create the buffer
    buffer = ctypes.create_string_buffer(di.Amount)

    # cast the pointer to the buffer to the required type
    pBuffer = ctypes.cast(ctypes.pointer(buffer),
                          ctypes.POINTER(ctypes.c_uint8))
    di.pData = pBuffer

result, data_items = plc.read_multi_vars(data_items)
for di in data_items:
    check_error(di.Result)

result_values = []
for di in data_items:
    if di.WordLen == getattr(snap7types, 'S7WLBit'):
        value = util.get_bool(di.pData, 0, 0)
    elif di.WordLen == getattr(snap7types, 'S7WLByte'):
        value = util.get_int(di.pData, 0)
    elif di.WordLen == getattr(snap7types, 'S7WLDWord'):
        value = util.get_dword(di.pData, 0)
    elif di.WordLen == getattr(snap7types, 'S7WLReal'):
        value = util.get_real(di.pData, 0)
    result_values.append(value)
print(result_values)