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);
        }