]> git.zerfleddert.de Git - rigol/blame - usbtmc.c
tokens can be NULL
[rigol] / usbtmc.c
CommitLineData
58a9e276
MG
1#include <string.h>
2#include <stdio.h>
3#include <stdint.h>
4#include <usb.h>
5#include <arpa/inet.h>
6
7#include "usbtmc.h"
8
9//Helper-routine: Convert a little-endian 4-byte word to an int
10static void int2chars(unsigned char *buff,unsigned int a) {
11 buff[3]=(a>>24)&0xff;
12 buff[2]=(a>>16)&0xff;
13 buff[1]=(a>>8)&0xff;
14 buff[0]=(a)&0xff;
15}
16
17//Helper-routine: Convert an int to little-endian 4-byte word
18unsigned int chars2int(unsigned char *buff) {
19 unsigned int a;
20 a=buff[3]<<24;
21 a+=buff[2]<<16;
22 a+=buff[1]<<8;
23 a+=buff[0];
24 return a;
25}
26
27//This routine locates a scope by VID/PID and returns an opened handle to it.
28static usb_dev_handle *usbtmc_find_scope() {
29 struct usb_bus *bus;
30 struct usb_device *dev=NULL;
31 usb_find_busses();
32 usb_find_devices();
33 for (bus=usb_busses; bus; bus=bus->next) {
34 for (dev=bus->devices; dev; dev=dev->next) {
35 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
36 if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
37 return usb_open(dev);
38 }
39 }
40 }
41 return NULL;
42}
43
44//Send a scpi-command to the scope. The response goes into the buffer
45//called resp, with a size of resplen. If resp==NULL, no response
46//is requested.
47int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
48 unsigned char *resp, int resplen) {
49 unsigned char *buff;
50 int len,r,i;
51 int cmdlen = strlen(cmd);
52 static unsigned char seq=0;
53
54
55 buff=malloc(0x40);
56 seq++;
57 buff[0]=1; //func
58 buff[1]=seq; buff[2]=~seq; //nseq
59 buff[3]=0;
60 int2chars(buff+4, cmdlen);
61 buff[8]=1;
62 buff[9]=0x37;
63 buff[10]=0x39;
64 buff[11]=0x39;
65 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
66 //printb(buff,12);
67 r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
68 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
69 //printb(cmd, cmdlen);
70 r=usb_bulk_write(dev, 1, cmd, cmdlen, 1000);
71 //fprintf(stderr,"%i bytes written.\n",r);
72 if (resp != NULL && resplen != 0) {
73 //send read command
74 buff[0]=2; //func
75 seq++;
76 buff[1]=seq; buff[2]=~seq; //nseq
77 int2chars(buff+4,0x40);
78 buff[8]=1;
79 buff[9]=0xA;
80 buff[10]=0;
81 buff[11]=0;
82 //fprintf(stderr,"Writing resp req header\n");
83 //printb(buff,12);
84 r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
85 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
86 r=usb_bulk_read(dev, 2, (char*)buff, 0x40, 1000);
87 //printb(buff,r);
88 len=chars2int(buff+4);
89 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
90 for (i=0; i<(r-12); i++) {
91 if (i<resplen) resp[i] = buff[i+12];
92 }
93 //printb(resp,r-12);
94 if (len > 0x40-12) {
95 //fprintf(stderr," Reading response:\n");
96 if (resplen<len) len=resplen;
97 r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12),1000);
98 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
99 return r+(0x40-12);
100 }
101 return len;
102 }
103 return 0;
104}
105
ad9fbc05
MG
106void usbtmc_claim(usb_dev_handle *sc)
107{
108 usb_claim_interface(sc, 0);
109}
110
111void usbtmc_release(usb_dev_handle *sc)
112{
113 usb_release_interface(sc, 0);
114}
115
58a9e276
MG
116//Initialize the scope.
117usb_dev_handle* usbtmc_initscope(void) {
118 int r;
119 unsigned char buff[10];
120 usb_dev_handle *dev;
121
122 //Init libusb
123 usb_init();
124 //Locate and open the scope
125 dev = usbtmc_find_scope();
126 if (!dev) {
127 printf("No scope found.\n");
128 exit(1);
129 } else {
130 printf("Scope found.\n");
131 }
ad9fbc05 132 usbtmc_claim(dev);
58a9e276
MG
133 //The following code isn't really necessary, the program works
134 //OK without it too.
135 r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, 1000);
ad9fbc05 136 usbtmc_release(dev);
58a9e276
MG
137 if (r < 0) {
138 fprintf (stderr, "Error %d sending init message: %s\n",
139 r, strerror (-r));
140 fprintf (stderr, "Do you have permission on the USB device?\n");
141 exit (1);
142 }
b74fea90 143 if (chars2int(buff)!=0x40005dc) {
58a9e276
MG
144 fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
145 }
146 return dev;
147}
b74fea90
MG
148
149void usbtmc_close(usb_dev_handle *sc)
150{
151 //Free up and exit
b74fea90
MG
152 usb_close(sc);
153}
Impressum, Datenschutz