File: vish.cpp

package info (click to toggle)
vish 0.0.20130812-1
  • links: PTS
  • area: main
  • in suites: bullseye, jessie, jessie-kfreebsd, sid, trixie
  • size: 356 kB
  • ctags: 35
  • sloc: sh: 996; cpp: 97; makefile: 5
file content (115 lines) | stat: -rw-r--r-- 3,954 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <visa.h>

#include <iostream>

#include <cstring>

int main(int argc, char **argv)
{
        if(argc != 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
        {
                std::cout << "Usage: " << argv[0] << " VISA-RESOURCE" << std::endl
                        << "       " << argv[0] << " --scan" << std::endl;
                return (argc != 2);
        }
        else if(!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V"))
        {
                std::cout << PACKAGE " " VERSION << std::endl;
                return 0;
        }

        ViSession rmgr;

        if(viOpenDefaultRM(&rmgr) != VI_SUCCESS)
        {
                std::cerr << "E: Cannot open default resource manager" << std::endl;
                return 1;
        }

        if(!strcmp(argv[1], "--scan"))
        {
                ViFindList fl;
                ViUInt32 count;
                ViChar rsrc[256];
                ViStatus rc = viFindRsrc(rmgr, const_cast<ViChar *>("?*"), &fl, &count, rsrc);
                while(rc == VI_SUCCESS)
                {
                        std::cout << rsrc << std::endl;
                        rc = viFindNext(fl, rsrc);
                }
                std::cerr << "I: " << count << " devices found." << std::endl;
                viClose(fl);
                viClose(rmgr);
                return 0;
        }

        ViSession vi;

        if(viOpen(rmgr, argv[1], VI_NO_LOCK, 0, &vi) != VI_SUCCESS)
        {
                std::cerr << "E: Cannot open resource " << argv[1] << std::endl;
                return 1;
        }

        bool rigol_stb_workaround = false;

        {
                char buffer[256] = "*IDN?";
                ViUInt32 count;
                if(viWrite(vi, reinterpret_cast<ViPBuf>(buffer), 5, &count) != VI_SUCCESS ||
                        viRead(vi, reinterpret_cast<ViPBuf>(buffer), sizeof buffer, &count) != VI_SUCCESS)
                {
                        std::cerr << "E: Cannot query device type" << std::endl;
                        return 1;
                }
                if(count >= 5 && !strncmp(buffer, "Rigol", 5))
                        rigol_stb_workaround = true;
        }

        if(viEnableEvent(vi, VI_EVENT_SERVICE_REQ, VI_QUEUE, VI_NULL) != VI_SUCCESS)
        {
                std::cerr << "E: Cannot enable SRQ events" << std::endl;
                return 1;
        }

        for(std::string line; getline(std::cin, line) && line.size();)
        {
                ViUInt32 count;
                if(viWrite(vi, reinterpret_cast<ViPBuf>(const_cast<char *>(line.data())), line.size(), &count) != VI_SUCCESS)
                        std::cerr << "E: Cannot write to resource" << std::endl;

                if(!rigol_stb_workaround)
                {
                        ViEventType event;
                        if(viWaitOnEvent(vi, VI_ALL_ENABLED_EVENTS, 5000, &event, 0) != VI_SUCCESS)
                                std::cerr << "E: Cannot wait for events" << std::endl;
                }

                for(;;)
                {
                        ViUInt16 status;
                        if(!rigol_stb_workaround && viReadSTB(vi, &status) != VI_SUCCESS)
                                std::cerr << "E: Cannot read status" << std::endl;

                        if(rigol_stb_workaround || status & 16)
                        {
                                ViChar buffer[256000];
                                if(viRead(vi, reinterpret_cast<ViPBuf>(buffer), sizeof buffer, &count) != VI_SUCCESS)
                                        break;
                                std::cout << std::string(buffer, count);
                        }
                        else
                                break;
                }
                std::cout << std::endl;
        }

        viClose(vi);
        viClose(rmgr);

        return 0;
}