diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index fdbc508..c713417 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -315,7 +315,7 @@ def remote_ip(self, socket_num): @property def link_status(self): - """"Returns if the PHY is connected.""" + """ "Returns if the PHY is connected.""" if self._chip_type == "w5500": data = self.read(REG_PHYCFGR, 0x00) return data[0] & 0x01 @@ -553,10 +553,11 @@ def get_socket(self): print("Allocated socket #{}".format(sock)) return sock - def socket_listen(self, socket_num, port): - """Start listening on a socket (TCP mode only). + def socket_listen(self, socket_num, port, conn_mode=SNMR_TCP): + """Start listening on a socket (default TCP mode). :parm int socket_num: socket number :parm int port: port to listen on + :parm int conn_mode: connection mode SNMR_TCP (default) or SNMR_UDP """ assert self.link_status, "Ethernet cable disconnected!" if self._debug: @@ -567,7 +568,7 @@ def socket_listen(self, socket_num, port): ) # Initialize a socket and set the mode self.src_port = port - res = self.socket_open(socket_num, conn_mode=SNMR_TCP) + res = self.socket_open(socket_num, conn_mode=conn_mode) self.src_port = 0 if res == 1: raise RuntimeError("Failed to initalize the socket.") @@ -575,7 +576,7 @@ def socket_listen(self, socket_num, port): self._send_socket_cmd(socket_num, CMD_SOCK_LISTEN) # Wait until ready status = [SNSR_SOCK_CLOSED] - while status[0] not in (SNSR_SOCK_LISTEN, SNSR_SOCK_ESTABLISHED): + while status[0] not in (SNSR_SOCK_LISTEN, SNSR_SOCK_ESTABLISHED, SNSR_SOCK_UDP): status = self._read_snsr(socket_num) if status[0] == SNSR_SOCK_CLOSED: raise RuntimeError("Listening socket closed.") diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 3b98267..aa9bd16 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -181,6 +181,13 @@ def bind(self, address): if ip_address != current_ip: _the_interface.ifconfig = (ip_address, subnet_mask, gw_addr, dns) self._listen_port = address[1] + # For UDP servers we need to open the socket here because we won't call + # listen + if self._sock_type == SOCK_DGRAM: + _the_interface.socket_listen( + self.socknum, self._listen_port, wiznet5k.adafruit_wiznet5k.SNMR_UDP + ) + self._buffer = b"" def listen(self, backlog=None): """Listen on the port specified by bind.