def request_metadata(self, attribute): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.metadata_request(attribute) self.socket.send(packet) data = self.socket.recv(HEADER_SIZE) length = int.from_bytes(data[6:10], byteorder='big') if (data[0] != COMMAND_META_RESPONSE): logging.error("Expected response code %s, but got %s", COMMAND_META_RESPONSE, data[0]) return # read data data = bytearray() while (len(data) < length): packet = self.socket.recv(length - len(data)) data = data + packet return data.decode("utf-8")
def request_generic(self, request_code, response_code=None): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.generic_request(request_code) self.socket.send(packet) if response_code is not None: # read header and get length field data = self.socket.recv(HEADER_SIZE) length = int.from_bytes(data[6:10], byteorder='big') if (data[0] != response_code): logging.error("Expected response code %s, but got %s", response_code, data[0]) # read data data = bytearray() while (len(data) < length): packet = self.socket.recv(length - len(data)) data = data + packet return data
def connect(self): try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((self.ip, self.port)) except IOError: self.socket = None raise SigmaTCPException("Could not connect to {}:{}".format( self.ip, self.port))
def write_memory(self, addr, data): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.write_request(addr, data) self.socket.send(packet)
def program_checksum(self): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.generic_request(COMMAND_CHECKSUM) self.socket.send(packet) data = self.socket.recv(HEADER_SIZE + 16) # remove the header data = data[HEADER_SIZE:] return data
def read_memory(self, addr, length): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.read_request(addr, length) self.socket.send(packet) data = self.socket.recv(HEADER_SIZE + length) # remove the header data = data[HEADER_SIZE:] return data
def readwrite_gpio(self, rw, pin, value): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.gpio_request(rw, pin, value) self.socket.send(packet) data = self.socket.recv(HEADER_SIZE + 1) # remove the header data = data[HEADER_SIZE:] return data
def write_eeprom_from_xml(self, xmldata): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") packet = self.write_eeprom_content_request(xmldata) self.socket.send(packet) result = int.from_bytes(self.socket.recv(1), byteorder='big', signed=False) if result == 1: return (True) else: return False
def write_eeprom_from_file(self, filename): if self.socket is None: if self.autoconnect: self.connect() else: raise SigmaTCPException("Not connected") if (os.path.exists(filename)): packet = self.write_eeprom_file_request(os.path.abspath(filename)) self.socket.send(packet) result = int.from_bytes(self.socket.recv(1), byteorder='big', signed=False) if result == 1: return (True) else: return False else: raise IOError("{} does not exist".format(filename))