-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUSBRead.cs
More file actions
143 lines (122 loc) · 4.8 KB
/
USBRead.cs
File metadata and controls
143 lines (122 loc) · 4.8 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
143
using System;
using System.Text;
using System.Threading;
using LibUsbDotNet;
using LibUsbDotNet.Main;
namespace ServerScan
{
internal class USBRead
{
private static UsbDevice usbDevice = null;
private static Boolean run = false;
private static Thread oThread = new Thread(new ThreadStart(USBRead.ReadDevice));
public static Boolean StartReading(Int32 vid, Int32 pid)
{
try
{
OpenDevice(vid, pid);
oThread.Start();
Thread.Sleep(1);
}
catch (Exception ex)
{
Program.ShowError(ex);
return false;
}
return true;
}
public static void StopReading()
{
run = false;
if (oThread.IsAlive)
oThread.Join();
}
public static Boolean IsReading()
{
return run;
}
public static void ReadDevice()
{
if (usbDevice == null)
Program.ShowError("Open device before trying to read.");
// open read endpoint 1.
UsbEndpointReader reader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
Logger.Log("Starting listening to device endpoint " + reader.EpNum);
try
{
run = true;
ErrorCode ec = ErrorCode.None;
byte[] readBuffer = new byte[128];
while (usbDevice != null && run)
{
int bytesRead;
// If the device hasn't sent data in the last 5 seconds,
// a timeout error (ec = IoTimedOut) will occur.
ec = reader.Read(readBuffer, Program.config.ButtonReadSize, out bytesRead);
// Start scan on signal.
if (ec == ErrorCode.None && bytesRead != 0)
{
Logger.Log("Received button signal");
Scan.StartScan();
}
}
}
finally
{
run = false;
CloseDevice();
}
}
public static void OpenDevice(Int32 vid, Int32 pid)
{
if (usbDevice != null)
Program.ShowError("A device is already openned, please close it first.");
Logger.Log("Connecting to device vid." + vid + " pid." + pid);
UsbDeviceFinder usbFinder = new UsbDeviceFinder(vid, pid);
// Find and open the usb device.
usbDevice = UsbDevice.OpenUsbDevice(usbFinder);
// If the device is open and ready
if (usbDevice == null) throw new Exception("Device Not Found.");
// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = usbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// This is a "whole" USB device. Before it can be used,
// the desired configuration and interface must be selected.
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
}
public static void CloseDevice()
{
if (usbDevice != null)
{
if (usbDevice.IsOpen)
{
Logger.Log("Closing device");
// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = usbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Release interface #0.
wholeUsbDevice.ReleaseInterface(0);
}
usbDevice.Close();
}
usbDevice = null;
// Free usb resources
UsbDevice.Exit();
}
}
}
}