-
Notifications
You must be signed in to change notification settings - Fork 6
/
udp_server.py
53 lines (37 loc) · 1.06 KB
/
udp_server.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
"""A server that handles a connection with an OpenBCI board and serves that
data over both a UDP socket server and a WebSocket server.
Requires:
- pyserial
- asyncio
- websockets
"""
import argparse
import cPickle as pickle
import json
import open_bci_v3 as open_bci
import socket
class UDPServer(object):
def __init__(self, ip, port, json):
self.ip = ip
self.port = port
self.json = json
self.server = socket.socket(
socket.AF_INET, # Internet
socket.SOCK_DGRAM)
def send_data(self, data):
self.server.sendto(data, (self.ip, self.port))
def handle_sample(self, sample):
if self.json:
# Just send channel data.
self.send_data(json.dumps(sample.channel_data))
else:
# Pack up and send the whole OpenBCISample object.
self.send_data(pickle.dumps(sample))
if __name__ == "__main__":
args = {
"host":'0.0.0.0',
"port": '8888',
"json":"store_true",
}
sock_server = UDPServer(args["host"], int(args["port"]), args["json"])
obci.start_streaming(sock_server.handle_sample)