Exemplo n.º 1
0
        /// <summary>
        /// Open a specific device and allocate buffers.
        /// </summary>
        public bool Open(string systemKey, string interfaceKey, string deviceKey)
        {
            Close();

            try
            {
                // Look for the device.
                SystemList systemList = SystemList.Instance;
                systemList.Refresh();
                foreach (KeyValuePair <string, BGAPI2.System> systemPair in systemList)
                {
                    if (systemPair.Key != systemKey)
                    {
                        continue;
                    }

                    system = systemPair.Value;
                    if (!system.IsOpen)
                    {
                        system.Open();
                    }

                    break;
                }

                if (system == null || !system.IsOpen)
                {
                    return(false);
                }

                system.Interfaces.Refresh(100);
                foreach (KeyValuePair <string, BGAPI2.Interface> interfacePair in system.Interfaces)
                {
                    if (interfacePair.Key != interfaceKey)
                    {
                        continue;
                    }

                    interf = interfacePair.Value;
                    if (!interf.IsOpen)
                    {
                        interf.Open();
                    }
                    break;
                }

                if (interf == null || !interf.IsOpen)
                {
                    return(false);
                }


                interf.Devices.Refresh(100);
                foreach (KeyValuePair <string, BGAPI2.Device> devicePair in interf.Devices)
                {
                    if (devicePair.Key != deviceKey)
                    {
                        continue;
                    }

                    device = devicePair.Value;
                    if (!device.IsOpen)
                    {
                        device.Open();
                    }
                    break;
                }

                if (device == null || !device.IsOpen)
                {
                    return(false);
                }

                DataStreamList dataStreamList = device.DataStreams;
                dataStreamList.Refresh();
                foreach (KeyValuePair <string, BGAPI2.DataStream> dataStreamPair in dataStreamList)
                {
                    if (string.IsNullOrEmpty(dataStreamPair.Key))
                    {
                        continue;
                    }

                    dataStream = dataStreamPair.Value;
                    dataStream.Open();
                    break;
                }

                if (dataStream == null)
                {
                    CloseDevice();
                    return(false);
                }

                // Use buffers internal to the API.
                bufferList = dataStream.BufferList;
                int countBuffers = 4;
                for (int i = 0; i < countBuffers; i++)
                {
                    BGAPI2.Buffer buffer = new BGAPI2.Buffer();
                    bufferList.Add(buffer);
                    //ulong memSize = buffer.MemSize;
                    //log.DebugFormat("Buffer mem size: {0}", memSize);
                }

                // Make buffers available to the Baumer producer.
                if (bufferList != null && bufferList.Count == countBuffers)
                {
                    foreach (KeyValuePair <string, BGAPI2.Buffer> bufferPair in bufferList)
                    {
                        bufferPair.Value.QueueBuffer();
                    }
                }

                opened = true;
            }
            catch (Exception e)
            {
                log.ErrorFormat("Failed to open device. {0}", e);
                DiscardBuffers();
                CloseDataStream();
                CloseDevice();
            }

            return(opened);
        }
Exemplo n.º 2
0
        protected override Result Open()
        {
            #region system
            BGAPI2.SystemList.Instance.Refresh();

            foreach (KeyValuePair <string, BGAPI2.System> kv in BGAPI2.SystemList.Instance)
            {
                if (kv.Value.TLType == "U3V")
                {
                    _usbSystem = kv.Value;
                    break;
                }
            }
            if (_usbSystem == null)
            {
                return(new Result("Fail", "Failed to get usb system"));
            }
            #endregion
            #region interface
            _usbSystem.Open();
            _usbSystem.Interfaces.Refresh(100);

            foreach (KeyValuePair <string, BGAPI2.Interface> kv in _usbSystem.Interfaces)
            {
                if (kv.Value.TLType == "U3V")
                {
                    _usbInterface = kv.Value;
                    break;
                }
            }
            if (_usbInterface == null)
            {
                _usbSystem.Close();
                return(new Result("Fail", "Failed to get usb interface"));
            }
            #endregion
            #region device
            _usbInterface.Open();
            _usbInterface.Devices.Refresh(100);

            foreach (KeyValuePair <string, BGAPI2.Device> kv in _usbInterface.Devices)
            {
                if (kv.Value.TLType == "U3V")
                {
                    _usbDevice = kv.Value;
                    break;
                }
            }
            if (_usbDevice == null)
            {
                _usbInterface.Close();
                _usbSystem.Close();
                return(new Result("Fail", "Failed to find usb device"));
            }
            else if (_usbInterface.Devices.Count > 1)
            {
                _log.Warn("More than 1 usb devices found");
            }
            #endregion
            #region datastream
            _usbDevice.Open();
            _usbDevice.RemoteNodeList["TriggerMode"].Value = "Off";
            _usbDevice.DataStreams.Refresh();

            _usbDataStream = _usbDevice.DataStreams["Stream0"];
            _usbDataStream.Open();
            _bufList = _usbDataStream.BufferList;
            for (int i = 0; i < BUF_NUM; i++)
            {
                _bufList.Add(new BGAPI2.Buffer());
            }
            foreach (KeyValuePair <string, BGAPI2.Buffer> buf in _bufList)
            {
                buf.Value.QueueBuffer();
            }
            #endregion

            _usbDataStream.StartAcquisition();
            _usbDevice.RemoteNodeList["AcquisitionStart"].Execute();

            return(new Result("Ok"));
        }