EIO error on poll.poll() when registered for usocket

Hello,

I want to read UART and web socket asynchronously. When I register the uart object in the uselect.poll then the poll.poll() waits for new uart event and I am able to read uart message successfully. But if I register usocket object in the uselect.poll(), then I am getting EIO error on the poll.poll() line. How to overcome this issue:

Code example:

import pyb
import uselect
import network
import usocket

def connect_ssid(ssid, key):
wlan = network.WINC()
wlan.connect(ssid, key=key, security=wlan.WPA_PSK)
print(wlan.ifconfig())

connect_ssid(“ssid”, “pwd”)

s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
s.bind([“”, 8088])
s.listen(5)
s.setblocking(False)

uart = pyb.UART(3, 115200)

p = uselect.poll()
p.register(uart, uselect.POLLIN)
p.register(s, uselect.POLLIN | uselect.POLLOUT)

print(“Start Program”)

while True:
print(“Wait for events”)
events = p.poll()
print('events = ', events)
for file in events:
if file[0] == uart:
ch = uart.read()
print('UART = ', ch)

Thank You!

Hi,

poll/select is not implemented for the WINC1500, you can use sockets in non-blocking mode instead, see:

Sorry for the late replay, but I was playing around with the non-blocking sockets.

If I do this:

s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
s.bind([“”, 8088])
s.listen(5)
s.settimeout(0.1)
while 1:
try:
conn, addr = s.accept()
print("Connected to address: %s " % (addr[0]))
conn.settimeout(2.0)
request = conn.recv(1024).decode(‘utf-8’)
print(“Request: %s” % request)
conn.close()
except OSError:
pass

I do not get any request data at all (I am sending REST request via PostMan).
If I recreate sockect and bind it in the while loop, then I am getting the request data. But the problem is I can not have more than 1 connection at the same time, but I am listing for 5 connections.

My ultimate goal is to stream video from the camera (how it is done in your MJPEG stream example) and and the same time to be able to receive and send another REST requests and also read UART.

Thank you for any response.

Maybe you should print the exception to see if it’s failing for some reason:

    except OSError as e:
        print("socket error: ", e)

If there’s another issue I’ll need an example I can run to debug.

Thank you for the quick response. I am getting socket error: -6

That means the socket was closed. Either the connection failed or you passed an argument that it didn’t like

Please see this code for how to use sockets: