Python socket.recv() 正在等待并且在数据流动时不读取数据
我正在使用 python 来读取条形码数字。起初一切都很好。但过了一段时间(我不知道确切的时间)socket.recv 没有从条形码阅读器获取数据。当我重新启动程序时,一切又恢复正常。我为重置套接字连接设置了计数器,并再次设置了计时器以重置连接,但它不起作用。几周来我一直在努力解决这个问题。有没有人知道这个问题。您可以在下面找到我的接收代码:
def getBarcodeValue(client_socket):
print("Waiting to reading...")
data = ""
r, _, _ = select.select([client_socket], [], [])
if r:
try:
# ready to receive
data = client_socket.recv(1024) # most 1024 bytes can be read.
data = data.decode("utf-8")
return data
# print("Data :", realData)
# print("Byte size", len(realData))
# print("Data type :", type(realData))
except socket.timeout as e:
err = e.args[0]
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
sleep(1)
print('No data available')
else:
# a "real" error occurred
print(e)
# sys.exit(1)
print("Data receive error")
except socket.error as e:
# Something else happened, handle error, exit, etc.
print(e)
print("socket.error")
一段时间后,程序打印出“等待读取”,然后一直等待。
Edit1:程序不会抛出任何错误。它只是在打印“等待阅读”字符串后永远等待。起初一切都很好。当产品在传送带上流动时,条形码阅读器也会读取条形码并给出输出。但是,socket.recv() 一段时间后不会获取数据。
I'm using python for reading barcode code numbers. At first everything is ok. But after a while (I don't know the exact time) socket.recv doesn't get the data from barcode reader. When I restart the program everything works fine again. I put counter for reset socket connection and also put timer again for reset connection but it didn't work. I've been trying to solve this for weeks. Does anybody know this problem. You can find my recv codes below :
def getBarcodeValue(client_socket):
print("Waiting to reading...")
data = ""
r, _, _ = select.select([client_socket], [], [])
if r:
try:
# ready to receive
data = client_socket.recv(1024) # most 1024 bytes can be read.
data = data.decode("utf-8")
return data
# print("Data :", realData)
# print("Byte size", len(realData))
# print("Data type :", type(realData))
except socket.timeout as e:
err = e.args[0]
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
sleep(1)
print('No data available')
else:
# a "real" error occurred
print(e)
# sys.exit(1)
print("Data receive error")
except socket.error as e:
# Something else happened, handle error, exit, etc.
print(e)
print("socket.error")
After a while program prints "waiting to reading" and it waits until forever.
Edit1 : Program doesn't throw any error. It just waits until forever after printed the "waiting to reading" string. At first everything is ok. While products is flowing conveyor also barcode reader is reading barcodes and give output.However, socket.recv() doesn't get data after a while.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论