int getPixels()
        {
            int retval = 0;
            int error  = 0;

            if (specIndex < 0)
            {
                return(retval);
            }

            bool ok = mut.WaitOne(2);

            if (!ok)
            {
                return(0);
            }

            retval = SeaBreezeWrapper.seabreeze_get_formatted_spectrum_length(specIndex, ref error);
            mut.ReleaseMutex();

            if (error != 0)
            {
                logger.queue("error getting pixel count (error = {0})", error);
                return(0);
            }
            return(retval);
        }
        string getModel()
        {
            int error = 0;

            if (specIndex < 0)
            {
                return(null);
            }

            byte[] buf = new byte[16];

            bool ok = mut.WaitOne(2);

            if (!ok)
            {
                return(null);
            }

            SeaBreezeWrapper.seabreeze_get_model(specIndex, ref error, ref buf[0], buf.Length);
            mut.ReleaseMutex();

            if (error != 0)
            {
                logger.queue("error getting model (error = {0})", error);
                return(null);
            }

            return(byteToString(buf));
        }
        double[] readWavelengths()
        {
            if (specIndex < 0)
            {
                return(null);
            }

            double[] wl = new double[pixels];

            int error = 0;

            bool ok = mut.WaitOne(2);

            if (!ok)
            {
                return(null);
            }

            SeaBreezeWrapper.seabreeze_get_wavelengths(specIndex, ref error, ref wl[0], pixels);
            mut.ReleaseMutex();

            if (error != 0)
            {
                logger.queue("Error calling readWavelengths(): errorCode = {0}", error);
                specIndex = -1;
                return(null);
            }

            return(wl);
        }
Exemplo n.º 4
0
        public string GetName()
        {
            string result = "Not Connected";

            if (isActive)
            {
                Int32 myErrorCode = 0;

                Int32  errorStringLength = SeaBreezeWrapper.seabreeze_get_error_string_maximum_length();
                byte[] errorStringBuffer = new byte[errorStringLength];

                Int32  modelStringLength = SeaBreezeWrapper.seabreeze_get_model_string_maximum_length();
                byte[] myModelTypeBuffer = new byte[modelStringLength];

                int bytesInBuffer = SeaBreezeWrapper.seabreeze_get_model(SEABREEZE_ID, ref myErrorCode, ref myModelTypeBuffer[0], modelStringLength);
                if (myErrorCode != 0)
                {
                    bytesInBuffer = SeaBreezeWrapper.seabreeze_get_error_string(myErrorCode, ref errorStringBuffer[0], errorStringLength);
                    string myErrorString = "Exception getting model description: " + System.Text.Encoding.Default.GetString(errorStringBuffer);
                    throw (new Exception(myErrorString));
                }

                result = System.Text.Encoding.UTF8.GetString(myModelTypeBuffer).TrimEnd((char)0);
            }
            return(result);
        }
Exemplo n.º 5
0
    public string getSpectrometerType()
    {
        string result = null;

        if (!initialized)
        {
            return(result);
        }

        mut.WaitOne();

        try
        {
            byte[] slot  = new byte[SeaBreezeWrapper.SLOT_LENGTH];
            int    error = 0;
            SeaBreezeWrapper.seabreeze_get_model(specIndex, ref error, ref slot[0], SeaBreezeWrapper.SLOT_LENGTH);
            if (checkSeaBreezeError("get_spectrometer_type", error))
            {
                result = byteToString(slot);
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting spectrometer type: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 6
0
    public string getVersion()
    {
        string result = null;

        mut.WaitOne();

        const int MAX_VERSION_LEN = 80;

        try
        {
            byte[] version = new byte[MAX_VERSION_LEN];

            int error = 0;
            SeaBreezeWrapper.seabreeze_get_api_version_string(ref version[0], version.Length);
            if (checkSeaBreezeError("get_api_version_string", error))
            {
                result = byteToString(version);
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting version string: {0}", e.Message);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 7
0
    public bool setTriggerMode(int mode)
    {
        bool result = false;

        if (!initialized)
        {
            return(result);
        }

        mut.WaitOne();
        try
        {
            int error = 0;
            SeaBreezeWrapper.seabreeze_set_trigger_mode(specIndex, ref error, mode);
            result = checkSeaBreezeError("set_integration_time_microsec", error);
        }
        catch (Exception e)
        {
            logger.log("Error setting trigger mode: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 8
0
    public bool open()
    {
        initialized = false;
        opened      = false;
        wavelengths = null;

        int error = 0;

        SeaBreezeWrapper.seabreeze_open_spectrometer(specIndex, ref error);
        if (!checkSeaBreezeError("open_spectrometer", error))
        {
            return(false);
        }
        opened = true;

        pixels = SeaBreezeWrapper.seabreeze_get_formatted_spectrum_length(specIndex, ref error);
        if (!checkSeaBreezeError("get_formatted_spectrum_length", error))
        {
            return(false);
        }

        double[] tmp = new double[pixels];
        SeaBreezeWrapper.seabreeze_get_wavelengths(specIndex, ref error, ref tmp[0], pixels);
        if (!checkSeaBreezeError("get_wavelengths", error))
        {
            return(false);
        }

        wavelengths = tmp;
        initialized = true;

        return(initialized);
    }
Exemplo n.º 9
0
    public int[] getEDCIndices()
    {
        int[] result = null;

        mut.WaitOne();

        try
        {
            int   error = 0;
            int[] buf   = new int[64];
            int   count = SeaBreezeWrapper.seabreeze_get_electric_dark_pixel_indices(specIndex, ref error, ref buf[0], 64);
            result = new int[count];
            for (int i = 0; i < count; i++)
            {
                result[i] = buf[i];
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting EDC indices: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 10
0
    public bool close()
    {
        bool result = false;

        opened = false;

        if (!initialized)
        {
            return(result);
        }

        mut.WaitOne();

        try
        {
            int error = 0;
            SeaBreezeWrapper.seabreeze_close_spectrometer(specIndex, ref error);
            result = checkSeaBreezeError("close_spectrometer", error);
        }
        catch (Exception e)
        {
            logger.log("Error closing spectrometer: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 11
0
    public double getCollectionArea()
    {
        double result = -1;

        mut.WaitOne();

        try
        {
            int    error = 0;
            double area  = SeaBreezeWrapper.seabreeze_read_irrad_collection_area(specIndex, ref error);
            if (checkSeaBreezeError("read_irrad_collection_area", error))
            {
                result = area;
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting collection area: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 12
0
    public double[] getIrradianceCalibration()
    {
        double[] result = null;

        mut.WaitOne();

        try
        {
            int     error   = 0;
            float[] irrad_f = new float[pixels];
            SeaBreezeWrapper.seabreeze_read_irrad_calibration(specIndex, ref error, ref irrad_f[0], pixels);
            if (checkSeaBreezeError("read_irrad_calibration", error))
            {
                result = new double[pixels];
                Array.Copy(irrad_f, result, pixels);
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting irradiance calibration: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 13
0
    public bool setIntegrationTimeMilliseconds(int ms)
    {
        bool result = false;

        if (!initialized)
        {
            return(result);
        }

        mut.WaitOne();

        try
        {
            int error = 0;
            SeaBreezeWrapper.seabreeze_set_integration_time_microsec(specIndex, ref error, (long)ms * 1000);
            result = checkSeaBreezeError("set_integration_time_microsec", error);
        }
        catch (Exception e)
        {
            logger.log("Error setting integration time: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 14
0
    public string getSerialNumber()
    {
        string result = null;

        if (!initialized)
        {
            return(result);
        }

        mut.WaitOne();

        try
        {
            byte[] slot = new byte[SeaBreezeWrapper.SLOT_LENGTH];

            int error = 0;
            SeaBreezeWrapper.seabreeze_get_serial_number(specIndex, ref error, ref slot[0], SeaBreezeWrapper.SLOT_LENGTH);
            if (checkSeaBreezeError("get_serial_number", error))
            {
                result = byteToString(slot);
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting serial number: {0}", e.Message);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 15
0
    public double[] getNLCCoeffs()
    {
        double[] result = null;

        if (!initialized)
        {
            return(result);
        }

        mut.WaitOne();

        try
        {
            double[] t     = new double[8];
            int      error = 0;
            for (int i = 0; i <= 7; i++)
            {
                byte[] buf        = new byte[SeaBreezeWrapper.SLOT_LENGTH];
                int    bytes_read = SeaBreezeWrapper.seabreeze_read_eeprom_slot(
                    specIndex, ref error, 6 + i, ref buf[0], SeaBreezeWrapper.SLOT_LENGTH);
                t[i] = Convert.ToDouble(byteToString(buf));
            }
            result = t;
        }
        catch (Exception e)
        {
            logger.log("Error getting NLC: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
        public bool init()
        {
            int error   = 0;
            int openErr = SeaBreezeWrapper.seabreeze_open_spectrometer(0, ref error);

            if (openErr != 0)
            {
                logger.queue("ERROR: SeaBreeze could not open first spectrometer");
                return(false);
            }

            Thread.Sleep(100); // Testing...

            numberOfSpectrometers = 1;
            specIndex             = 0;

            logger.queue("Found {0} spectrometers", numberOfSpectrometers);
            if (numberOfSpectrometers < 1)
            {
                logger.queue("ERROR: no spectrometers found");
                return(false);
            }
            if (numberOfSpectrometers > 1)
            {
                logger.queue("WARNING: defaulting to first-detected spectrometer");
            }

            ////////////////////////////////////////////////////////////////////
            // initialize metadata for each spectrometer
            ////////////////////////////////////////////////////////////////////

            pixels       = getPixels();
            wavelengths  = readWavelengths();
            serialNumber = getSerialNumber();
            modelName    = getModel();

            ////////////////////////////////////////////////////////////////////
            // init GPIO
            ////////////////////////////////////////////////////////////////////

            gpio = new FeatureControllerGPIO();
            gpio.init();
            gpio.setCacheEnabled(true);

            ////////////////////////////////////////////////////////////////////
            // finished
            ////////////////////////////////////////////////////////////////////

            logger.queue("Spectrometer at index {0} is a {1} with serial {2} and {3} pixels from {4:0.00} to {5:0.00}nm",
                         specIndex, modelName, serialNumber, pixels, wavelengths[0], wavelengths[wavelengths.Length - 1]);

            return(true);
        }
        // NO INTERNAL MUTEX! Caller must synchronize
        public bool writeUSB(byte[] data)
        {
            int error = 0;

            string debug = "";

            for (int i = 0; i < data.Length; i++)
            {
                debug += String.Format(" 0x{0:x2}", data[i]);
            }
            logger.log(">> {0}", debug);

            SeaBreezeWrapper.seabreeze_write_usb(specIndex, ref error, USB_TX_ENDPOINT, ref data[0], data.Length);
            return(error == 0);
        }
        // NO INTERNAL MUTEX! Caller must synchronize
        public bool readUSB(ref byte[] buffer)
        {
            int errorCode = 0;

            SeaBreezeWrapper.seabreeze_read_usb(specIndex, ref errorCode, USB_RX_ENDPOINT, ref buffer[0], buffer.Length);

            string debug = "";

            for (int i = 0; i < buffer.Length; i++)
            {
                debug += String.Format(" 0x{0:x2}", buffer[i]);
            }
            logger.log("<< {0}", debug);

            return(errorCode == 0);
        }
Exemplo n.º 19
0
    // returns true if last operation was successful, false if last operation had an error
    private bool checkSeaBreezeError(string operation, int errorCode)
    {
        if (errorCode == SeaBreezeWrapper.ERROR_SUCCESS)
        {
            return(true);
        }

        byte[] buffer = new byte[64];
        SeaBreezeWrapper.seabreeze_get_error_string(errorCode, ref buffer[0], 64);
        string msg = byteToString(buffer);

        if (logger != null)
        {
            logger.log("[SeaBreeze] error during {0}: {1}", operation, msg);
        }

        return(false);
    }
Exemplo n.º 20
0
        // opens the first spectrometer found. It will have a SeaBreeze ID of 0
        public SeaBreezeSpectrometerClass(ListBox listBoxForLogging, ref Boolean isConnected)
        {
            int zero_on_success = 0;
            int errorCode       = 0;

            logListBox = listBoxForLogging;
            if (File.Exists(SeaBreezeWrapper.DLL))
            {
                zero_on_success = SeaBreezeWrapper.seabreeze_open_spectrometer(SEABREEZE_ID, ref errorCode);
                if ((zero_on_success == 0) && (errorCode == 0))
                {
                    isActive = true;
                    myName   = GetName();
                    logListBox.Items.Add("Connected to: " + myName);
                    isConnected = true;

                    if (myName.StartsWith("FLAMEX"))
                    {
                        myTests = new FlameTestClass(logListBox, SEABREEZE_ID);
                    }
                    else if (myName.StartsWith("STS"))
                    {
                        myTests = new STSTestClass(logListBox);
                    }
                    else if (myName.StartsWith("QEPRO"))
                    {
                        myTests = new QEPROTestClass(logListBox);
                    }
                    else
                    {
                        logListBox.Items.Add(string.Format("A spectrometer test class was not found for {0}.", myName));
                    }
                }
                isConnected = isActive;
            }
            else
            {
                throw (new Exception("The DLL library link was incorrect in the SeaBreezeWrapper.cs file."));
            }
        }
Exemplo n.º 21
0
    public double[] getSpectrum()
    {
        double[] result = null;

        mut.WaitOne();

        try
        {
            int      error = 0;
            double[] spec  = new double[pixels];

            SeaBreezeWrapper.seabreeze_get_formatted_spectrum(specIndex, ref error, ref spec[0], pixels);
            if (checkSeaBreezeError("get_formatted_spectrum", error))
            {
                // KLUDGE: Some spectrometers (e.g. HR4000) insert non-pixel data
                // into the first few pixels of the spectrum they report, which
                // we can override here to avoid confusing EDC and stray noise on
                // graphs.
                //
                // TODO: Put in appropriate logic based on spectrometer model.
                for (int i = 0; i < 5; i++)
                {
                    spec[i] = spec[5];
                }

                result = spec;
            }
        }
        catch (Exception e)
        {
            logger.log("Error getting spectrum: {0}", e);
        }
        finally
        {
            mut.ReleaseMutex();
        }

        return(result);
    }
Exemplo n.º 22
0
        public void Close()
        {
            if (isActive)
            {
                Int32  myErrorCode = 0;
                Int32  aLength     = SeaBreezeWrapper.seabreeze_get_error_string_maximum_length();
                byte[] myBuffer    = new byte[aLength];

                int    totallyBogus  = SeaBreezeWrapper.seabreeze_close_spectrometer(SEABREEZE_ID, ref myErrorCode);
                int    bytesInBuffer = SeaBreezeWrapper.seabreeze_get_error_string(myErrorCode, ref myBuffer[0], aLength);
                string myErrorString = "Exception closing spectrometer: " + System.Text.Encoding.Default.GetString(myBuffer);


                if (myErrorCode == 0)
                {
                    logListBox.Items.Add("Disconnected from spectrometer.");
                }
                else
                {
                    logListBox.Items.Add("Error disconnecting: " + myErrorString);
                }
            }
        }
Exemplo n.º 23
0
        public void GetSerialNumber()
        {
            Int32 myErrorCode = 0;

            Int32 errorStringLength = SeaBreezeWrapper.seabreeze_get_error_string_maximum_length();

            byte[] errorStringBuffer = new byte[errorStringLength];

            Int32 serialNumberStringLength = SeaBreezeWrapper.seabreeze_get_serial_number_max_length(SEABREEZE_ID, ref myErrorCode);

            byte[] mySerialNumberBuffer = new byte[serialNumberStringLength];

            int bytesInBuffer = SeaBreezeWrapper.seabreeze_get_serial_number(SEABREEZE_ID, ref myErrorCode, ref mySerialNumberBuffer[0], serialNumberStringLength);

            if (myErrorCode != 0)
            {
                bytesInBuffer = SeaBreezeWrapper.seabreeze_get_error_string(myErrorCode, ref errorStringBuffer[0], errorStringLength);
                string myErrorString = "Exception getting model description: " + System.Text.Encoding.Default.GetString(errorStringBuffer);
                throw (new Exception(myErrorString));
            }
            string mySerialNumber = System.Text.Encoding.UTF8.GetString(mySerialNumberBuffer).TrimEnd((char)0);

            Log("Serial Number: " + mySerialNumber);
        }
Exemplo n.º 24
0
        public FlameTestClass(ListBox lb, int spectrometerID) : base(lb)
        {
            int       errorCode        = 0;
            const int maxIndiciesCount = 32;

            int [] pixelIndicies  = new int[maxIndiciesCount];
            uint   numberOfPixels = 0;

            // these commands are called when the spectrometer is initialized
            Log("The Flame X sends introspective commands during initialization.");

            mySpectrometerID = spectrometerID;
            // even though these were called in the initialization, call again to display values.
            numberOfPixels = SeaBreezeWrapper.seabreeze_get_number_of_pixels(spectrometerID, ref errorCode);
            if (errorCode == SeaBreezeWrapper.ERROR_SUCCESS)
            {
                Log("Executed Command: 0x00110220  Get number of pixels. " + numberOfPixels.ToString());
            }
            else
            {
                Log("Failure getting pixel count: " + GetErrorDescription(errorCode));
            }

            int numberOfIndicies = SeaBreezeWrapper.seabreeze_get_active_pixel_indices(spectrometerID, ref errorCode, ref pixelIndicies[0], 32);

            if (errorCode == SeaBreezeWrapper.ERROR_SUCCESS)
            {
                string indicies = "";
                for (int x = 0; x < numberOfIndicies; x = x + 2)
                {
                    indicies = indicies + "(" + pixelIndicies[x].ToString() + ", " + pixelIndicies[x + 1].ToString() + ") ";
                }
                Log("Executed Command: 0x00110221  Get active pixel ranges:" + indicies);
            }
            else
            {
                Log("Failure getting pixel count: " + GetErrorDescription(errorCode));
            }

            int opticalDarkIndex = SeaBreezeWrapper.seabreeze_get_optical_dark_pixel_indices(spectrometerID, ref errorCode, ref pixelIndicies[0], 32);

            if (errorCode == SeaBreezeWrapper.ERROR_SUCCESS)
            {
                string indicies = "";
                for (int x = 0; x < opticalDarkIndex; x = x + 2)
                {
                    indicies = indicies + "(" + pixelIndicies[x].ToString() + ", " + pixelIndicies[x + 1].ToString() + ") ";
                }
                Log("Executed Command: 0x00110222  Get optical dark pixel ranges" + indicies);
            }
            else
            {
                Log("Failure getting pixel count: " + GetErrorDescription(errorCode));
            }

            int electricDarkPixelIndex = SeaBreezeWrapper.seabreeze_get_electric_dark_pixel_indices(spectrometerID, ref errorCode, ref pixelIndicies[0], 32);

            if (errorCode == SeaBreezeWrapper.ERROR_SUCCESS)
            {
                string indicies = "";
                for (int x = 0; x < electricDarkPixelIndex; x = x + 2)
                {
                    indicies = indicies + "(" + pixelIndicies[x].ToString() + ", " + pixelIndicies[x + 1].ToString() + ") ";
                }
                Log("Executed Command: 0x00110223  Get electrical dark pixel ranges" + indicies);
            }
            else
            {
                Log("Failure getting pixel count: " + GetErrorDescription(errorCode));
            }
        }
Exemplo n.º 25
0
        public string GetErrorDescription(int errorCode)
        {
            int stringlength = SeaBreezeWrapper.seabreeze_get_error_string(errorCode, ref errorMessage[0], errorStringLength);

            return(errorMessage.ToString());
        }