-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1140 from OpenBCI/june2023-networking-refactor
Refactor Networking Widget - June 2023
- Loading branch information
Showing
10 changed files
with
1,314 additions
and
1,356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Here we show that we can use push_sample to send and pull_chunk to receive a sample.""" | ||
import time | ||
from pylsl import StreamInlet, resolve_stream | ||
from time import sleep | ||
|
||
# first resolve an EEG stream on the lab network | ||
print("looking for an EEG stream...") | ||
streams = resolve_stream('type', 'FOCUS') | ||
|
||
# create a new inlet to read from the stream | ||
inlet = StreamInlet(streams[0]) | ||
duration = 5 | ||
|
||
sleep(1) | ||
|
||
def testLSLSamplingRate(): | ||
start = time.time() | ||
totalNumSamples = 0 | ||
validSamples = 0 | ||
numChunks = 0 | ||
print( "Testing Sampling Rates..." ) | ||
|
||
while time.time() <= start + duration: | ||
# print(time.time()) | ||
# get chunks of samples | ||
sample, timestamp = inlet.pull_chunk() | ||
if sample: | ||
print(sample) | ||
validSamples += 1 | ||
|
||
#print( "Number of Chunks and Samples == {} , {}".format(numChunks, totalNumSamples) ) | ||
#print( "Valid Samples and Duration == {} / {}".format(validSamples, duration) ) | ||
print( "Avg Sampling Rate == {}".format(validSamples / duration) ) | ||
|
||
|
||
testLSLSamplingRate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#N canvas -1158 177 945 588 12; | ||
#X obj 179 241 netreceive -u -b; | ||
#X msg 179 179 listen 0; | ||
#X obj 179 270 oscparse; | ||
#X obj 262 148 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000; | ||
#X obj 179 149 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000; | ||
#X msg 262 179 listen 12345; | ||
#X obj 179 343 route openbci; | ||
#X obj 179 302 list trim; | ||
#X obj 354 250 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000; | ||
#X obj 354 311 list prepend symbol; | ||
#X obj 354 340 list trim; | ||
#X text 423 126 Enter the address you'd like to receive here. This is the same as the Address textfield in the GUI. Example: accelerometer, f 23; | ||
#X text 437 213 Clicking the bang would change the routing address to the new value., f 21; | ||
#X text 437 339 After data is routed \, we are left with a list of floats that can be unpacked., f 21; | ||
#X text 149 508 Happy Hacking! We're excited to see what our global Community can continue to create using OpenBCI hardware and software! -RW, f 62; | ||
#X text 48 256 PureData adds the "list" selector after our binary data is parsed., f 16; | ||
#X text 152 107 Turn off netreceive, f 10; | ||
#X text 249 107 Start recieving on named port, f 16; | ||
#X text 647 22 Example Incoming OSC Packet; | ||
#X obj 179 376 route time-series-raw; | ||
#X msg 354 283 time-series-raw; | ||
#X obj 52 427 print rawmsg; | ||
#X obj 179 419 route ch0 ch1 ch2 ch3; | ||
#X obj 179 461 print channel0; | ||
#X text 40 61 openbci time-series-raw ch0 -3.92592 -3.88553 -3.26396 -3.62399 -2.70645 -1.6476 -0.789745 0.749153 1.55314 3.081, f 114; | ||
#X text 32 18 OpenBCI OSC Reciever Example June 21 \, 2023 Richard Waltman, f 31; | ||
#X connect 0 0 2 0; | ||
#X connect 1 0 0 0; | ||
#X connect 2 0 7 0; | ||
#X connect 3 0 5 0; | ||
#X connect 4 0 1 0; | ||
#X connect 5 0 0 0; | ||
#X connect 6 0 19 0; | ||
#X connect 7 0 6 0; | ||
#X connect 7 0 21 0; | ||
#X connect 8 0 20 0; | ||
#X connect 9 0 10 0; | ||
#X connect 10 0 19 1; | ||
#X connect 19 0 22 0; | ||
#X connect 20 0 9 0; | ||
#X connect 22 0 23 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#N canvas 3103 183 661 588 12; | ||
#X obj 179 241 netreceive -u -b; | ||
#X msg 179 179 listen 0; | ||
#X obj 179 270 oscparse; | ||
#X obj 262 148 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000; | ||
#X obj 179 149 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000; | ||
#X msg 262 179 listen 12345; | ||
#X obj 179 343 route openbci; | ||
#X obj 179 302 list trim; | ||
#X obj 354 250 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000; | ||
#X obj 354 311 list prepend symbol; | ||
#X obj 354 340 list trim; | ||
#X obj 69 389 print data; | ||
#X text 423 126 Enter the address you'd like to receive here. This is the same as the Address textfield in the GUI. Example: accelerometer, f 23; | ||
#X text 437 213 Clicking the bang would change the routing address to the new value., f 21; | ||
#X text 437 339 After data is routed \, we are left with a list of floats that can be unpacked., f 21; | ||
#X text 149 508 Happy Hacking! We're excited to see what our global Community can continue to create using OpenBCI hardware and software! -RW, f 62; | ||
#X text 48 256 PureData adds the "list" selector after our binary data is parsed., f 16; | ||
#X text 152 107 Turn off netreceive, f 10; | ||
#X text 249 107 Start recieving on named port, f 16; | ||
#X text 34 25 OpenBCI OSC Reciever Example June 14 \, 2023 Richard Waltman, f 31; | ||
#X obj 179 383 route accelerometer; | ||
#X msg 354 283 accelerometer; | ||
#X obj 179 421 route x y z, f 21; | ||
#X floatatom 179 466 5 0 0 0 - - - 0; | ||
#X floatatom 275 466 5 0 0 0 - - - 0; | ||
#X floatatom 227 466 5 0 0 0 - - - 0; | ||
#X text 394 19 Example Incoming OSC Packet; | ||
#X text 359 41 openbci accelerometer x 0.986508; | ||
#X connect 0 0 2 0; | ||
#X connect 1 0 0 0; | ||
#X connect 2 0 7 0; | ||
#X connect 3 0 5 0; | ||
#X connect 4 0 1 0; | ||
#X connect 5 0 0 0; | ||
#X connect 6 0 20 0; | ||
#X connect 7 0 6 0; | ||
#X connect 7 0 11 0; | ||
#X connect 8 0 21 0; | ||
#X connect 9 0 10 0; | ||
#X connect 10 0 20 1; | ||
#X connect 20 0 22 0; | ||
#X connect 21 0 9 0; | ||
#X connect 22 0 23 0; | ||
#X connect 22 1 25 0; | ||
#X connect 22 2 24 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Networking-Test-Kit/UDP/udp_receive_timeSeriesAllChannels.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import socket | ||
import sys | ||
import time | ||
import argparse | ||
import signal | ||
import struct | ||
import os | ||
import json | ||
|
||
numSamples = 0 | ||
|
||
# Print received message to console | ||
def print_message(*args): | ||
try: | ||
# print(args[0]) #added to see raw data | ||
obj = json.loads(args[0].decode()) | ||
print(obj.get('data')) | ||
numSamplesInChannelOne = len(obj.get('data')[0]) | ||
global numSamples | ||
print("NumSamplesInPacket_ChannelOne == " + str(numSamplesInChannelOne)) | ||
numSamples += numSamplesInChannelOne | ||
if obj: | ||
return True | ||
else: | ||
return False | ||
except BaseException as e: | ||
print(e) | ||
return False | ||
# print("(%s) RECEIVED MESSAGE: " % time.time() + | ||
# ''.join(str(struct.unpack('>%df' % int(length), args[0])))) | ||
|
||
# Clean exit from print mode | ||
def exit_print(signal, frame): | ||
print("Closing listener") | ||
sys.exit(0) | ||
|
||
# Record received message in text file | ||
def record_to_file(*args): | ||
textfile.write(str(time.time()) + ",") | ||
#textfile.write(args[0]) | ||
obj = json.loads(args[0].decode()) | ||
#print(obj.get('data')) | ||
textfile.write(json.dumps(obj)) | ||
textfile.write("\n") | ||
|
||
# Save recording, clean exit from record mode | ||
def close_file(*args): | ||
print("\nFILE SAVED") | ||
textfile.close() | ||
sys.exit(0) | ||
|
||
if __name__ == "__main__": | ||
# Collect command line arguments | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", | ||
default="127.0.0.1", help="The ip to listen on") | ||
parser.add_argument("--port", | ||
type=int, default=12345, help="The port to listen on") | ||
parser.add_argument("--address",default="/openbci", help="address to listen to") | ||
parser.add_argument("--option",default="print",help="Debugger option") | ||
parser.add_argument("--len",default=9,help="Debugger option") | ||
args = parser.parse_args() | ||
|
||
# Set up necessary parameters from command line | ||
length = int(args.len) | ||
if args.option=="print": | ||
signal.signal(signal.SIGINT, exit_print) | ||
elif args.option=="record": | ||
i = 0 | ||
while os.path.exists("udp_test%s.txt" % i): | ||
i += 1 | ||
filename = "udp_test%i.txt" % i | ||
textfile = open(filename, "w") | ||
textfile.write("time,address,messages\n") | ||
textfile.write("-------------------------\n") | ||
print("Recording to %s" % filename) | ||
signal.signal(signal.SIGINT, close_file) | ||
|
||
# Connect to socket | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
server_address = (args.ip, args.port) | ||
sock.bind(server_address) | ||
|
||
# Display socket attributes | ||
print('--------------------') | ||
print("-- UDP LISTENER -- ") | ||
print('--------------------') | ||
print("IP:", args.ip) | ||
print("PORT:", args.port) | ||
print('--------------------') | ||
print("%s option selected" % args.option) | ||
|
||
# Receive messages | ||
print("Listening...") | ||
start = time.time() | ||
|
||
duration = 10 | ||
while time.time() <= start + duration: | ||
data, addr = sock.recvfrom(20000) # buffer size is 20000 bytes | ||
if args.option=="print": | ||
print_message(data) | ||
# numSamples += 1 | ||
elif args.option=="record": | ||
record_to_file(data) | ||
numSamples += 1 | ||
|
||
print( "Samples == {}".format(numSamples) ) | ||
print( "Duration == {}".format(duration) ) | ||
print( "Avg Sampling Rate == {}".format(numSamples / duration) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.