Tektronix Technical Forums are maintained by community involvement. Feel free to post questions or respond to questions by other members. Should you require a time-sensitive answer, please contact your local Tektronix support center here.

Save HardCopy to PC

Programming examples for remote communication with instruments
C/C++, C#, Python, Matlab, LabVIEW
Post Reply
Scott H
Tektronix Applications
Tektronix Applications
Posts: 21
Joined: July 3rd, 2013, 9:29 am

Save HardCopy to PC

Post by Scott H » September 5th, 2017, 2:32 pm

The following is an example of using NI-VISA to get a screen image over USB from a TDS/TBS 1000/2000 series scope.

Code: Select all

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NationalInstruments.VisaNS; // Add Reference->NationalInstruments.VisaNS & NationalInstruments.common by installing NI-VISA .NET4 files
// Change Project Example Properties Applicaiton Target fromwork to ".Net Framework 4"

namespace Example7_HardCopy
{
    public partial class Form1 : Form
    {
        private MessageBasedSession mbSession;

        // Instrument VISA address
        string resourceString = "USB0::0x0699::0x03B2::QU010012::INSTR";

        // Where to save
        string target_path = @"C:\temp\";
        string target_name = @"TBS_screen.bmp";

        byte[] Block = null;

        public Form1()
        {
            InitializeComponent();

            // Open connection to instrument
            mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resourceString);

            // Change timeout to 10 sec.
            mbSession.Timeout = 10000;

            // Copy Image
            HardCopy();

            // Close instrument connection
            mbSession.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void HardCopy()
        {
            // Send commands as described in programmers manual
            mbSession.Write("HardCopy:FormatException BMP");
            mbSession.Write("Hardcopy:Layout Portrait");
            mbSession.Write("Hardcopy:port USB");
            mbSession.Write("Hardcopy Start");

            // Read bytes
            Block = mbSession.ReadByteArray();
            // Write bytes to file
            File.WriteAllBytes(target_path + target_name, Block);
        }
    }

}

Austin H
Tektronix Applications
Tektronix Applications
Posts: 6
Joined: October 29th, 2015, 9:21 am

Re: Save HardCopy to PC

Post by Austin H » February 5th, 2018, 2:39 pm

The above example will work if the image happens to fit within a single visa standard buffer but will only return one buffer size worth of bytes if the image happens to be larger. To work around this you need to read until there are no more bytes left to be sent. The most robust method that I have found is to catch the SuccessMaxCountRead visa exception. This exception indicates that a full buffer was transferred and implies that more data may be available to transfer. Additionally using the MessageBasedSession.ReadToFile() function we can streamline the instrument read and file write to a single function call. The code block will look like this now:

Code: Select all

                                     
Scope.ReadToFileAppends = true;
do
{
	Scope.ReadToFile(path + fname);
}
while (Scope.LastStatus.Equals(VisaStatusCode.SuccessMaxCountRead));
There are other methods to accomplish the same task but I have found them to be less robust or more restricting. For example one might say that you could just increase the size of the buffer. While this would work it would require you to either know the size of the file you would be sending before sending it which is impracticable or grossly over provision your buffer size which is not an efficient use of memory. Another method would be to read the first few bytes of the image as it is transferred and decode the file size from the byte values received. This is trivial for some file types and more difficult for others. This is why I believe the exception checking method is best practice.


Here is the example rewritten to include the above method:

Code: Select all

using System;
using NationalInstruments.VisaNS;

namespace instr_control
{
    public class Program
    {
        static void Main(string[] args)
        {
            MessageBasedSession Scope;
            string resourceString = "USB0::0x0699::0x03A6::C010000::INSTR";
            Scope = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resourceString);
            string path = @"C:\";
            string fname = @"temp" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".bmp";

            Scope.Timeout = 5000;
            Scope.Write("ACQuire:STATE STOP");
            Scope.Write("HardCopy:FormatException bmp");
            Scope.Write("Hardcopy:Layout Portrait");
            Scope.Write("HARDCopy:INKSaver OFF");
            Scope.Write("Hardcopy:port USB");
            Scope.Write("Hardcopy Start");
            try
            {
                Scope.ReadToFileAppends = true;
                do
                {
                    Scope.ReadToFile(path + fname);
                }
                while (Scope.LastStatus.Equals(VisaStatusCode.SuccessMaxCountRead)) ;
            }

            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
            Scope.Dispose();
        }
    }
}

Please discuss.
Tektronix Applications
Image

Post Reply

Return to “Remote Instrument Communication (Programming) Examples”

Who is online

Users browsing this forum: No registered users and 1 guest