File tree Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -528,19 +528,26 @@ def get_host_by_name(self, hostname):
528
528
529
529
### Socket API (TCP, UDP) ###
530
530
531
- def get_socket (self , sockets ):
532
- """Returns the first avaliable (unused) socket
533
- by the socket module.
531
+ def get_socket (self ):
532
+ """Returns an avaliable socket (INITIAL or CLOSED state).
534
533
535
534
"""
536
535
if self ._debug :
537
536
print ("*** Allocating Socket" )
538
- sock = 0
537
+
538
+ self ._uart .write (b"AT+CIPSTATUS\r \n " )
539
+ self ._read_line (100 ) # OK
540
+ self ._read_line (100 ) # table header
541
+
539
542
for sock in range (0 , FONA_MAX_SOCKETS ):
540
- if sock not in sockets :
543
+ # parse and check for INITIAL client state
544
+ self ._read_line (100 )
545
+ self ._parse_reply (b'C:' , idx = 5 )
546
+ if self ._buf .strip ('\" ' ) == "INITIAL" or self ._buf .strip ('\" ' ) == "CLOSED" :
541
547
break
542
- if self ._debug :
543
- print ("Allocated socket #" , sock )
548
+ # read out the rest of the responses
549
+ for _ in range (sock , FONA_MAX_SOCKETS ):
550
+ self ._read_line (100 )
544
551
return sock
545
552
546
553
def remote_ip (self , sock_num ):
Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ def __init__(
103
103
self ._buffer = b""
104
104
self ._timeout = 0
105
105
106
- self ._socknum = _the_interface .get_socket (SOCKETS )
106
+ self ._socknum = _the_interface .get_socket ()
107
107
SOCKETS .append (self ._socknum )
108
108
self .settimeout (self ._timeout )
109
109
@@ -210,7 +210,7 @@ def recv(self, bufsize=0):
210
210
211
211
def readline (self ):
212
212
"""Attempt to return as many bytes as we can up to but not including '\r \n '"""
213
- # print("Socket readline")
213
+ print ("Socket readline" )
214
214
stamp = time .monotonic ()
215
215
while b"\r \n " not in self ._buffer :
216
216
# there's no line already in there, read some more
You can’t perform that action at this time.
0 commit comments