@@ -105,6 +105,7 @@ def __init__(
105
105
self ._ipdpacket = bytearray (1500 )
106
106
self ._ifconfig = []
107
107
self ._initialized = False
108
+ self ._conntype = None
108
109
109
110
def begin (self ) -> None :
110
111
"""Initialize the module by syncing, resetting if necessary, setting up
@@ -187,6 +188,9 @@ def socket_connect(
187
188
can be an IP address or DNS (we'll do the lookup for you. Remote port
188
189
is integer port on other side. We can't set the local port"""
189
190
# lets just do one connection at a time for now
191
+ if conntype == self .TYPE_UDP :
192
+ # always disconnect for TYPE_UDP
193
+ self .socket_disconnect ()
190
194
while True :
191
195
stat = self .status
192
196
if stat in (self .STATUS_APCONNECTED , self .STATUS_SOCKETCLOSED ):
@@ -209,7 +213,12 @@ def socket_connect(
209
213
)
210
214
replies = self .at_response (cmd , timeout = 10 , retries = retries ).split (b"\r \n " )
211
215
for reply in replies :
212
- if reply == b"CONNECT" and self .status == self .STATUS_SOCKETOPEN :
216
+ if reply == b"CONNECT" and (
217
+ conntype == self .TYPE_TCP
218
+ and self .status == self .STATUS_SOCKETOPEN
219
+ or conntype == self .TYPE_UDP
220
+ ):
221
+ self ._conntype = conntype
213
222
return True
214
223
return False
215
224
@@ -232,6 +241,8 @@ def socket_send(self, buffer: bytes, timeout: int = 1) -> bool:
232
241
raise RuntimeError ("Didn't get data prompt for sending" )
233
242
self ._uart .reset_input_buffer ()
234
243
self ._uart .write (buffer )
244
+ if self ._conntype == self .TYPE_UDP :
245
+ return True
235
246
stamp = time .monotonic ()
236
247
response = b""
237
248
while (time .monotonic () - stamp ) < timeout :
@@ -314,6 +325,7 @@ def socket_receive(self, timeout: int = 5) -> bytearray:
314
325
315
326
def socket_disconnect (self ) -> None :
316
327
"""Close any open socket, if there is one"""
328
+ self ._conntype = None
317
329
try :
318
330
self .at_response ("AT+CIPCLOSE" , retries = 1 )
319
331
except OKError :
0 commit comments