-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommUSB.cpp
113 lines (98 loc) · 2.65 KB
/
commUSB.cpp
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
#include "commUSB.hpp"
enum USB_STATUS usb_status = NORMAL;
/* constructor */
commUSB::commUSB()
{
}
/* destructor */
commUSB::~commUSB()
{
/* close the device */
hid_close(handle);
/* finalize the hidapi lib */
hid_exit();
}
/* initialization function */
void commUSB::commUSBInit()
{
int res;
/* initialize the hidapi lib */
res = hid_init();
if(res == -1)
{
std::cout << "hid_init failed" << std::endl;
usb_status = ERROR;
}
/* open the device using the VID, PID, and optionally the Serial number */
handle = hid_open(USB_VENDOR_ID , USB_PRODUCT_ID , NULL);
if(handle == NULL)
{
std::cout << "hid_open failed" << std::endl;
usb_status = ERROR;
}
/* read the manufacturer string */
res = hid_get_manufacturer_string(handle , wstr , MAX_STR);
if(res == -1)
{
std::cout << "hid_get_manufacturer_string failed" << std::endl;
usb_status = ERROR;
}
#ifdef PRINT_USB_DEVICE_INFO
std::cout << res << std::endl;
wprintf(L"Manufacturer String: %s\n" , wstr);
#endif
/* read the product string */
res = hid_get_product_string(handle , wstr , MAX_STR);
if(res == -1)
{
std::cout << "hid_get_product_string failed" << std::endl;
usb_status = ERROR;
}
#ifdef PRINT_USB_DEVICE_INFO
wprintf(L"Product String: %s\n" , wstr);
#endif
/* read the serial number string */
res = hid_get_serial_number_string(handle , wstr , MAX_STR);
if(res == -1)
{
std::cout << "hid_get_serial_number_string failed" << std::endl;
usb_status = ERROR;
}
#ifdef PRINT_USB_DEVICE_INFO
wprintf(L"Serial Number String: (%d) %s\n" , wstr[0] , wstr);
#endif
/* read indexed string 1 */
res = hid_get_indexed_string(handle , 1 , wstr , MAX_STR);
if(res == -1)
{
std::cout << "hid_get_indexed_string failed" << std::endl;
usb_status = ERROR;
}
#ifdef PRINT_USB_DEVICE_INFO
wprintf(L"Indexed String 1: %s\n" , wstr);
#endif
}
/* tranform function */
void commUSB::commUSBWrite()
{
hid_write(handle , txbuf , TX_DATA_LEN);
}
/* receive function */
void commUSB::commUSBRead()
{
hid_read(handle , rxbuf , RX_DATA_LEN);
}
/* print tx data & rx data info */
void commUSB::commUSBPrintResult()
{
/* print the tx data */
for (int i = 0 ; i < TX_DATA_LEN ; i++)
{
std::cout << "txbuf[ " << i << " ]: " << txbuf[i] << std::endl;
}
/* print the rx data */
for (int i = 0 ; i < RX_DATA_LEN ; i++)
{
std::cout << "rxbuf[ " << i << " ]: " << rxbuf[i] << std::endl;
}
}