How to know how many bytes available to usocket.recv() ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
water
Posts: 30
Joined: Sun Sep 24, 2017 9:16 am

How to know how many bytes available to usocket.recv() ?

Post by water » Tue Jul 30, 2019 10:41 pm

How to know how many bytes available to usocket.recv() ?
:?:
Thanks.

User avatar
pythoncoder
Posts: 3651
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to know how many bytes available to usocket.recv() ?

Post by pythoncoder » Wed Jul 31, 2019 5:53 am

There is no way to know in advance how many bytes will be received (as far as I know). When you issue the .recv() method you will get back as many bytes as were available in the timeout period. You can then check the size of the received bytes object or parse it and look for a delimiter.

Code: Select all

bytes_rx = my_socket.recv()
print(len(bytes_rx))
eol = bytes_rx.find(b'\n')
if eol > 0:
    # Got a line of text
else:
    # need to try again and concatenate
Peter Hinch

Post Reply