forked from srinathr91/TestJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsbComm.java
More file actions
296 lines (270 loc) · 8.45 KB
/
UsbComm.java
File metadata and controls
296 lines (270 loc) · 8.45 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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.DoubleBuffer;
import java.nio.IntBuffer;
import java.util.List;
import javax.usb.UsbClaimException;
import javax.usb.UsbConfiguration;
import javax.usb.UsbConst;
import javax.usb.UsbControlIrp;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbDisconnectedException;
import javax.usb.UsbEndpoint;
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbInterfacePolicy;
import javax.usb.UsbIrp;
import javax.usb.UsbNotActiveException;
import javax.usb.UsbNotClaimedException;
import javax.usb.UsbPipe;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class UsbComm {
/**
* @param args
* @throws UsbDisconnectedException
* @throws IllegalArgumentException
* @throws UsbException
* @throws SecurityException
*/
// variable declaration
public static UsbDevice myDevice;
public UsbControlIrp irp;
final short vendor = 0x1cbe;
final short product = 0x0003;
public double sent, numBodies, mode, weeks, days;
public FileReader fr;
public double Mass, Px, Py, Vx, Vy;
public double sendarr[] = new double[5];
public byte[] senddata = new byte[40];
public byte[] sendinit = new byte[24];
byte[] body_params = new byte[40];
public double[] send_Init = new double[3];
public byte[] init_params = new byte[24];
public byte[] recvData = new byte[40];
public FileWriter fw;
public BufferedWriter bw;
public double[] recvval;
int count = 0;
// constructor to initialize my device and create controlIrp
public UsbComm() throws IllegalArgumentException, UsbDisconnectedException,
UsbException {
myDevice = findDevice(UsbHostManager.getUsbServices().getRootUsbHub(),
vendor, product);
//System.out.println(myDevice);
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);
}
// method to send initial values to the controller
public void sendInitialParams() throws UsbNotActiveException,
UsbNotClaimedException, UsbDisconnectedException, UsbException,
IOException {
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();
try {
fr = new FileReader("F:/eclipseworkspace/GUI/src/init.txt");
LineNumberReader lnr = new LineNumberReader(fr);
String str = lnr.readLine();
mode = Double.parseDouble(str);
str = lnr.readLine();
numBodies = Double.parseDouble(str);
str = lnr.readLine();
weeks = Double.parseDouble(str);
str = lnr.readLine();
days = Double.parseDouble(str);
send_Init[0] = mode;
send_Init[1] = numBodies;
double iteration = ((weeks * 7) + days);
send_Init[2] = iteration;
// send_Init[3] = days;
init_params = DoubleArray2ByteArray(send_Init);
ByteBuffer.wrap(sendinit).order(ByteOrder.LITTLE_ENDIAN)
.put(init_params);
sent = pipe.syncSubmit(sendinit);
//System.out.println("sent initial params");
String line = lnr.readLine();
try {
while (line.isEmpty()) {
int num = lnr.getLineNumber();
lnr.setLineNumber(num);
String string = lnr.readLine();
if ((!string.isEmpty())) {
Mass = Double.parseDouble(string);
string = lnr.readLine();
Px = Double.parseDouble(string);
string = lnr.readLine();
Py = Double.parseDouble(string);
string = lnr.readLine();
Vx = Double.parseDouble(string);
string = lnr.readLine();
Vy = Double.parseDouble(string);
line = lnr.readLine();
sendarr[0] = Mass;
sendarr[1] = Px;
sendarr[2] = Py;
sendarr[3] = Vx;
sendarr[4] = Vy;
body_params = DoubleArray2ByteArray(sendarr);
ByteBuffer.wrap(senddata)
.order(ByteOrder.LITTLE_ENDIAN)
.put(body_params);
sent = pipe.syncSubmit(senddata);
//System.out.println("Sent Mass values");
} else {
System.out.println("EOF");
}
}
} catch (Exception e) {
//System.out.println("end");
}
fr.close();
} finally {
pipe.close();
}
} finally {
iface.release();
}
//return send_Init;
}
// method to convert double array to byte array
public static byte[] DoubleArray2ByteArray(double[] values) {
ByteBuffer buffer = ByteBuffer.allocate(8 * values.length);
for (double value : values) {
buffer.order(ByteOrder.LITTLE_ENDIAN).putDouble(value);
}
return buffer.array();
}
// method to convert int array to byte array
public static byte[] IntArray2ByteArray(int[] values) {
ByteBuffer buffer = ByteBuffer.allocate(4 * values.length);
for (int value : values) {
buffer.putInt(value);
}
return buffer.array();
}
// method to receive data from the controller
public double[] receiveValFromCont() throws UsbClaimException,
UsbNotActiveException, UsbDisconnectedException, UsbException,
IOException {
double[] returnval = new double[3];
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) 0x81);
UsbPipe pipe = endpoint.getUsbPipe();
pipe.open();
try {
int received = pipe.syncSubmit(recvData);
DoubleBuffer recv;
// for(int m = 0; m<numBodies; m++){
// recv =
// ByteBuffer.wrap(recvData).order(ByteOrder.LITTLE_ENDIAN).get(body_final).asDoubleBuffer();
// double recve =
// ByteBuffer.wrap(recvData).order(ByteOrder.LITTLE_ENDIAN).getDouble();
// double[] copy = new double[recv.remaining()];
ByteBuffer buf2 = ByteBuffer.wrap(recvData).order(
ByteOrder.LITTLE_ENDIAN);
recvval = new double[recvData.length / 8];
//System.out.println("receive val" + recvval.length);
for (int i = 0; i < recvval.length; i++) {
recvval[i] = buf2.getDouble(i * 8);
//System.out.println(recvval[i]);
if(count==0 && i==0){
//System.out.println(recvval[i]);
}
count++;
if(count==numBodies){
count = 0;
}
// }
if(mode==0){
//System.out.println("entered");
fw = new FileWriter(
"F:/eclipseworkspace/GUI/src/final.txt", true);
bw = new BufferedWriter(fw);
for (int k = 0; k < recvval.length; k++) {
//System.out.println(recvval[k]);
String str = String.valueOf(recvval[k]);
//bw.write(k);
bw.write(str);
bw.newLine();
}
bw.newLine();
}
else{
fw = new FileWriter(
"F:/eclipseworkspace/GUI/src/viz.txt", true);
bw = new BufferedWriter(fw);
//int index = (int)recvval[0];
returnval[0] = recvval[0];
returnval[1] = recvval[2];
returnval[2] = recvval[3];
for (int k = 0; k < recvval.length; k++) {
String str = String.valueOf(recvval[k]);
//bw.write(k);
bw.write(str);
bw.newLine();
}
bw.newLine();
}
}
//System.out.println(received + " bytes received");
} finally {
pipe.close();
bw.close();
fw.close();
}
} finally {
iface.release();
}
if((returnval[0] > numBodies) && (returnval[0] < 0)){
return null;
}
else
return returnval;
}
// method to find my device
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;
}
}