Exemple #1
0
        /*public string read(int timeout_ms)
         * {
         *  // timeout_ms is the time out to lock the device
         *  string resp= string.Empty;
         *  try
         *  {
         *      mSession.LockResource(timeout_ms);
         *      resp = mSession.ReadString();
         *      mSession.UnlockResource();
         *      return resp;
         *  }
         *  catch (VisaException ex)
         *  {
         *      throw ex;
         *  }
         * }*/

        public byte[] readBytes()
        {
            try
            {
                return(mSession.ReadByteArray());
            }
            catch (VisaException ex)
            {
                throw ex;
            }
        }
        public int ReaddatabyNetWork(ref byte[] pdatabuff)
        {
            var length      = 0;
            var sourceArray = new byte[0x200008];

            try
            {
                sourceArray = mbSession.ReadByteArray();
            }
            catch (Exception)
            {
                MessageBox.Show("Read trace data from device has problem,please check the device");
            }
            var destinationArray = new byte[9];

            Array.Copy(sourceArray, 12, destinationArray, 0, 9);
            Array.Reverse(destinationArray);
            var num2 = 0.0;

            for (var i = 0; i < 9; i++)
            {
                var num4 = 0.0;
                var num5 = (destinationArray[i] - 0x30) & 0xff;
                num4  = num5 * Math.Pow(10.0, i);
                num2 += num4;
            }
            length = ((int)num2) + 2;
            Array.Copy(sourceArray, 0x15, pdatabuff, 0, length);
            return(length);
        }
Exemple #3
0
        private void cmdStartTest_Click(object sender, EventArgs e)
        {
            System.DateTime CurveTime;
            float           wfmPerSec = 0;

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            string temp;

            string SaveDirectory = txtSaveDirectory.Text;

            byte[] DataBuffer;
            int    AcqLength  = 0;
            int    CurveCount = 0;
            int    DataLength;
            int    BytesRemaining;

            // Curve data conversion parameters
            int   pt_off;
            float xinc;
            float xzero;
            float ymult;
            float yoff;
            float yzero;

            float xvalue;
            float yvalue;

            cmdStartTest.Enabled = false;
            lblCurveCount.Text   = CurveCount.ToString();
            lblWfmPerSec.Text    = wfmPerSec.ToString("f");
            Application.DoEvents();  // Allow the front panel to redraw

            // Check that the save directory is valid
            if (!System.IO.Directory.Exists(SaveDirectory))
            {
                MessageBox.Show("Invalid save directory.  Please enter a valid directory then try again.", "Error: Invalid Directory", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmdStartTest.Enabled = true;
                return;
            }

            // Prompt the user to prep the scope
            if (MessageBox.Show("Please setup the scope then press OK to start Curve Streaming.  Once Curve Streaming has started you will not be able to control the scope until Curve Streaming is ended.",
                                "Setup Scope",
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Information) == DialogResult.Cancel)
            {
                cmdStartTest.Enabled = true;
                return;
            }

            // Open a connection to the instrument
            try
            {
                TekScope = new MessageBasedSession(txtVisaResourceName.Text.Trim());
                TekScope.Clear();
            }
            catch (Exception ex)
            {
                // Show and error message then exit if the connection fails
                MessageBox.Show(ex.Message, "Error Opening Connection to Instrument", MessageBoxButtons.OK, MessageBoxIcon.Error);
                TekScope             = null;
                cmdStartTest.Enabled = true;
                return;
            }

            GatherCurves       = true;
            cmdEndTest.Enabled = true;

            // Setup the waveform transfer
            TekScope.Write("*CLS");
            TekScope.Write("*CLE");
            TekScope.Write("DATa:SOUrce CH1");
            TekScope.Write("DATa:ENCdg RIBinary");
            TekScope.Write("DATa:STARt 0");
            TekScope.Write("HORizontal:ACQLENGTH?");
            temp      = TekScope.ReadString().Trim();
            AcqLength = Int32.Parse(temp);
            TekScope.Write(String.Format("DATa:STOP {0}", AcqLength));
            TekScope.Write("WFMOutpre:ENCdg BINary");
            TekScope.Write("WFMOutpre:BYT_Nr 1");

            // Get the needed values from the scope to scale the data
            TekScope.Write("WFMOutpre:PT_Off?");
            temp   = TekScope.ReadString().Trim();
            pt_off = Int32.Parse(temp);
            TekScope.Write("WFMOutpre:XINcr?");
            temp = TekScope.ReadString().Trim();
            xinc = Single.Parse(temp);
            TekScope.Write("WFMOutpre:XZEro?");
            temp  = TekScope.ReadString().Trim();
            xzero = Single.Parse(temp);
            TekScope.Write("WFMOutpre:YMUlt?");
            temp  = TekScope.ReadString().Trim();
            ymult = Single.Parse(temp);
            TekScope.Write("WFMOutpre:YOFf?");
            temp = TekScope.ReadString().Trim().TrimEnd('0').TrimEnd('.');
            yoff = Single.Parse(temp);
            TekScope.Write("WFMOutpre:YZEro?");
            temp  = TekScope.ReadString().Trim();
            yzero = Single.Parse(temp);

            // Turn on curve streaming
            TekScope.Write("CURVEStream?");
            stopWatch.Reset();
            stopWatch.Start();

            // While still collecting curves.  Ends when the user clicks End Test
            while (GatherCurves)
            {
                int    NumBytesCharCount = 0;
                string BlockHeader       = "";

                TekScope.Timeout = 200;

                // Loop until the block header is found
                while (BlockHeader.Length == 0)
                {
                    try
                    {
                        // Read the length of the string that contains the length of the data
                        // Note: If this times out it just means that no curve has been sent out yet so need to wait again
                        BlockHeader = TekScope.ReadString(2);

                        if (BlockHeader == ";\n") // Then it's the terminator from the previous curve so throw it out and try again.
                        {
                            BlockHeader = "";
                        }
                    }
                    catch (VisaException ex)
                    {
                        if (ex.ErrorCode != VisaStatusCode.ErrorTimeout) // Then still waiting on another curve to come in
                        {
                            MessageBox.Show(ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;
                        }
                    }

                    wfmPerSec         = (float)CurveCount / ((float)stopWatch.ElapsedMilliseconds / (float)1000);
                    lblWfmPerSec.Text = wfmPerSec.ToString("f");
                    Application.DoEvents();
                    if (!GatherCurves)
                    {
                        break;
                    }
                }
                if (!GatherCurves)
                {
                    break;
                }

                // Create a file with the current date and time as the name
                CurveTime = System.DateTime.Now;
                string FileName = String.Format("{0}{1}-{2:D2}-{3:D2}_{4:D2}{5:D2}{6:D2}.{7:D3}.csv",
                                                txtSaveDirectory.Text,
                                                CurveTime.Year,
                                                CurveTime.Month,
                                                CurveTime.Day,
                                                CurveTime.Hour,
                                                CurveTime.Minute,
                                                CurveTime.Second,
                                                CurveTime.Millisecond);
                StreamWriter SaveFile = new StreamWriter(FileName, false, Encoding.ASCII, BUFFER_SIZE * 10);

                // Calculate the xvalue for the first point in the record
                xvalue = (float)(-pt_off * (xinc + xzero));

                // Get the number of bytes that make up the data length string
                NumBytesCharCount = Int32.Parse(BlockHeader.TrimStart('#'), System.Globalization.NumberStyles.HexNumber);

                // Read the data length string
                temp           = TekScope.ReadString(NumBytesCharCount);
                DataLength     = int.Parse(temp);
                BytesRemaining = DataLength;

                // Read the back the data, process it and save it to the file
                TekScope.Timeout = 5000;
                while (BytesRemaining > 0)
                {
                    // Read bytes from scope
                    if (BytesRemaining >= BUFFER_SIZE)
                    {
                        DataBuffer      = TekScope.ReadByteArray(BUFFER_SIZE);
                        BytesRemaining -= BUFFER_SIZE;
                    }
                    else
                    {
                        DataBuffer     = TekScope.ReadByteArray(BytesRemaining);
                        BytesRemaining = 0;
                    }

                    // Convert byte values to floating point values then write to .csv file
                    foreach (byte DataPoint in DataBuffer)
                    {
                        yvalue = (float)((Convert.ToSingle((sbyte)DataPoint) - yoff) * (ymult + yzero));
                        SaveFile.WriteLine(xvalue.ToString() + "," + yvalue.ToString());
                        // Note: Converting to .CSV is very time consuming operation.
                        // Save in a binary format to maximize speed.  Highly recommended for waveforms >= 1 Million points.
                        xvalue += xinc;
                    }
                }

                SaveFile.Close();

                CurveCount++;
                wfmPerSec          = (float)CurveCount / ((float)stopWatch.ElapsedMilliseconds / (float)1000);
                lblWfmPerSec.Text  = wfmPerSec.ToString("f");
                lblCurveCount.Text = CurveCount.ToString();
                Application.DoEvents();
            }

            // Send Device Clear to stop the curve streaming
            TekScope.Clear();

            TekScope.Dispose();
            TekScope             = null;
            cmdStartTest.Enabled = true;
            cmdEndTest.Enabled   = false;
        }