move command functions in own file
[rigol] / rigol.c
CommitLineData
037166c1
MG
1/*
2Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux.
3This code is licensed under the GPL V3.
4
5Warning: This code can in theory fubar the communications with the scope to a
6point where the Linux USB-stack seems to get confused. Do a
7rmmod uhci_hcd; modprobe uhci_hcd
8(or alternately: use ohci_hcd) if that happens and you should be fine.
610d5b65 9 */
037166c1
MG
10
11#include <usb.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include <string.h>
15#include <sys/stat.h>
a8982973
MG
16#include <sys/types.h>
17#include <sys/wait.h>
037166c1
MG
18#include <fcntl.h>
19#include <unistd.h>
a8982973
MG
20#include <signal.h>
21#include <time.h>
037166c1
MG
22
23#include <readline/readline.h>
24#include <readline/history.h>
25
58a9e276 26#include "usbtmc.h"
0b7a29d9 27#include "commands.h"
037166c1 28
037166c1
MG
29#define MIN(a,b) (((a)<(b))?(a):(b))
30
31inline char printable (char ch)
32{
610d5b65
MG
33 if (ch < ' ') return '.';
34 if (ch > '~') return '.';
35 return ch;
037166c1
MG
36}
37
38//Debugging: Print a buffers contents in hex
39void printb (unsigned char *pkt, int len)
40{
610d5b65
MG
41 int i, j;
42
43 for (i=0;i<len;i+= 16) {
44 printf ("%04x: ", i);
45 for (j=0;j < MIN(len-i, 16);j++) {
46 printf (" %02x", pkt[i+j]);
47 if (j == 7) printf (" ");
48 }
49 if (j < 7) printf (" ");
50 for (;j<17;j++)
51 printf (" ");
52
53 for (j=0;j < MIN(len-i, 16);j++) {
54 printf ("%c", printable (pkt[i+j]));
55 }
56 printf ("\n");
57 }
037166c1
MG
58}
59
037166c1
MG
60#define HAVE_READLINE
61#ifndef HAVE_READLINE
62
63char *readline (prompt)
64{
610d5b65
MG
65 char *buf;
66
67 printf (prompt);
68 fflush (stdout);
69 buf = malloc (1024);
70
71 if (fgets (buf, 1023, stdin) == NULL) {
72 free (buf);
73 return NULL;
74 }
75 l = strlen (buf);
76 if (buf[l-1] == '\n') {
77 buf[l] = 0;
78 }
79 return buf;
037166c1
MG
80}
81
82void add_history (char *buf)
83{
84}
85#endif
86
a8982973
MG
87void child_reaper(int sig)
88{
89 pid_t child;
90
91 do {
92 child = waitpid(-1, NULL, WNOHANG);
93 } while(child > 0);
94
95}
037166c1
MG
96
97int main(int argc, char **argv)
98{
610d5b65
MG
99 struct usb_dev_handle *sc;
100 char *scpi;
101 unsigned char *buff;
102 int l;
a8982973
MG
103 struct sigaction act;
104
610d5b65 105 //Initialize scope
58a9e276 106 sc = usbtmc_initscope();
610d5b65 107 buff = malloc (1024*1024);
a8982973
MG
108 if (buff == NULL) {
109 perror("malloc");
110 exit(EXIT_FAILURE);
111 }
112
113 bzero(&act, sizeof(act));
114 act.sa_handler = child_reaper;
115 act.sa_flags = SA_NOCLDSTOP|SA_RESTART;
116 if (sigaction(SIGCHLD, &act, NULL) == -1) {
117 perror("sigaction");
118 exit(EXIT_FAILURE);
119 }
120
610d5b65
MG
121 while (1) {
122 scpi = readline ("> ");
123
124 if (!scpi) break;
125 if (strlen (scpi) == 0) {
126 free (scpi);
127 continue;
128 }
129
130 add_history (scpi);
131
132 if (strncmp (scpi, "quit", 4) == 0) break;
133 if (strncmp (scpi, "plot", 4) == 0) {
134 do_plot (sc);
135 continue;
136 }
a8982973 137 if (strncmp (scpi, "databuf", 7) == 0) {
610d5b65
MG
138 do_get_buf (sc);
139 continue;
140 }
a8982973
MG
141 if (strncmp (scpi, "screen", 6) == 0) {
142 do_get_screen (sc);
143 continue;
144 }
610d5b65
MG
145
146 l = strlen (scpi);
147 //printf ("got buf(%d): ", l);
148 //printb (scpi, l+2);
149 if (strchr (scpi, '?')) {
150 //printf ("Expecting reply\n");
58a9e276 151 l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024);
610d5b65
MG
152 //printf ("Got replylen = %d.\n", l);
153 buff[l] = 0; //zero-terminate
154 printb (buff, l);
155 } else {
156 //printf ("No reply expected\n");
58a9e276 157 l=usbtmc_sendscpi(sc,scpi,NULL,0);
610d5b65
MG
158 }
159 free (scpi);
160 }
161 //Disable keylock, so the user doesn't have to press the 'force'-button
58a9e276 162 l=usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0);
610d5b65
MG
163
164 //Free up and exit
165 usb_release_interface(sc,0);
166 usb_close(sc);
167 return 0;
037166c1 168}
Impressum, Datenschutz