Пример #1
0
        /// <summary>
        /// Writes calibration data to an instrument.
        /// </summary>
        /// <param name="appSettings">ApplicationSettings object.</param>
        /// <param name="gpibAddress">GPIB address of instrument.</param>
        /// <param name="sramData">256 byte array containing data to write to instrument SRAM.</param>
        static void WriteCalibration(ApplicationSettings appSettings, int gpibAddress, byte[] sramData)
        {
            // Check for valid data buffer
            if (sramData == null || sramData.Length != 256)
            {
                throw new ArgumentException("SRAM data buffer must contain 256 bytes.", "sramData");
            }

            // Connect to GPIB adapter
            GPIBConnection gpibConn = new GPIBConnection(appSettings.SerialPortName,
                                                         appSettings.SerialBaudRate, appSettings.SerialDataBits,
                                                         appSettings.SerialParity, appSettings.SerialStopBits,
                                                         appSettings.SerialFlowControl)
            {
                Timeout = appSettings.GPIBAdapterTimeout,
                GPIBAdapterVersionString = appSettings.GPIBAdapterVersionString,
                GPIBAddress = gpibAddress
            };

            gpibConn.Connect();

            try
            {
                // Test instrument communications
                string cmdResult = gpibConn.QueryInstrument("S");
                if (cmdResult != "0" && cmdResult != "1")
                {
                    throw new ApplicationException("Could not communicate with instrument.");
                }

                // Display "WRITING CAL" on instrument
                gpibConn.QueryInstrument("D2WRITING CAL", false);
                gpibConn.QueryInstrumentBinary(new byte[] { 0x1b, 0x0d }, 0);  // Terminate D2 command

                // Setup command buffer to write calibration data.
                //
                // The first byte 'X' is the write SRAM command.
                //
                // The second and forth bytes are the ESC (ASCII 27) character
                // used to escape the following byte in order to ensure it gets
                // sent to the instrument and not consumed by the GPIB adapter.
                //
                // The third byte is the SRAM address to be written.
                //
                // The fifth byte is the SRAM data to be written.
                //
                // Notes:
                //   - SRAM is 256 x 4 (256 4-bit nibbles).
                //
                //   - X<addr><val> command is used to write SRAM nibbles where
                //     <addr> is an SRAM address from 0-255 and <val> is the data
                //     to be written.
                //
                //   - The instrument ignores the upper four bits of the data byte.
                //     For example, to write the value 0x5 to the instrument,
                //     sending a data byte of 0x05 or 0x45 ('E') are both equivalent.
                //
                //   - Only escaping CR, LF, ESC and '+' is required, but all
                //     characters are escaped for simplicity.
                byte[] writeCmdBytes = new byte[5];
                writeCmdBytes[0] = Convert.ToByte('X');
                writeCmdBytes[1] = 0x1b;
                writeCmdBytes[2] = 0x00;
                writeCmdBytes[3] = 0x1b;
                writeCmdBytes[4] = 0x00;

                // Setup command buffer to read SRAM
                byte[] readCmdBytes = new byte[3];
                readCmdBytes[0] = Convert.ToByte('W');
                readCmdBytes[1] = 0x1b;
                readCmdBytes[2] = 0x00;

                // Write all 256 nibbles of SRAM
                byte[] sramBytes = new byte[256];
                for (int i = 0; i < 256; i++)
                {
                    // Send command to instrument
                    writeCmdBytes[2] = (byte)i;
                    writeCmdBytes[4] = sramData[i];
                    gpibConn.QueryInstrumentBinary(writeCmdBytes, 0);

                    // Read value back for verification of successful write
                    readCmdBytes[2] = (byte)i;
                    byte[] recvByte = gpibConn.QueryInstrumentBinary(readCmdBytes, 1);

                    // Verify correct nibble was read back from instrument
                    if (recvByte == null || recvByte.Length != 1 ||
                        (recvByte[0] & 0x0f) != (sramData[i] & 0x0f))
                    {
                        throw new ApplicationException("Failed while verifying calibration data in instrument SRAM.");
                    }
                }

                // Return instrument to normal display
                gpibConn.QueryInstrument("D1", false);
            }
            finally
            {
                // Disconnect from GPIB adapter
                gpibConn.Disconnect();
            }
        }
Пример #2
0
        /// <summary>
        /// Reads calibration data from an instrument.
        /// </summary>
        /// <param name="appSettings">ApplicationSettings object.</param>
        /// <param name="gpibAddress">GPIB address of instrument.</param>
        /// <returns>256 byte array containing instrument SRAM data.</returns>
        static byte[] ReadCalibration(ApplicationSettings appSettings, int gpibAddress)
        {
            // Connect to GPIB adapter
            GPIBConnection gpibConn = new GPIBConnection(appSettings.SerialPortName,
                                                         appSettings.SerialBaudRate, appSettings.SerialDataBits,
                                                         appSettings.SerialParity, appSettings.SerialStopBits,
                                                         appSettings.SerialFlowControl)
            {
                Timeout = appSettings.GPIBAdapterTimeout,
                GPIBAdapterVersionString = appSettings.GPIBAdapterVersionString,
                GPIBAddress = gpibAddress
            };

            gpibConn.Connect();

            byte[] sramBytes;
            try
            {
                // Test instrument communications
                string cmdResult = gpibConn.QueryInstrument("S");
                if (cmdResult != "0" && cmdResult != "1")
                {
                    throw new ApplicationException("Could not communicate with instrument.");
                }

                // Display "READING CAL" on instrument
                gpibConn.QueryInstrument("D2READING CAL", false);
                gpibConn.QueryInstrumentBinary(new byte[] { 0x1b, 0x0d }, 0);  // Terminate D2 command

                // Setup command buffer to read calibration data.
                //
                // The first byte 'W' is the read SRAM command.
                //
                // The second byte is the ESC (ASCII 27) character used to escape
                // the following byte in order to ensure it gets sent to the
                // instrument and not consumed by the GPIB adapter.
                //
                // The third byte is the SRAM address to be read.
                //
                // Notes:
                //   - SRAM is 256 x 4 (256 4-bit nibbles).
                //
                //   - W<addr> command is used to read SRAM nibbles where <addr>
                //     is an SRAM address from 0-255.
                //
                //   - Nibbles are returned from the instrument with 0x40 added to
                //     them to make them human readable ASCII.
                //     For example, if the nibble value is 0x5, then 0x45 or 'E'
                //     is the byte returned.
                //
                //   - Only escaping CR, LF, ESC and '+' is required, but all
                //     characters are escaped for simplicity.
                byte[] cmdBytes = new byte[3];
                cmdBytes[0] = Convert.ToByte('W');
                cmdBytes[1] = 0x1b;
                cmdBytes[2] = 0x00;

                // Read all 256 nibbles of SRAM
                sramBytes = new byte[256];
                for (int i = 0; i < 256; i++)
                {
                    // Send command to instrument
                    cmdBytes[2] = (byte)i;
                    byte[] recvByte = gpibConn.QueryInstrumentBinary(cmdBytes, 1);

                    if (recvByte == null || recvByte.Length != 1)
                    {
                        throw new ApplicationException("Failed while reading calibration data from instrument SRAM.");
                    }

                    // Copy received byte to buffer
                    sramBytes[i] = recvByte[0];
                }

                // Return instrument to normal display
                gpibConn.QueryInstrument("D1", false);
            }
            finally
            {
                // Disconnect from GPIB adapter
                gpibConn.Disconnect();
            }

            return(sramBytes);
        }