问题描述
我在运行使用超声波传感器记录距离的代码时遇到了一些问题。
import socket
import time
class SensorStreamingTest(object):
def __init__(self,host,port):
self.server_socket = socket.socket()
self.server_socket.setsockopt(socket.soL_SOCKET,socket.so_REUSEADDR,1)
self.server_socket.bind((host,port))
self.server_socket.listen(0)
self.connection,self.client_address = self.server_socket.accept()
self.host_name = socket.gethostname()
self.host_ip = socket.gethostbyname(self.host_name)
self.streaming()
def streaming(self):
try:
print("Host: ",self.host_name + ' ' + self.host_ip)
print("Connection from: ",self.client_address)
start = time.time()
#print("hi")
while True:
sensor_data = self.connection.recv(1024)
#print (sensor_data.decode('utf-8'))
#print("hi")
print("distance: %0.1f cm",sensor_data.decode('utf-8'))
# test for 10 seconds
if time.time() - start > 10:
break
finally:
self.connection.close()
self.server_socket.close()
if __name__ == '__main__':
h,p = "192.168.29.77",9000
SensorStreamingTest(h,p)```
#客户端:
from socket import *
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
create a socket and bind socket to the host
client_socket = socket(AF_INET,SOCK_STREAM)
client_socket.connect(('192.168.29.77',9000))
def measure():
GPIO.output(GPIO_TRIGGER,True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER,False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
elapsed = stop-start
distance = (elapsed * 34300)/2
return distance
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
GPIO.setup(GPIO_ECHO,GPIO.IN)
GPIO.output(GPIO_TRIGGER,False)
try:
while True:
print('hi')
distance = measure()
client_socket.send(str(distance).encode('utf-8'))
time.sleep(0.5)
finally:
client_socket.close()
GPIO.cleanup()```
在 0.5 秒的间隔内接收 20 个距离后,我收到了管道损坏错误并且执行停止。 不知道出了什么问题,但我不断收到损坏的管道错误, 请帮忙 谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)