-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusbhid.cpp
More file actions
142 lines (120 loc) · 3.37 KB
/
usbhid.cpp
File metadata and controls
142 lines (120 loc) · 3.37 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "stdafx.h"
#include "ethernet.h"
#include "comport.h"
#include "hex.h"
#include "usbhid.h"
#include "bootloader.h"
#include "pic32ubldlg.h"
#define USB_BUFFER_SIZE 64
/****************************************************************************
* Open USB device
*
* \param vid: Vendor Id
* \param pid: Product Id
* \param
* \return
*****************************************************************************/
BOOL CUsbHid::OpenUSBDevice(UINT vid, UINT pid)
{
hidDevice = hid_open(vid, pid, NULL);
if(!hidDevice)
{
QString error;
error.sprintf("USB Device with VID= %04X and PID= %04X is not found", vid, pid);
QMessageBox::warning(NULL, "Error opening USB Device", error, QMessageBox::Ok);
}
else
{
hid_set_nonblocking(hidDevice, true);
}
return (hidDevice != NULL);
}
/****************************************************************************
* Write USB device
*
* \param *buffer: Pointer to the buffer
* \param bufflen: Buffer length
* \param
* \return
*****************************************************************************/
BOOL CUsbHid::WriteUSBDevice(CHAR *buffer, INT bufflen)
{
int BytesWritten;
if(hidDevice == NULL || buffer == NULL || bufflen == 0)
{
return false;
}
BytesWritten = hid_write(hidDevice, (const unsigned char *)buffer, bufflen);
#ifdef QT_DEBUG
if (BytesWritten == -1)
{
std::cout << "WriteUSBDevice BytesWritten == -1" << std::endl;
}
else if (BytesWritten > 0)
{
std::cout << "WriteUSBDevice BytesWritten == " << BytesWritten << " bufflen: " << bufflen << std::endl;
}
#endif
return (BytesWritten != -1);
}
/****************************************************************************
* Read USB device
*
* \param *buffer: Pointer to the buffer
* \param bufflen: Maximum size of the read buffer. (Not used, since USB buffer is always 64bytes)
* \param
* \return
*****************************************************************************/
USHORT CUsbHid::ReadUSBDevice(CHAR* buffer, INT bufflen)
{
if(hidDevice == NULL)
{
return 0;
}
// int bytesRead = hid_read_timeout(hidDevice, (unsigned char *)buffer, bufflen, 1000);
int bytesRead = hid_read(hidDevice, (unsigned char *)buffer, bufflen);
if (bytesRead == -1)
{
bytesRead = 0;
#ifdef QT_DEBUG
std::cout << "ReadUSBDevice bytesRead == -1" << std::endl;
#endif
}
#ifdef QT_DEBUG
else if (bytesRead > 0)
{
std::cout << "ReadUSBDevice bytesRead == " << bytesRead << std::endl;
}
#endif
return bytesRead;
}
/****************************************************************************
* Check if USB port is already opened.
*
* \param
* \param
* \param
* \return
*****************************************************************************/
BOOL CUsbHid::GetPortOpenStatus(void)
{
return (hidDevice != NULL);
}
/****************************************************************************
* Close port
*
* \param
* \param
* \param
* \return
*****************************************************************************/
VOID CUsbHid::ClosePort()
{
if(hidDevice != NULL)
{
hid_close(hidDevice);
// Initialize device found to false.
hidDevice = NULL;
}
}
/***************************End ********************************************/