Skip to content

Instantly share code, notes, and snippets.

@henryiii henryiii/sockcom.py
Created Jul 28, 2014

Embed
What would you like to do?
basic comm code
#!/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
You can’t perform that action at this time.