Victor86B digital multimeter app
[tridge/junkcode.git] / RawMeterReader.c
1 /*
2   raw meter reader for Linux
3
4   (C) Andrew Tridgell 2009
5   Released under GPLv3
6
7   to compile:
8
9     gcc RawMeterReader.c -o RawMeterReader.exe -lusb-1.0
10  */
11 #include <libusb-1.0/libusb.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16
17 int main(int argc, char *argv[])
18 {
19
20         libusb_context *ctx;
21         unsigned short vendor_id  = 0x1244;
22         unsigned short product_id = 0xd237;
23         libusb_device_handle *h;
24         int ret, nread;
25         unsigned char data[14];
26         unsigned char cdata[0x53];
27
28         libusb_init(&ctx);
29         libusb_set_debug(ctx, 3);
30         
31         h = libusb_open_device_with_vid_pid(ctx, vendor_id, product_id);
32         if (h == NULL) {
33                 fprintf(stderr, "Failed to open DMM device\n");
34                 exit(1);
35         }
36
37         libusb_detach_kernel_driver(h, 0);
38
39         ret = libusb_claim_interface(h, 0);
40         if (ret != 0) {
41                 fprintf(stderr, "Failed to claim interface\n");
42                 exit(1);
43         }
44
45         memset(cdata, 0, sizeof(cdata));
46
47         ret = libusb_control_transfer(h, 0x80, 0x06, 0x0200, 0, cdata, 0x22, 0); 
48         ret = libusb_control_transfer(h, 0x80, 0x06, 0x0200, 0, cdata, 0x22, 0); 
49         ret = libusb_control_transfer(h, 0x21, 0x0a, 0x0000, 0, cdata, 0, 0); 
50         ret = libusb_control_transfer(h, 0x81, 0x06, 0x2200, 0, cdata, 0x53, 0); 
51
52         while (1) {
53                 int i;
54
55                 memset(data, 0, sizeof(data));
56                 ret = libusb_interrupt_transfer(h, 0x81, data, sizeof(data), &nread, 10000);
57                 if (ret != 0 || nread != 14) {
58                         fprintf(stderr, "libusb_interrupt_transfer ret=%d nread=%d\n", ret, nread);
59                         continue;
60                 }
61                 printf("00 ");
62                 for (i=0;i<14;i++) {
63                         printf("%02X ", data[i]);
64                 }
65                 printf("\n");
66                 fflush(stdout);
67         }
68         
69         return 0;
70 }