def _serve_thread(self): while True: client_socket, addr = self.socket.accept() print("Connected with " + addr[0] + ":" + str(addr[1])) try: while True: try: packet = self.receive_packet(client_socket) if packet == 0: break except: break packet = EtherbonePacket(packet) packet.decode() record = packet.records.pop() # Wait for lock while self.lock: time.sleep(0.01) # Set lock self.lock = True # Handle writes: if record.writes != None: self.comm.write(record.writes.base_addr, record.writes.get_datas()) # Handle reads if record.reads != None: max_length = { "CommUART": 256, "CommUDP": 1, }.get(self.comm.__class__.__name__, 1) bursts = { "CommUART": ["incr", "fixed"] }.get(self.comm.__class__.__name__, ["incr"]) reads = [] for addr, length, burst in _read_merger( record.reads.get_addrs(), max_length=max_length, bursts=bursts): reads += self.comm.read(addr, length, burst) record = EtherboneRecord() record.writes = EtherboneWrites(datas=reads) record.wcount = len(record.writes) packet = EtherbonePacket() packet.records = [record] packet.encode() self.send_packet(client_socket, packet) # release lock self.lock = False finally: print("Disconnect") client_socket.close()
def read(self, addr, length=None, burst="incr"): length_int = 1 if length is None else length # Prepare packet record = EtherboneRecord() incr = (burst == "incr") record.reads = EtherboneReads(addrs=[ self.base_address + addr + 4 * incr * j for j in range(length_int) ]) record.rcount = len(record.reads) # Send packet packet = EtherbonePacket() packet.records = [record] packet.encode() self.send_packet(self.socket, packet) # Receive response packet = EtherbonePacket(self.receive_packet(self.socket)) packet.decode() datas = packet.records.pop().writes.get_datas() if self.debug: for i, data in enumerate(datas): print("read 0x{:08x} @ 0x{:08x}".format( data, self.base_address + addr + 4 * i)) return datas[0] if length is None else datas
def _serve_thread(self): while True: client_socket, addr = self.socket.accept() print("Connected with " + addr[0] + ":" + str(addr[1])) try: while True: try: packet = self.receive_packet(client_socket) if packet == 0: break except: break packet = EtherbonePacket(packet) packet.decode() record = packet.records.pop() # wait for lock while self.lock: time.sleep(0.01) # set lock self.lock = True # handle writes: if record.writes != None: self.comm.write(record.writes.base_addr, record.writes.get_datas()) # handle reads if record.reads != None: reads = [] for addr in record.reads.get_addrs(): reads.append(self.comm.read(addr)) record = EtherboneRecord() record.writes = EtherboneWrites(datas=reads) record.wcount = len(record.writes) packet = EtherbonePacket() packet.records = [record] packet.encode() self.send_packet(client_socket, packet) # release lock self.lock = False finally: print("Disconnect") client_socket.close()
def write(self, addr, datas): datas = datas if isinstance(datas, list) else [datas] record = EtherboneRecord() record.writes = EtherboneWrites(base_addr=self.base_address + addr, datas=[d for d in datas]) record.wcount = len(record.writes) packet = EtherbonePacket() packet.records = [record] packet.encode() self.send_packet(self.socket, packet) if self.debug: for i, data in enumerate(datas): print("write 0x{:08x} @ 0x{:08x}".format(data, self.base_address + addr + 4*i))
def write(self, addr, datas): datas = datas if isinstance(datas, list) else [datas] length = len(datas) record = EtherboneRecord() record.writes = EtherboneWrites(base_addr=addr, datas=iter(datas)) record.wcount = len(record.writes) packet = EtherbonePacket() packet.records = [record] packet.encode() self.socket.sendto(bytes(packet), (self.server, self.port)) if self.debug: for i, value in enumerate(datas): print("write {:08x} @ {:08x}".format(value, addr + 4 * i))
def read(self, addr, length=None): length_int = 1 if length is None else length record = EtherboneRecord() record.reads = EtherboneReads( addrs=[addr + 4 * j for j in range(length_int)]) record.rcount = len(record.reads) packet = EtherbonePacket() packet.records = [record] packet.encode() self.socket.sendto(bytes(packet), (self.server, self.port)) datas, dummy = self.socket.recvfrom(8192) packet = EtherbonePacket(datas) packet.decode() datas = packet.records.pop().writes.get_datas() if self.debug: for i, value in enumerate(datas): print("read {:08x} @ {:08x}".format(value, addr + 4 * i)) return datas[0] if length is None else datas
def read(self, addr, length=None): length_int = 1 if length is None else length # prepare packet record = EtherboneRecord() record.reads = EtherboneReads( addrs=[addr + 4 * j for j in range(length_int)]) record.rcount = len(record.reads) # send packet packet = EtherbonePacket() packet.records = [record] packet.encode() self.send_packet(self.socket, packet[:]) # receive response packet = EtherbonePacket(self.receive_packet(self.socket)) packet.decode() datas = packet.records.pop().writes.get_datas() if self.debug: for i, data in enumerate(datas): print("read {:08x} @ {:08x}".format(data, addr + 4 * i)) return datas[0] if length is None else datas