def read_modbus_register(self, reg_addr, reg_qty, parse_format, attempt_count=1, attempt_interval=5):
		
		# Create request with network endianness
		struct_format = ("!BBHH")
		packed_data = struct.pack(struct_format, self.modbus_addr, self.modbus_func, reg_addr, reg_qty)
		
		packed_data_size = struct.calcsize(struct_format)
		
		# Calculate the CRC16 and append to the end
		crc = crc16.calcCRC(packed_data)
		crc = socket.htons(crc)
		struct_format = ("!BBHHH")
		packed_data = struct.pack(struct_format, self.modbus_addr, self.modbus_func, reg_addr, reg_qty, crc)
		
		#print "Packed data: " + repr(packed_data)
	
		sent = False
		reply = None
		
		while sent==False and attempt_count>0:
			attempt_count = attempt_count-1
			try:
				# Send data
				self.sock.sendall(packed_data)
				reply = self.get_modbus_response(reg_qty,parse_format)
				sent = True
			except socket.error:
				if (attempt_count>0):
					logging.error("Connection closed by Modbus device %s:%s. Retrying in %s seconds..."%(self.type,self.id,attempt_interval))
					time.sleep(attempt_interval)
					self.connect()
				else:
					logging.error("Connection closed by Modbus device %s:%s."%(self.type,self.id))
		
		return reply
Example #2
0
    def modbusReadReg(self, addr, modbus_func, reg_addr, reg_qty):

        # Create request with network endianness
        struct_format = ("!BBHH")
        packed_data = struct.pack(struct_format, addr, modbus_func, reg_addr, reg_qty)

        packed_data_size = struct.calcsize(struct_format)

        # Calculate the CRC16 and append to the end
        crc = crc16.calcCRC(packed_data)
        crc = socket.htons(crc)
        struct_format = ("!BBHHH")
        packed_data = struct.pack(struct_format, addr, modbus_func, reg_addr, reg_qty, crc)

        #print "Packed data: " + repr(packed_data)

        sent = False
        while sent == False:
            try:
                # Send data
                self.sock.sendall(packed_data)
                response = self.getResponse(reg_qty)
                sent = True
            except socket.error:
                print ("Modbus Connection was closed by Modbus Server. "
                       "Retrying in 5 seconds...")
                time.sleep(5)
                self.connect()
    
        return response
    def read_modbus_register(self,
                             reg_addr,
                             reg_qty,
                             parse_format,
                             attempt_count=1,
                             attempt_interval=5):

        # Create request with network endianness
        struct_format = ("!BBHH")
        packed_data = struct.pack(struct_format, self.modbus_addr,
                                  self.modbus_func, reg_addr, reg_qty)

        packed_data_size = struct.calcsize(struct_format)

        # Calculate the CRC16 and append to the end
        crc = crc16.calcCRC(packed_data)
        crc = socket.htons(crc)
        struct_format = ("!BBHHH")
        packed_data = struct.pack(struct_format, self.modbus_addr,
                                  self.modbus_func, reg_addr, reg_qty, crc)

        #print "Packed data: " + repr(packed_data)

        sent = False
        reply = None

        while sent == False and attempt_count > 0:
            attempt_count = attempt_count - 1
            try:
                # Send data
                self.sock.sendall(packed_data)
                reply = self.get_modbus_response(reg_qty, parse_format)
                sent = True
            except socket.error:
                if (attempt_count > 0):
                    logging.error(
                        "Connection closed by Modbus device %s:%s. Retrying in %s seconds..."
                        % (self.type, self.id, attempt_interval))
                    time.sleep(attempt_interval)
                    self.connect()
                else:
                    logging.error("Connection closed by Modbus device %s:%s." %
                                  (self.type, self.id))

        return reply