forked from srinathr91/TestJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsbcommtest.java
More file actions
134 lines (120 loc) · 4.22 KB
/
Usbcommtest.java
File metadata and controls
134 lines (120 loc) · 4.22 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package usbcomm1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;
import javax.usb.UsbConfiguration;
import javax.usb.UsbConst;
import javax.usb.UsbControlIrp;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbEndpoint;
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbPipe;
/**
*
* @author Srinath Ramesh
*/
public class Usbcommtest {
private static final short VENDOR_ID = 0x1cbe;
private static final short PRODUCT_ID = 0x0003;
/**
* @param args
* @throws UsbException
* @throws SecurityException
*/
public static void main(String[] args) throws SecurityException, UsbException {
// TODO Auto-generated method stub
final short vendor = 0x1cbe;
final short product = 0x0003;
UsbDevice myDevice = findDevice(UsbHostManager.getUsbServices().getRootUsbHub(),vendor, product);
System.out.println(myDevice);
UsbControlIrp irp = myDevice.createUsbControlIrp(
(byte) (UsbConst.REQUESTTYPE_DIRECTION_IN
| UsbConst.REQUESTTYPE_TYPE_STANDARD
| UsbConst.REQUESTTYPE_RECIPIENT_DEVICE),
UsbConst.REQUEST_GET_CONFIGURATION,
(short) 0,
(short) 0
);
irp.setData(new byte[1]);
myDevice.syncSubmit(irp);
System.out.println(irp.getData()[0]);
UsbConfiguration configuration = myDevice.getActiveUsbConfiguration();
System.out.println(configuration);
UsbInterface iface = configuration.getUsbInterface((byte) 0);
System.out.println(iface);
iface.claim();
try
{
UsbEndpoint endpoint = iface.getUsbEndpoint((byte) 1);
UsbPipe pipe = endpoint.getUsbPipe();
pipe.open();
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the input string");
try
{
String T= inp.readLine();
char[] c=T.toCharArray();
byte[] bytes = Charset.forName("UTF-8").encode(CharBuffer.wrap(c)).array();
int sent = pipe.syncSubmit(bytes );//{ 'a','b'}
System.out.println(sent + " bytes sent");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
pipe.close();
}
endpoint = iface.getUsbEndpoint((byte) 0x81);
pipe = endpoint.getUsbPipe();
pipe.open();
try {
byte[] data = new byte[256];
int received = pipe.syncSubmit(data);
double val;
val = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN ).getDouble();
for(int iu=0;iu<data.length;iu++){
//System.out.print(received + " bytes received"+" "+(char)data[0]+(char)data[1]);
if(iu==0){
System.out.println(received + " bytes received"+" ");
}System.out.print((char)data[iu]);
}
System.out.println();
} finally {
pipe.close();
}
}
finally
{
iface.release();
}
}
public static UsbDevice findDevice(UsbHub hub, short vendorId, short productId)
{
for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices())
{
UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
if (desc.idVendor() == vendorId && desc.idProduct() == productId) return device;
if (device.isUsbHub())
{
device = findDevice((UsbHub) device, vendorId, productId);
if (device != null) return device;
}
}
return null;
}
}