]> git.zerfleddert.de Git - rigol/blame - usbtmc.c
skeleton for rigold webserver
[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
106//Initialize the scope.
107usb_dev_handle* usbtmc_initscope(void) {
108 int r;
109 unsigned char buff[10];
110 usb_dev_handle *dev;
111
112 //Init libusb
113 usb_init();
114 //Locate and open the scope
115 dev = usbtmc_find_scope();
116 if (!dev) {
117 printf("No scope found.\n");
118 exit(1);
119 } else {
120 printf("Scope found.\n");
121 }
122 usb_claim_interface(dev,0);
123 //The following code isn't really necessary, the program works
124 //OK without it too.
125 r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, 1000);
126 if (r < 0) {
127 fprintf (stderr, "Error %d sending init message: %s\n",
128 r, strerror (-r));
129 fprintf (stderr, "Do you have permission on the USB device?\n");
130 exit (1);
131 }
b74fea90 132 if (chars2int(buff)!=0x40005dc) {
58a9e276
MG
133 fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
134 }
135 return dev;
136}
b74fea90
MG
137
138void usbtmc_close(usb_dev_handle *sc)
139{
140 //Free up and exit
141 usb_release_interface(sc,0);
142 usb_close(sc);
143}
Impressum, Datenschutz