def main():
global HOST, PORT, EOF, MAXTRIES
global s, tries, loops
try:
s.connect((HOST, PORT))
while True:
# Measure timing using GPIO4
risetime = RCtime(4)
# Send to the connected socket
# (as we're using UDP, we must
# send separate messages)
s.sendall('foo %s%s' % (loops, EOF))
s.sendall('bar %s%s' % (risetime, EOF))
# Advance counter
loops = loops + 1
except socket.error as err:
errcode = err[0]
if errcode==errno.ECONNREFUSED:
print 'Connection refused by host!'
elif errcode==errno.ENETDOWN:
print 'No network connection!'
elif errcode==errno.ENETUNREACH:
print 'Network unreachable!'
elif errcode==errno.ENETRESET:
print 'Network dropped connection!'
elif errcode==errno.ECONNRESET:
print 'Connection reset by peer!'
elif errcode==errno.EHOSTDOWN:
print 'Host is down!'
elif errcode==errno.EHOSTUNREACH:
print 'No route to host!'
else:
print 'Caught: %s!' % err
if tries >= MAXTRIES:
GPIO.cleanup()
s.close()
print 'No connection. Exiting.'
else:
print 'Tried %i of %i times.\nWaiting %is...' % (tries, MAXTRIES, tries/10)
time.sleep(tries/10)
tries = tries + 1
main()
except KeyboardInterrupt:
GPIO.cleanup()
s.close()
print '%i loops' % loops
评论列表
文章目录