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