1
+ //-------------------------------------------------------------------------------
2
+ // Name: Save Hardcopy with C++ using VISA for 2/3/4/5/6 Series Scopes
3
+ // Purpose : This example demonstrates how to save a hard copy screen image from
4
+ // the scope to the PC.
5
+ //
6
+ // Created: 10/4/2023
7
+ //
8
+ // Compatible Instruments : 2/3/4/5/6 Series Riddick Based Oscilloscopes
9
+ //
10
+ // Compatible Interfaces : USB, Ethernet, GPIB
11
+ //
12
+ // Tektronix provides the following example "AS IS" with no support or warranty.
13
+ //
14
+ //-------------------------------------------------------------------------------
15
+ #define _CRT_SECURE_NO_WARNINGS
16
+
17
+ #include <stdio.h>
18
+ #include <string.h>
19
+ #include <stdlib.h>
20
+ #include <visa.h>
21
+
22
+ int main () {
23
+
24
+ ViStatus status ;
25
+ ViSession defaultRM , instr ;
26
+ ViUInt32 retCount ;
27
+ ViChar buffer [80000 ];
28
+
29
+ // Modify the following line to configure this script for your instrument
30
+ char resourceString [] = "TCPIP::10.233.70.92::INSTR" ;
31
+ ViBuf scpi ;
32
+
33
+ // Where to save (PC side)
34
+ char target_path [] = "C:\\tempx\\" ;
35
+ char target_name [] = "scope_screenshot.png" ;
36
+ char full_path [256 ];
37
+
38
+ // Initialize VISA session
39
+ status = viOpenDefaultRM (& defaultRM );;
40
+ if (status < VI_SUCCESS ) {
41
+ printf ("Failed to initialize\n" );
42
+ return -1 ;
43
+ }
44
+
45
+ // Open instrument connection
46
+ status = viOpen (defaultRM , resourceString , VI_NULL , VI_NULL , & instr );
47
+ if (status < VI_SUCCESS ) {
48
+ printf ("Error connecting to instrument\n" );
49
+ viStatusDesc (defaultRM , status , buffer );
50
+ printf ("%s\n" , buffer );
51
+ return 0 ;
52
+ }
53
+
54
+ // Set timeout
55
+ status = viSetAttribute (instr , VI_ATTR_TMO_VALUE , 10000 );
56
+
57
+ // Configure scope for screenshot, see programmers manual for scope-specific syntax
58
+ // Setting where to save screenshot on scope
59
+ scpi = "SAVE:IMAGE \"C:\\screenshots\\tek.png\"\n" ;
60
+ status = viWrite (instr , scpi , (ViUInt32 )strlen (scpi ), & retCount );
61
+ if (status < VI_SUCCESS ) {
62
+ printf ("Error writing to instrument\n" );
63
+ viStatusDesc (defaultRM , status , buffer );
64
+ printf ("%s\n" , buffer );
65
+ return 0 ;
66
+ }
67
+
68
+ // Transfer screenshot to PC
69
+ status = viWrite (instr , "FILESystem:READFile \"C:\\screenshots\\tek.png\"\n" , 45 , & retCount );
70
+ if (status < VI_SUCCESS ) {
71
+ printf ("Error writing to instrument\n" );
72
+ viStatusDesc (defaultRM , status , buffer );
73
+ printf ("%s\n" , buffer );
74
+ return 0 ;
75
+ }
76
+ status = viRead (instr , buffer , sizeof (buffer ), & retCount );
77
+ if (status < VI_SUCCESS ) {
78
+ printf ("Error reading from instrument\n" );
79
+ viStatusDesc (defaultRM , status , buffer );
80
+ printf ("%s\n" , buffer );
81
+ return 0 ;
82
+ }
83
+
84
+ snprintf (full_path , sizeof (full_path ), "%s%s" , target_path , target_name );
85
+ FILE * file = fopen (full_path , "wb" );
86
+ if (file == NULL ) {
87
+ printf ("Failed to open file for writing.\n" );
88
+ return -1 ;
89
+ }
90
+
91
+ size_t bytesWritten = fwrite (buffer , 1 , retCount , file );
92
+
93
+ // Check if the write operation was successful
94
+ if (bytesWritten != buffer ) {
95
+ perror ("Error writing to the file" );
96
+ fclose (file );
97
+ return 1 ; // Exit with an error code
98
+ }
99
+
100
+ fclose (file );
101
+
102
+ viClose (instr );
103
+ viClose (defaultRM );
104
+ return 0 ;
105
+ }
0 commit comments