-
Notifications
You must be signed in to change notification settings - Fork 5
/
SocketServer-Matlab.py
61 lines (50 loc) · 1.52 KB
/
SocketServer-Matlab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#####################################################################
# Aldo Vargas
#
#
# Purpose:
# Matlab sends n signals via UDP to the Raspberry pie...
# RPI receives the data, unpacks it and prints it.
# - This is just for testing the best methodology between Matlab-rpi
# This methos uses sockets
#
########################################################################
import SocketServer
import struct
import time
#Raspberry pie IP address
#UDP_IP = "172.30.144.154"
#Mac IP address
UDP_IP = "130.209.27.59"
#UDP_IP = "localhost"
UDP_PORT = 51001
class MyUDPHandler(SocketServer.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
udp_mess=""
mess=""
numOfValues=0
timestamp = time.time()
data = self.request[0].strip()
socket = self.request[1]
try:
numOfValues = len(data) / 8
mess=struct.unpack('>' + 'd' * numOfValues, data)
for x in range(0, numOfValues):
udp_mess = udp_mess+" "+str(mess[x])
diff = time.time() - timestamp
print str(diff)+udp_mess
udp_mess=""
except:
pass
if __name__ == "__main__":
server = SocketServer.UDPServer((UDP_IP, UDP_PORT), MyUDPHandler)
#st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d+%H-%M-%S')+".csv"
#file = open("data/"+st, "w")
print "System ready on "+str(UDP_IP)
server.serve_forever()