private void Form1FormClosed(object sender, FormClosedEventArgs e)
        {
            if (device != null)
            {
                device.SendStopDacq();

                device.Disconnect();
                device.Dispose();

                device = null;
            }
        }
        private void CbDevicesSelectedIndexChanged(object sender, EventArgs e)
        {
            groupBoxSettings.Enabled = false;

            if (device != null)
            {
                device.StopDacq();

                device.Disconnect();
                device.Dispose();

                device = null;
            }

            btStart.Enabled = cbDevices.SelectedIndex >= 0;
            btStop.Enabled  = false;

            var entry = usblist.GetUsbListEntry((uint)cbDevices.SelectedIndex);

            /* choose one of the following constructors:
             * The first one uses the OnNewData callback and gives you a reference to the raw multiplexed data.
             * This could be used without further initialization
             * The second one uses the more advanced callback which gives you the data for each channel in a callback but needs initialization
             * for buffers and the selected channels
             */
            if (usePollForData)
            {
                device = new CMeaDeviceNet(entry.DeviceId.BusType);
            }
            else
            {
                device = new CMeaDeviceNet(entry.DeviceId.BusType, OnChannelData, OnError);
            }

            device.Connect(entry);
            useWireless = entry.DeviceId.IdProduct == ProductIdEnumNet.W2100;
            bool isMCCard = entry.DeviceId.IdProduct == ProductIdEnumNet.MC_Card;

            if (useWireless)
            {
                CW2100_FunctionNet   func           = new CW2100_FunctionNet(device);
                HeadStageIDTypeState headstagestate = func.GetSelectedHeadstageState(0); // information about currently selected headstage
                HeadStageIDType[]    headstages     = func.GetAvailableHeadstages(30);   // list of available headstage in range

                if (headstagestate.IdType.ID == 0xFFFF)                                  // if no headstage is currently selected
                {
                    if (headstages.Length > 0)                                           // if at least one headstage is available
                    {
                        func.SelectHeadstage(headstages[0].ID, 0);                       // then selected the first one
                    }
                }
                // func.DeselectHeadstage(0); // use this to deselect a selected headstage
            }

            device.SendStopDacq(); // only to be sure

            tbDeviceInfo.Text  = "";
            tbDeviceInfo.Text += "Serial number: " + device.SerialNumber + Environment.NewLine;
            device.HWInfo().GetNumberOfHWADCChannels(out int hwchannels);
            tbDeviceInfo.Text += @"Number of Hardware channels: " + hwchannels.ToString("D") + Environment.NewLine;

            if (hwchannels == 0)
            {
                hwchannels = 64;
            }

            // configure MeaDevice: MC_Card or Usb
            device.SetNumberOfChannels(hwchannels);

            const int Samplingrate = 20000; // MC_Card does not support all settings, please see MC_Rack for valid settings

            device.SetSamplerate(Samplingrate, 1, 0);

            int gain = device.GetGain();

            device.HWInfo().GetAvailableVoltageRangesInMicroVoltAndStringsInMilliVolt(out var voltageRanges);
            for (int i = 0; i < voltageRanges.Count; i++)
            {
                tbDeviceInfo.Text += @"(" + i.ToString("D") + @") " + voltageRanges[i].VoltageRangeDisplayStringMilliVolt + Environment.NewLine;
            }

            // Set the range according to the index (only valid for MC_Card)
            if (isMCCard)
            {
                device.SetVoltageRangeByIndex(0, 0);
            }

            device.EnableDigitalIn(true, 0);

            // Checksum not supported by MC_Card
            if (!isMCCard)
            {
                device.EnableChecksum(true, 0);
            }

            // Get the layout to know how the data look like that you receive
            device.GetChannelLayout(out int ana, out int digi, out int che, out int tim, out int block, 0);

            // or
            block = device.GetChannelsInBlock(0);

            // set the channel combo box with the channels
            SetChannelCombo(block);

            channelblocksize = Samplingrate / 10; // good choice for MC_Card

            bool[] selChannels = new bool[block];

            for (int i = 0; i < block; i++)
            {
                selChannels[i] = true; // With true channel i is selected
                // selChannels[i] = false; // With false the channel i is deselected
            }
            mChannelHandles = block; // for this case, if all channels are selected
            // queue size and threshold should be selected carefully
            if (useChannelMethod && useChannelData)
            {
                device.SetSelectedData(selChannels, 10 * channelblocksize, channelblocksize, SampleSizeNet.SampleSize16Unsigned, block);
                //device.AddSelectedChannelsQueue(10, 2, 10 * channelblocksize, channelblocksize, SampleSizeNet.SampleSize16Unsigned);
                //device.ChannelBlock_SetCommonThreshold(channelblocksize);
                // Alternative call if you want to select all channels
                //device.SetSelectedData(block, 10 * channelblocksize, channelblocksize, SampleSizeNet.SampleSize16Unsigned, block);
            }
            else if (useChannelMethod && !useChannelData)
            {
                device.SetSelectedChannels(selChannels, 10 * channelblocksize, channelblocksize, SampleSizeNet.SampleSize16Unsigned, block);
            }
            else
            {
                device.SetSelectedChannelsQueue(selChannels, 10 * channelblocksize, channelblocksize, SampleSizeNet.SampleSize16Unsigned, block);
            }

            device.ChannelBlock_SetCheckChecksum((uint)che, (uint)tim);
        }
Exemplo n.º 3
0
        public bool connectDataAcquisitionDevice(uint index)
        {
            this.dataFormat = SampleSizeNet.SampleSize32Signed;

            if (dataAcquisitionDevice != null)
            {
                dataAcquisitionDevice.StopDacq();
                dataAcquisitionDevice.Disconnect();
                dataAcquisitionDevice.Dispose();
                dataAcquisitionDevice = null;
                throw new System.ArgumentException("Reached bad code path", "DAQ is null, mcs cruft");
            }

            dataAcquisitionDevice = new CMeaDeviceNet(usblist.GetUsbListEntry(index).DeviceId.BusType,
                                                      _onChannelData,
                                                      onError);


            // The second arg refers to lock mask, allowing multiple device objects to be connected
            // to the same physical device. Yes, I know, what the f**k...
            dataAcquisitionDevice.Connect(usblist.GetUsbListEntry(index), 1);
            dataAcquisitionDevice.SendStop();

            int what = 0;

            dataAcquisitionDevice.HWInfo().GetNumberOfHWADCChannels(out what);
            hwchannels = what;

            dataAcquisitionDevice.SetNumberOfChannels(hwchannels);
            dataAcquisitionDevice.EnableDigitalIn(false, 0);
            dataAcquisitionDevice.EnableChecksum(false, 0);
            dataAcquisitionDevice.SetDataMode(DataModeEnumNet.dmSigned32bit, 0);

            // block:
            // get the number of 16 bit datawords which will be collected per sample frame,
            // use after the device is configured. (which means?, setting data mode, num channels etc?)
            int ana, digi, che, tim, block;

            dataAcquisitionDevice.GetChannelLayout(out ana, out digi, out che, out tim, out block, 0);


            dataAcquisitionDevice.SetSampleRate(samplerate, 1, 0);

            int gain = dataAcquisitionDevice.GetGain();

            List <CMcsUsbDacqNet.CHWInfo.CVoltageRangeInfoNet> voltageranges;

            dataAcquisitionDevice.HWInfo().
            GetAvailableVoltageRangesInMicroVoltAndStringsInMilliVolt(out voltageranges);


            bool[] selectedChannels = new bool[block];
            for (int i = 0; i < block; i++)
            {
                selectedChannels[i] = true;
            }                                                        // hurr


            // *org [[file:killme.cs::/%20a%20device%20are%20read%20in%2032%20bit%20data%20format%20nChannels%20=%20ChannelsInBlock/2][documentation]]
            bool[]        nChannels       = selectedChannels;
            int           queueSize       = 120000;
            int           threshold       = segmentLength;
            SampleSizeNet sampleSize      = dataFormat;
            int           ChannelsInBlock = block / 2;

            dataAcquisitionDevice.SetSelectedChannelsQueue
                (nChannels,
                queueSize, // huh?
                threshold,
                sampleSize,
                ChannelsInBlock);

            mChannelHandles = block;

            dataAcquisitionDevice.ChannelBlock_SetCheckChecksum((uint)che, (uint)tim); // ???

            // int voltrange = voltageranges.ToArray()[0];

            int validDataBits    = -1;
            int deviceDataFormat = -1;

            /**
             * Summary:
             *  Get the real number of data bits.
             *
             * Remarks:
             *  This value may be different from the value returned by GetDataFormat, e.g. in
             *  MC_Card the data are shifted 2 bits so the real number is 14 while the data format
             *  is 16 bits
             */
            dataAcquisitionDevice.GetNumberOfDataBits(0,
                                                      DacqGroupChannelEnumNet.HeadstageElectrodeGroup,
                                                      out validDataBits);

            dataAcquisitionDevice.GetDataFormat(0,
                                                DacqGroupChannelEnumNet.HeadstageElectrodeGroup,
                                                out deviceDataFormat);

            DataModeEnumNet dataMode = dataAcquisitionDevice.GetDataMode(0);


            /**
             * Summary:
             * Get the number of 16 bit datawords which will be collected per sample frame,
             * use after the device is configured.
             *
             * Returns:
             * Number of 16 bit datawords per sample frame.
             * Returns 132 (66 32 bit words???)
             */
            int meme = dataAcquisitionDevice.GetChannelsInBlock();

            deviceInfo =
                "Data acquisition device connected to physical device with parameters: \n" +
                $"[SetSelectedChannelsQueue arguments:]\n" +
                $"nChannels           \t{selectedChannels}\n" +
                $"queueSize:          \t{queueSize}\n" +
                $"threshold:          \t{threshold}\n" +
                $"samplesize:         \t{sampleSize}\n" +
                $"channelsInBlock:    \t{ChannelsInBlock}\n\n" +
                $"[Experiment params]\n" +
                $"sample rate:        \t{samplerate}\n" +
                $"Voltage range:      \t{voltageranges[0].VoltageRangeDisplayStringMilliVolt}\n" +
                $"Corresponding to    \t{voltageranges[0].VoltageRangeInMicroVolt} µV\n" +
                $"[Device channel layout]\n\n" +
                $"hardware channels:  \t{hwchannels}\n" + // 64
                $"analog channels:    \t{ana}\n" +        // 128
                $"digital channels:   \t{digi}\n" +       // 2
                $"che(??) channels:   \t{che}\n" +        // 4
                $"tim(??) channels:   \t{tim}\n\n" +
                $"[Other..]\n" +
                $"valid data bits:    \t{validDataBits}\n" +    // 24
                $"device data format: \t{deviceDataFormat}\n" + // 32
                $"device data mode:   \t{dataMode}\n" +         // dmSigned24bit
                $"nice meme:          \t{meme}\n" +
                "";

            return(true);
        }
Exemplo n.º 4
0
        private void cbDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (device != null)
            {
                device.StopDacq();

                device.Disconnect();
                device.Dispose();

                device = null;
            }

            uint sel = (uint)cbDevices.SelectedIndex;

            /* choose one of the following contructors:
             * The first one uses the OnNewData callback and gives you a reference to the raw multiplexed data,
             * this could be used without further initialisation
             * The second uses the more advanced callback which gives you the data for each channel in a callback, but need initialistation
             * for buffers and the selected channels
             */

            if (sel >= 0) // a device is selected, enable the sampling buttons
            {
                btStart.Enabled = true;
            }
            else
            {
                btStart.Enabled = false;
            }
            btStop.Enabled = false;

            device = new CMeaDeviceNet(usblist.GetUsbListEntry(sel).DeviceId.BusType, OnChannelData, OnError);
            device.Connect(usblist.GetUsbListEntry(sel));

            device.SendStop(); // only to be sure

            tbDeviceInfo.Text  = "";
            tbDeviceInfo.Text += /*"Serialnumber: " +*/ device.SerialNumber + "\r\n";
            int hwchannels;

            device.HWInfo().GetNumberOfHWADCChannels(out hwchannels);
            tbDeviceInfo.Text += "Number of Hardwarechannels: " + hwchannels.ToString() + "\r\n";


            // configure MeaDevice: MC_Card or Usb
            device.SetNumberOfChannels(hwchannels);

            int samplingrate = 5000; // MC_Card does not support all settings, please see MC_Rack for valid settings

            device.SetSampleRate(samplingrate, 1, 0);

            int gain;

            device.GetGain(out gain);

            List <CMcsUsbDacqNet.CHWInfo.CVoltageRangeInfoNet> voltageranges;

            device.HWInfo().GetAvailableVoltageRangesInMicroVoltAndStringsInMilliVolt(out voltageranges);
            for (int i = 0; i < voltageranges.Count; i++)
            {
                tbDeviceInfo.Text += "(" + i.ToString() + ") " + voltageranges[i].VoltageRangeDisplayStringMilliVolt + "\r\n";
            }

            // Set the range acording to the index (only valid for MC_Card)
            // device.SetVoltageRangeInMicroVoltByIndex(0, 0);

            device.EnableDigitalIn(true, 0);

            // Checksum not supported by MC_Card
            device.EnableChecksum(true, 0);


            // Get the layout to know how the data look like that you receive
            int ana, digi, che, tim, block;

            device.GetChannelLayout(out ana, out digi, out che, out tim, out block, 0);

            // or
            device.GetChannelsInBlock(out block);

            m_block = block;
            // set the channel combo box with the channels
            SetChannelCombo(block);

            channelblocksize = samplingrate / 10; // good choice for MC_Card

            bool[] selChannels = new bool[block];

            for (int i = 0; i < block; i++)
            {
                selChannels[i] = true; // With true channel i is selected
                //Console.WriteLine("Selected: " + block);
                // selChannels[i] = false; // With false the channel i is deselected
            }
            // queue size and threshold should be selected carefully

            device.SetSelectedData(selChannels, 10 * channelblocksize, channelblocksize, SampleSizeNet.SampleSize16, block);
            // Alternative call if you want to select all channels
            //device.SetSelectedData(block, 10 * channelblocksize, channelblocksize, CMcsUsbDacqNet.SampleSize.Size16, block);
            m_channel_handles = block; // for this case, if all channels are selected
        }
Exemplo n.º 5
0
        // Occurs when the value of the selected index of the combo box (i.e. the seleted MEA device) changes.
        private void ComboBoxMeaDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = (uint)this.comboBoxMeaDevices.SelectedIndex;

            // TODO understand MCS black magic.
            usb = this.usbList.GetUsbListEntry(index);
            // TODO understand MCS black magic.
            this.device = new CMeaDeviceNet(usb.DeviceId.BusType, ChannelDataCallback, ErrorCallback);
            // Establish a connection to the required DAQ device.
            this.device.Connect(usb);
            // TODO understand MCS black magic.
            this.device.SendStop();
            // TODO understand MCS black magic.
            var hardwareInfo = this.device.HWInfo();

            // Get the number of available analog hardware channels.
            hardwareInfo.GetNumberOfHWADCChannels(out nb_channels);
            // Set the number of analog hardware channels to the maximum.
            this.device.SetNumberOfChannels(nb_channels);
            // Update the text box which displays the number of analog hardware channels.
            this.textBoxNumberOfChannels.Text = nb_channels.ToString();
            // TODO understand MCS black magic.
            //hardwareInfo.GetAvailableSampleRates(out List<int> sample_rates);
            this.sample_rate = 20000;
            // TODO understand MCS black magic.
            var oversample    = (uint)1;
            var virtualDevice = 0;

            this.device.SetSampleRate(sample_rate, oversample, virtualDevice);
            this.textBoxSampleRate.Text = sample_rate.ToString();
            // TODO understand MCS black magic.
            gain = this.device.GetGain();
            this.textBoxGain.Text = gain.ToString();
            // TODO understand MCS black magic.
            //hardwareInfo.GetAvailableVoltageRangesInMicroVoltAndStringsInMilliVolt(out List<CMcsUsbDacqNet.CHWInfo.CVoltageRangeInfoNet> voltage_ranges);
            //var voltage_range = 10;
            // TODO understand MCS black magic.
            //this.device.SetVoltageRangeInMicroVolt(voltage_range);
            // TODO understand MCS black magic.
            this.device.EnableDigitalIn(true, virtualDevice);
            // TODO understand MCS black magic.
            this.device.EnableChecksum(true, virtualDevice);
            // TODO understand MCS black magic.
            //this.device.EnableTimestamp(false);
            // TODO understand MCS black magic.
            int analog_channels;
            int digital_channels;
            int checksum_channels;
            int timestamp_channels;

            this.device.GetChannelLayout(out analog_channels, out digital_channels, out checksum_channels, out timestamp_channels, out this.channels_in_block, (uint)virtualDevice);
            this.channels_in_block           = this.device.GetChannelsInBlock();
            this.textBoxChannelsInBlock.Text = this.channels_in_block.ToString();
            // ...
            this.selected_channels = new bool[this.channels_in_block];
            for (var i = 0; i < this.channels_in_block; i++)
            {
                this.selected_channels[i] = true;
            }
            // TODO check the value of the buffer size.
            this.buf_size = sample_rate / 10;
            //this.buf_size = sample_rate / 100;
            this.queue_size            = 20 * this.buf_size;
            this.textBoxQueueSize.Text = this.queue_size.ToString();
            // ...
            this.threshold             = this.buf_size;
            this.textBoxThreshold.Text = this.threshold.ToString();
            // ...
            this.sample_size            = SampleSizeNet.SampleSize16Unsigned;
            this.textBoxSampleSize.Text = sample_size.ToString();
            // TODO understand MCS black magic.
            this.device.ChannelBlock_SetCheckChecksum((uint)checksum_channels, (uint)timestamp_channels);
            // Enable control.
            this.textBoxQueueSize.Enabled           = true;
            this.textBoxThreshold.Enabled           = true;
            this.buttonDataAcquisitionStart.Enabled = true;
        }