Created
July 28, 2014 14:28
-
-
Save henryiii/c1cf97f9cbc86ff08895 to your computer and use it in GitHub Desktop.
basic comm code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
import pickle | |
import time | |
TCP_IP_PORT = '192.168.1.8', 5005 | |
BUFFER_SIZE = 1024 | |
RMSG = "recvd." | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
import motion | |
s.settimeout(2) | |
s.connect(TCP_IP_PORT) | |
motion.start_updates() | |
start = time.time() | |
freq = 30. | |
for i in range(60): | |
till_next = start + 1/freq*i - time.time() | |
if till_next > 0: | |
time.sleep(till_next) | |
MESSAGE = pickle.dumps(dict(att=motion.get_attitude(),grav=motion.get_gravity(),mag=motion.get_magnetic_field(),accel=motion.get_user_acceleration())) | |
s.send(MESSAGE) | |
data = s.recv(BUFFER_SIZE) | |
assert data == RMSG | |
motion.stop_updates() | |
s.close() | |
except ImportError: | |
s.bind(TCP_IP_PORT) | |
s.listen(1) | |
conn, addr = s.accept() | |
print 'Connection address:', addr | |
while 1: | |
data = conn.recv(BUFFER_SIZE) | |
if not data: break | |
print "received data:", pickle.loads(data) | |
conn.send(RMSG) | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment