lunedì 16 gennaio 2012

Datalogger in UDP


L'ennesima versione del datalogger che usa il protocollo UDP per scambiare i dati
-----------------------------------------------------------------------------------------------------
import android
import socket
import time
import math




droid = android.Android();




hostname = "192.168.0.1"; 
port = 21567;
data = "";
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
s.connect((hostname,port));


droid.startSensingTimed(1,1000)
time.sleep(3)
while True:
az = str(math.degrees(droid.sensorsReadOrientation().result[0]))
pitch = str(math.degrees(droid.sensorsReadOrientation().result[1]))
roll =  str(math.degrees(droid.sensorsReadOrientation().result[2]))
s.sendto(az+";"+pitch+";"+roll,(hostname,port));
-----------------------------------------------------------------------------------------------------
# Server program

import string
from socket import *

host = "192.168.0.1"
port = 21567
buf = 1024
addr = (host,port)

UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
valori = string.split(data,";")
print valori[0]
print valori[1]
print valori[2]

# Close socket
UDPSock.close()