Skip to content

Commit f42f970

Browse files
committed
Add test for receiving Markers over UDP
1 parent 08e9ef5 commit f42f970

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import socket
2+
import sys
3+
import time
4+
import argparse
5+
import signal
6+
import struct
7+
import os
8+
import json
9+
10+
numSamples = 0
11+
12+
# Print received message to console
13+
def print_message(*args):
14+
try:
15+
#print(args[0]) #added to see raw data
16+
obj = json.loads(args[0].decode())
17+
print(obj.get('data'))
18+
num_samples_packet = len(obj.get('data'))
19+
global numSamples
20+
print("NumSamplesInPacket_Marker == " + str(num_samples_packet))
21+
numSamples += num_samples_packet
22+
if obj:
23+
return True
24+
else:
25+
return False
26+
except BaseException as e:
27+
print(e)
28+
return False
29+
# print("(%s) RECEIVED MESSAGE: " % time.time() +
30+
# ''.join(str(struct.unpack('>%df' % int(length), args[0]))))
31+
32+
# Clean exit from print mode
33+
def exit_print(signal, frame):
34+
print("Closing listener")
35+
sys.exit(0)
36+
37+
# Record received message in text file
38+
def record_to_file(*args):
39+
textfile.write(str(time.time()) + ",")
40+
#textfile.write(args[0])
41+
obj = json.loads(args[0].decode())
42+
#print(obj.get('data'))
43+
textfile.write(json.dumps(obj))
44+
textfile.write("\n")
45+
46+
# Save recording, clean exit from record mode
47+
def close_file(*args):
48+
print("\nFILE SAVED")
49+
textfile.close()
50+
sys.exit(0)
51+
52+
if __name__ == "__main__":
53+
# Collect command line arguments
54+
parser = argparse.ArgumentParser()
55+
parser.add_argument("--ip",
56+
default="127.0.0.1", help="The ip to listen on")
57+
parser.add_argument("--port",
58+
type=int, default=12345, help="The port to listen on")
59+
parser.add_argument("--address",default="/openbci", help="address to listen to")
60+
parser.add_argument("--option",default="print",help="Debugger option")
61+
parser.add_argument("--len",default=9,help="Debugger option")
62+
args = parser.parse_args()
63+
64+
# Set up necessary parameters from command line
65+
length = int(args.len)
66+
if args.option=="print":
67+
signal.signal(signal.SIGINT, exit_print)
68+
elif args.option=="record":
69+
i = 0
70+
while os.path.exists("udp_test%s.txt" % i):
71+
i += 1
72+
filename = "udp_test%i.txt" % i
73+
textfile = open(filename, "w")
74+
textfile.write("time,address,messages\n")
75+
textfile.write("-------------------------\n")
76+
print("Recording to %s" % filename)
77+
signal.signal(signal.SIGINT, close_file)
78+
79+
# Connect to socket
80+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
81+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
82+
server_address = (args.ip, args.port)
83+
sock.bind(server_address)
84+
85+
# Display socket attributes
86+
print('--------------------')
87+
print("-- UDP LISTENER -- ")
88+
print('--------------------')
89+
print("IP:", args.ip)
90+
print("PORT:", args.port)
91+
print('--------------------')
92+
print("%s option selected" % args.option)
93+
94+
# Receive messages
95+
print("Listening...")
96+
start = time.time()
97+
98+
duration = 10
99+
while time.time() <= start + duration:
100+
data, addr = sock.recvfrom(20000) # buffer size is 20000 bytes
101+
if args.option=="print":
102+
print_message(data)
103+
elif args.option=="record":
104+
record_to_file(data)
105+
numSamples += 1
106+
107+
print( "Samples == {}".format(numSamples) )
108+
print( "Duration == {}".format(duration) )
109+
print( "Avg Sampling Rate == {}".format(numSamples / duration) )

0 commit comments

Comments
 (0)