Esempio n. 1
0
        /// <summary>
        /// Terminates a running data acquisition, closes the device and lsl outlets.
        /// </summary>
        private void Uninitialize()
        {
            try
            {
                //stop acquisition thread if running
                if (_acquisitionRunning)
                {
                    _acquisitionRunning = false;
                    _acquisitionThread.Join();
                }

                //close device
                _device.Dispose();
                _device = null;

                //uninitialize lsl
                if (_lslInfos.Count > 0)
                {
                    _lslInfos.Clear();
                }

                _lslInfos = null;

                if (_lslOutlets.Count > 0)
                {
                    _lslOutlets.Clear();
                }

                _lslOutlets = null;

                UpdateUIElements(DeviceStates.NotConnected);
            }
            catch
            {
                _device = null;
                if (_lslInfos != null && _lslInfos.Count > 0)
                {
                    _lslInfos.Clear();
                }

                _lslInfos = null;

                if (_lslOutlets != null && _lslOutlets.Count > 0)
                {
                    _lslOutlets.Clear();
                }

                _lslOutlets = null;

                UpdateUIElements(DeviceStates.NotConnected);
            }
        }
Esempio n. 2
0
 private void OnDestroy()
 {
     try
     {
         unicornDevice.StopAcquisition();
     }
     catch (Exception e)
     {
         print(e.Message);
     }
     unicornDevice.Dispose();
     receiveBufferHandle.Free();
 }
        /// <summary>
        /// The open/close button click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenClose_Click(object sender, EventArgs e)
        {
            //if no device opened
            if (_device == null)
            {
                try
                {
                    //connect to currently selected device
                    string serial = cbDevices.SelectedItem.ToString();

                    string streamName;
                    if (tbStreamName.Text.Length <= 0 || tbStreamName.Text == null)
                    {
                        streamName        = "Unicorn";
                        tbStreamName.Text = streamName;
                    }
                    else
                    {
                        streamName = tbStreamName.Text;
                    }

                    Thread connectionThread = new Thread(() => ConnectionThread_DoWork(serial, streamName));
                    connectionThread.Start();
                }
                catch (DeviceException ex)
                {
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                    ShowErrorBox(ex.Message);
                }
                catch (Exception ex)
                {
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                    ShowErrorBox(String.Format("Could not open device. {0}", ex.Message));
                }
            }
            //if device opened
            else
            {
                try
                {
                    //stop acquisition thread if running
                    if (_acquisitionRunning)
                    {
                        _acquisitionRunning = false;
                        _acquisitionThread.Join();
                    }

                    //close device
                    _device.Dispose();
                    _device = null;

                    //uninitialize lsl
                    _lslInfo   = null;
                    _lslOutlet = null;

                    UpdateUIElements(DeviceStates.NotConnected);
                }
                catch
                {
                    _device    = null;
                    _lslInfo   = null;
                    _lslOutlet = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// The open/close button click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenClose_Click(object sender, EventArgs e)
        {
            //if no device opened
            if (_device == null)
            {
                try
                {
                    //connect to currently selected device
                    string serial = cbDevices.SelectedItem.ToString();

                    IPAddress ip   = IPAddress.Parse(tbIP.Text);
                    int       port = Int32.Parse(tbPort.Text);

                    Thread connectionThread = new Thread(() => ConnectionThread_DoWork(serial, ip, port));
                    connectionThread.Start();
                }
                catch (DeviceException ex)
                {
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                    ShowErrorBox(ex.Message);
                }
                catch (Exception ex)
                {
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                    ShowErrorBox(String.Format("Could not open device. {0}", ex.Message));
                }
            }
            //if device opened
            else
            {
                try
                {
                    //stop acquisition thread if running
                    if (_acquisitionRunning)
                    {
                        _acquisitionRunning = false;
                        _acquisitionThread.Join();
                    }

                    //close device
                    _device.Dispose();
                    _device = null;

                    UpdateUIElements(DeviceStates.NotConnected);
                }
                catch
                {
                    _device = null;;
                    UpdateUIElements(DeviceStates.NotConnected);
                }
                finally
                {
                    try
                    {
                        //close socket connection
                        _socket.Shutdown(SocketShutdown.Both);
                        _socket.Close();
                    }
                    catch (Exception ex)
                    {
                        ShowErrorBox(String.Format("Could not close socket. {0}", ex.Message));
                    }
                }
            }
        }