def buildPacket(self, message): ''' Creates a ready to send modbus packet Built off of a modbus request/response :param message: The request/response to send :return: The built packet ''' data = b2a_hex(message.encode()) packet = '%02x%02x%s' % (message.unit_id, message.function_code, data) packet = '%c%s%02x%s' % (self.__start, packet, computeLRC(packet), self.__end) return packet
def buildPacket(self, message): ''' Creates a ready to send modbus packet Built off of a modbus request/response :param message: The request/response to send :return: The encoded packet ''' encoded = message.encode() buffer = struct.pack('>BB', message.unit_id, message.function_code) checksum = computeLRC(encoded + buffer) params = (message.unit_id, message.function_code, b2a_hex(encoded)) packet = '%02x%02x%s' % params packet = '%c%s%02x%s' % (self.__start, packet, checksum, self.__end) return packet.upper()
def receiveTres(self, socket, cmd): crc = ('%02x' % computeLRC(a2b_hex(cmd))).encode() send = (':' + cmd + crc + '\r\n').upper() print "Sending data: " + send socket.send(send) print "Receiving data..." message = self.recv_timeout(socket) if (len(message) > 0): print 'Received: ' + message hex_val = a2b_hex(message[7:len(message) - 3]) return int(hex_val.encode('hex'), 16) return None
def buildPacket(self, message): ''' Creates a ready to send modbus packet Built off of a modbus request/response :param message: The request/response to send :return: The encoded packet ''' encoded = message.encode() buffer = struct.pack('>BB', message.unit_id, message.function_code) checksum = computeLRC(encoded + buffer) packet = bytearray() params = (message.unit_id, message.function_code) packet.extend(self.__start) packet.extend(('%02x%02x' % params).encode()) packet.extend(b2a_hex(encoded)) packet.extend(('%02x' % checksum).encode()) packet.extend(self.__end) return bytes(packet).upper()
def buildPacket(self, message): ''' Creates a ready to send modbus packet Built off of a modbus request/response :param message: The request/response to send :return: The encoded packet ''' encoded = message.encode() buffer = struct.pack('>BB', message.unit_id, message.function_code) checksum = computeLRC(encoded + buffer) packet = bytearray() params = (message.unit_id, message.function_code) packet.extend(self._start) packet.extend(('%02x%02x' % params).encode()) packet.extend(b2a_hex(encoded)) packet.extend(('%02x' % checksum).encode()) packet.extend(self._end) return bytes(packet).upper()
def receiveCuatro(self, socket, cmd): crc = ('%02x' % computeLRC(a2b_hex(cmd))).encode() send = (':' + cmd + crc + '\r\n').upper() print "Sending data: " + send socket.send(send) print "Receiving data..." message = self.recv_timeout(socket) if (len(message) > 0): print 'Received: ' + message hex_val = a2b_hex(message[7:len(message) - 4]) return int(hex_val.encode('hex'), 16) return None #tiempo_llenado(10079) #update=updateBBDD(10079,1000,5,'1900-01-01 00:01:00.000')