public static void Main(string[] args) { // wait until an EEG stream shows up StreamInfo[] results = LSL.LSL.resolve_stream("type", "EEG"); // open an inlet, with post-processing enabled, and print meta-data // Note: The use of post-processing makes it impossible to recover // the original timestamps and is not recommended for applications // that store data to disk. using StreamInlet inlet = new StreamInlet(results[0], postproc_flags: processing_options_t.proc_ALL); results.DisposeArray(); System.Console.Write(inlet.info().as_xml()); // read samples float[,] buffer = new float[512, 8]; double[] timestamps = new double[512]; while (!Console.KeyAvailable) { int num = inlet.pull_chunk(buffer, timestamps); for (int s = 0; s < num; s++) { for (int c = 0; c < 8; c++) { Console.Write("\t{0}", buffer[s, c]); } Console.WriteLine(); } } }
/// <summary> /// Read from the LSL stream for server status /// </summary> async Task RunReadStatusPortAsync(CancellationToken cancelToken) { ReportNetworkTimeInterval.Restart(); StreamInlet inlet = null; try { inlet = new StreamInlet(StatusStream); cancelToken.Register(() => inlet.close_stream()); inlet.open_stream(); SyncedTime = false; SetBoardProperties(inlet); Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadStatusPortAsync", $"Create LSL stream for status on host {HostName}.", LogLevel.DEBUG)); HatConnectionChanged?.Invoke(this, new HatConnectionEventArgs(HatConnectionState.Discovered, HostName, "", BoardId, SampleRate)); string[,] samples = new string[32, 1]; double[] timestamps = new double[32]; // spin until canceled while (!cancelToken.IsCancellationRequested) { await Task.Delay(StatusReadingDelay); try { var sampleCount = inlet.pull_chunk(samples, timestamps); if (sampleCount > 0 && (samples[sampleCount - 1, 0].Length > 0)) { await ParseServerStatus(samples[sampleCount - 1, 0]); } } catch (ObjectDisposedException) { } catch (Exception ex) { Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadStatusPortAsync", ex, LogLevel.WARN)); } } } catch (OperationCanceledException) { } catch (Exception e) { Log?.Invoke(this, new LogEventArgs(this, "RunReadStatusPortAsync", e, LogLevel.FATAL)); } finally { if (inlet != null) { inlet.close_stream(); } } }
/// <summary> /// Run function for reading data on LSL multicast data port /// </summary> async Task RunReadDataPortAsync(CancellationToken cancelToken) { StreamInlet inlet = null; try { int maxChunkLen = (int)((SampleRate * 1.5) / ReadingDelay); inlet = new StreamInlet(DataStream, 360, maxChunkLen); cancelToken.Register(() => inlet.close_stream()); inlet.open_stream(); Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadDataPortAsync", $"Create LSL data stream for host {HostName}.", LogLevel.DEBUG)); double[,] buffer = new double[512, SampleSize]; double[] timestamps = new double[512]; // spin until canceled while (!cancelToken.IsCancellationRequested) { await Task.Delay(ReadingDelay); try { int num = inlet.pull_chunk(buffer, timestamps); ProcessChunk(buffer, num); } catch (ObjectDisposedException) { } catch (Exception ex) { Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadDataPortAsync", ex, LogLevel.WARN)); } } } catch (OperationCanceledException) { } catch (Exception e) { Log?.Invoke(this, new LogEventArgs(this, "RunReadDataPortAsync", e, LogLevel.FATAL)); } finally { if (inlet != null) { inlet.close_stream(); } } }