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.

Capturing scope screen shots in C#

Post Reply
sean.rhinehart
Posts: 2
Joined: June 24th, 2017, 7:32 pm
Country: United States

Capturing scope screen shots in C#

Post by sean.rhinehart » June 24th, 2017, 9:10 pm

My employer has some TDS 3034B and 2024 scopes, and I have been tasked with writing some software to automatically capture and annotate screen captures from our scopes. The application logs the test conditions on the image, as well as in the TIFF metadata, so it can be organized and searched.

I have successfully used OpenChoiceDesktop to capture screens, and OpenChoice Call Monitor to see exactly how this was done, so I have had some success capturing images with my C# application. However, I have found that the C# API layer (above the viRead() function) can cause corruption of the binary image data, as it converts it from a binary byte array to a text unicode string, and frequently causes some bytes to be dropped.

OpenChoiceDesktop does not appear to use the C# API, and seems to use the lower level viRead calls, so it has more reliable screen capturing.

Questions:
1. Is it possible to augment the existing Read function of the C# API, with an overload function like this:
bool Read(out byte[] OutputBuffer, int BytesDesired, out BytesRead)
This function would keep the binary data intact, and would likely only be used for screen dumping.

2. Are there any examples of using the C# API with the lower level viRead calls as needed?

Thanks!

Best Regards,
Sean

sean.rhinehart
Posts: 2
Joined: June 24th, 2017, 7:32 pm
Country: United States

Re: Capturing scope screen shots in C#

Post by sean.rhinehart » June 26th, 2017, 12:18 pm

I found a solution, and I am sharing it in case anyone else has the same issue.

I found an ancient copy of Visa32.cs -, and from this I extracted just the information required to use the viRead function in Visa32.dll from C#.
internal static class Visa32Helper
{
[DllImportAttribute("VISA32.DLL", EntryPoint = "#256", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int viRead(int vi, byte[] buffer, int count, out int retCount);

public const int VI_SUCCESS = 0;
public const int VI_SUCCESS_TERM_CHAR = 1073676293;
public const int VI_SUCCESS_MAX_CNT = 1073676294;
}

And here is a C# Read() wrapper function that uses the viRead() function to return a byte[] instead of a unicode string:
private bool Read(int BytesDesired, out int BytesRead, out byte[] data)
{
// This is the function I would like to see in the Tek C# API, to support screen captures
bool success = false;
byte[] buffer = new byte[BytesDesired];
data = null;
BytesRead = 0;
int vi = (int)myVISA.Session;
int status = Visa32Helper.viRead(vi, buffer, BytesDesired, out BytesRead);

switch(status)
{
case Visa32Helper.VI_SUCCESS:
case Visa32Helper.VI_SUCCESS_TERM_CHAR:
case Visa32Helper.VI_SUCCESS_MAX_CNT:
success = true;
break;
}

if (success)
{
data = new byte[BytesRead];
Array.ConstrainedCopy(buffer, 0, data, 0, BytesRead);
}
return success;
}

Dave W
Tektronix Applications
Tektronix Applications
Posts: 270
Joined: April 26th, 2010, 12:01 pm
Country: United States

Re: Capturing scope screen shots in C#

Post by Dave W » July 28th, 2017, 11:55 am

Hi Sean,

It sounds like you're using Tek VISA in your application. For C# development I'd high recommend using NI-VISA as NI-VISA has a really great native C# interface and does not use interop with a COM driver like Tek VISA does. NI-VISA message based session objects have a member method ReadByteArray() that returns an array of bytes and removes the issue with ReadString() turning the data into unicode characters. The NI-VISA package also include really good standard .NET formatted documentation. As I said, I recommend using it for your C# development.

Post Reply

Return to “Programming Support”

Who is online

Users browsing this forum: No registered users and 19 guests