public FBGAModule(RingBufferDouble ringBuffer)
 {
     this.ringBuffer     = ringBuffer;
     this.mwlsModule     = new MWLSModule();
     this.fbgaMwlsStatus = USBDeviceStateEnum.NOT_INITIALIZED;
     this.lockObject     = new System.Object();
 }
 public virtual void startAcquisition(bool trigger)
 {
     try
     {
         //if (fbgaMwlsStatus == USBDeviceStateEnum.INITIALIZED)
         //{
         if (DLL_Start_Continuously_Acquire_Spectra(trigger))
         {
             acquisition = true;
             thread      = new Thread(doProcessing);
             thread.Start();
             fbgaMwlsStatus = USBDeviceStateEnum.TRANSFERING;
         }
         else
         {
             throw new FBGAException("FBGA start acquisition failed: FBGA module can not start continuous acquisition");
         }
         //}
         //else throw new FBGAException("FBGA start acquisition failed: FBGA module is not initialized properly");
     }
     catch (Exception ex) {
         throw ex;
     }
 }
 public USBEventArgs(int index, USBDeviceStateEnum statue)
 {
     DeviceIndex = index;
     this.Status = statue;
 }
        protected void doProcessing()
        {
            int wProcessCode;
            int dwLineIndex, dwLineCount, dwPreviousLineCount;

            int[]    pdwLineStamp = new int[2], wPeakCount = new int[2], dwLineStamp = new int[2];
            double[] dblTemp      = new double[2];
            double[] pdblWL       = new double[512], pdblPwr = new double[512], pdblPeakWL = new double[512], pdblPeakPwr = new double[512], pdblPeakFWHM = new double[512], m_pdblBackground = new double[512];


            wPeakCount[0]       = 0;
            wProcessCode        = 0;
            dwLineIndex         = 0;
            dwLineCount         = 0;
            dwPreviousLineCount = 0;
            try
            {
                while (acquisition)
                {
                    Thread.Sleep(50);

                    dwLineCount = DLL_Get_TotalSpectrumCount();
                    if (dwPreviousLineCount > dwLineCount)
                    {
                        DLL_Stop_Continuously_Acquire_Spectra();
                        if (DLL_Start_Continuously_Acquire_Spectra(false))
                        {
                            wPeakCount[0]       = 0;
                            wProcessCode        = 0;
                            dwLineIndex         = 0;
                            dwLineCount         = 0;
                            dwPreviousLineCount = 0;
                        }
                        else
                        {
                            break;
                        }
                    }
                    dwPreviousLineCount = dwLineCount;
                    while (dwLineIndex < dwLineCount)
                    {
                        if (DLL_Get_SpectrumInformation(dwLineIndex, wProcessCode, m_pdblBackground, 0.0, null,
                                                        dwLineStamp, dblTemp, pdblWL, pdblPwr, wPeakCount, pdblPeakWL, pdblPeakPwr, pdblPeakFWHM))
                        {
                            //pdwLineStamp[dwLineIndex] = dwLineStamp[0];
                            if (dwLineIndex == dwLineCount - 1)
                            {
                                ringBuffer.Write(pdblPwr, 512);
                            }
                            dwLineIndex++;
                            //if (dwLineIndex >= 65535)
                            //{
                            //reverse = false;
                            //}
                            if (saveToFile)
                            {
                                lock (lockObject)
                                {
                                    byte[] byteArray;
                                    byteArray = BitConverter.GetBytes(dwLineStamp[0]);
                                    binaryWriter.Write(byteArray, 0, byteArray.Length);
                                    for (int j = 0; j < 512; j++)
                                    {
                                        byteArray = BitConverter.GetBytes(pdblPwr[j]);
                                        binaryWriter.Write(byteArray, 0, byteArray.Length);
                                    }
                                }
                            }
                        }
                    }
                }
                if ((fbgaMwlsStatus != USBDeviceStateEnum.NOT_INITIALIZED) && dllLibOpened)
                {
                    DLL_Stop_Continuously_Acquire_Spectra();
                }
                fbgaMwlsStatus = USBDeviceStateEnum.INITIALIZED;
                //DLL_Close_Device();
            }
            finally
            {
                if (binaryWriter != null)
                {
                    binaryWriter.Close();
                }
            }
        }
        public void TryInitialize(SettingFBGA fbgaSetting)
        {
            int a;

            int[]         pdwHW        = new int[2];
            int[]         pdwFW        = new int[2];
            StringBuilder stringlpszSN = new StringBuilder(10);

            string lpszSystemFileDirectory = System.IO.Directory.GetCurrentDirectory();

            fbgaMwlsStatus = USBDeviceStateEnum.NOT_INITIALIZED;

            if (22002082 != DLL_Get_DLL_Version())
            {
                return;
            }


            a = DLL_Connect_System(lpszSystemFileDirectory);
            if (a != 0)
            {
                return;
            }
            dllLibOpened = true;

            /* if (!DLL_Is_Device_OK())
             * {
             *   return;
             * }
             *
             * if (!DLL_Get_Device_SN(stringlpszSN)) return;
             * if (!DLL_Get_HW_FW_REV(pdwHW, pdwFW)) return;
             * if (512 != DLL_Get_Pixel_Count())
             * {
             *   return;
             * }
             * //set device sensor mode to 0(HighSensitive) 1(High Dynamic)
             * if (fbgaSetting.HighDynamicRange)
             * {
             *   if (!DLL_Set_Sensor_Mode(1)) return;
             * }
             * else
             * {
             *   if (!DLL_Set_Sensor_Mode(0)) return;
             * }
             * //set device sample integration time (1000us) and sample rate (500Hz) Tsr>Tint
             * if (!DLL_Set_IntegrationTime_SampleRate(500, 500)) return;
             *
             */
            /*try{
             *   mwlsModule.InitSLED(fbgaSetting.SLEDPower);
             * }
             * catch (InitMWLSException ex){
             *  fbgaMwlsStatus = USBDeviceStateEnum.INITIALIZED;
             *  return;
             * }
             * catch (TurnOnMWLSException ex){
             *  fbgaMwlsStatus = USBDeviceStateEnum.INITIALIZED_OFF;
             *  return;
             * }*/
            fbgaMwlsStatus = USBDeviceStateEnum.INITIALIZED;
        }
        public void Initialize(SettingFBGA fbgaSetting)
        {
            int a;

            int[]         pdwHW        = new int[2];
            int[]         pdwFW        = new int[2];
            StringBuilder stringlpszSN = new StringBuilder(10);

            string lpszSystemFileDirectory = System.IO.Directory.GetCurrentDirectory();

            fbgaMwlsStatus = USBDeviceStateEnum.NOT_INITIALIZED;

            if (22002082 != DLL_Get_DLL_Version())
            {
                throw new FBGAException("FBGA initialization failed: DLL version is not verified");
            }


            a = DLL_Connect_System(lpszSystemFileDirectory);
            switch (a)
            {
            case 0:
                break;

            case 1:
                throw new FBGAException("FBGA initialization failed: Device is acquiring spectrum in continuous mode");

            case 2:
                throw new FBGAException("FBGA initialization failed: Create USB instance failed");

            case 3:
                throw new FBGAException("FBGA initialization failed: No or multiple FBGA connected");

            case 4:
                throw new FBGAException("FBGA initialization failed: Initialize device failed");

            case 5:
                throw new FBGAException("FBGA initialization failed: Wrong sensor type");

            case 6:
                throw new FBGAException("FBGA initialization failed: Allocate memory failed");

            case 7:
                throw new FBGAException("FBGA initialization failed: Load system configuration file faile");

            case 8:
                throw new FBGAException("FBGA initialization failed: Load calibration data file faile");

            default:
                throw new FBGAException("FBGA initialization failed: Device not detected");
            }
            if (!DLL_Is_Device_OK())
            {
                throw new FBGAException("FBGA initialization failed: Device not OK");
            }

            if (!DLL_Get_Device_SN(stringlpszSN))
            {
                throw new FBGAException("FBGA initialization failed: Get device serial number error");
            }
            if (!DLL_Get_HW_FW_REV(pdwHW, pdwFW))
            {
                throw new FBGAException("FBGA initialization failed: Get HW FW error");
            }
            if (512 != DLL_Get_Pixel_Count())
            {
                throw new FBGAException("FBGA initialization failed: Device pixel count != 512");
            }
            //set device sensor mode to 0(HighSensitive) 1(High Dynamic)
            if (fbgaSetting.HighDynamicRange)
            {
                if (!DLL_Set_Sensor_Mode(1))
                {
                    throw new FBGAException("FBGA initialization failed: Set sensor mode error");
                }
            }
            else
            {
                if (!DLL_Set_Sensor_Mode(0))
                {
                    throw new FBGAException("FBGA initialization failed: Set sensor mode error");
                }
            }
            //set device sample integration time (1000us) and sample rate (500Hz) Tsr>Tint
            if (!DLL_Set_IntegrationTime_SampleRate(fbgaSetting.IntegrationTime, 5000))
            {
                throw new FBGAException("FBGA initialization failed: Set integrationTime/samplingRate error");
            }
            fbgaMwlsStatus = USBDeviceStateEnum.INITIALIZED;
        }