1 package org.lsst.ccs.drivers.ftdi;
2
3 /**
4 ***************************************************************************
5 **
6 ** Accesses a device which uses the FTDI chip.
7 **
8 ** @author Owen Saxton
9 **
10 ***************************************************************************
11 */
12 public class Ftdi implements FtdiInterface {
13
14 /**
15 ***************************************************************************
16 **
17 ** Public constants.
18 **
19 ***************************************************************************
20 */
21 /** Word length value: 8 bits */
22 public final static int DATABITS_8 = 8;
23
24 /** Word length value: 7 bits */
25 public final static int DATABITS_7 = 7;
26
27 /** Stop bits value: 1 */
28 public final static int STOPBITS_1 = 0;
29
30 /** Stop bits value: 2 */
31 public final static int STOPBITS_2 = 2;
32
33 /** Parity value: none */
34 public final static int PARITY_NONE = 0;
35
36 /** Parity value: odd */
37 public final static int PARITY_ODD = 1;
38
39 /** Parity value: even */
40 public final static int PARITY_EVEN = 2;
41
42 /** Parity value: mark */
43 public final static int PARITY_MARK = 3;
44
45 /** Parity value: space */
46 public final static int PARITY_SPACE = 4;
47
48 /**
49 ***************************************************************************
50 **
51 ** Private fields.
52 **
53 ***************************************************************************
54 */
55 private FtdiClient client;
56 private FtdiLocal local;
57 private FtdiInterface ftdi;
58
59
60 /**
61 ***************************************************************************
62 **
63 ** Opens a local device.
64 **
65 ** @param index The zero-based index of the FTDI device within the
66 ** list selected by the serial argument.
67 **
68 ** @param serial A string which, if non-null and non-empty, restricts
69 ** the list of available devices to those with a serial
70 ** number containing this string.
71 **
72 ** @throws FtdiException
73 **
74 ***************************************************************************
75 */
76 @Override
77 public void open(int index, String serial) throws FtdiException
78 {
79 checkNotOpen();
80 if (local == null) {
81 local = new FtdiLocal();
82 }
83 local.open(index, serial);
84 ftdi = local;
85 }
86
87
88 /**
89 ***************************************************************************
90 **
91 ** Opens a local or remote device.
92 **
93 ** @param node The name of the node where the device is located, or
94 ** null to specify a local device.
95 **
96 ** @param index The zero-based index of the FTDI device within the
97 ** list selected by the serial argument.
98 **
99 ** @param serial A string which, if non-null and non-empty, restricts
100 ** the list of available devices to those with a serial
101 ** number containing this string.
102 **
103 ** @throws FtdiException
104 **
105 ***************************************************************************
106 */
107 @Override
108 public void open(String node, int index, String serial) throws FtdiException
109 {
110 if (node == null) {
111 open(index, serial);
112 }
113 else {
114 checkNotOpen();
115 if (client == null) {
116 client = new FtdiClient();
117 }
118 client.open(node, index, serial);
119 ftdi = client;
120 }
121 }
122
123
124 /**
125 ***************************************************************************
126 **
127 ** Closes the device.
128 **
129 ** @throws FtdiException
130 **
131 ***************************************************************************
132 */
133 @Override
134 public void close() throws FtdiException
135 {
136 checkOpen();
137 try {
138 ftdi.close();
139 }
140 finally {
141 ftdi = null;
142 }
143 }
144
145
146 /**
147 ***************************************************************************
148 **
149 ** Sets the baud rate.
150 **
151 ** @param baudrate The baud rate to set.
152 **
153 ** @throws FtdiException
154 **
155 ***************************************************************************
156 */
157 @Override
158 public void setBaudrate(int baudrate) throws FtdiException
159 {
160 checkOpen();
161 ftdi.setBaudrate(baudrate);
162 }
163
164
165 /**
166 ***************************************************************************
167 **
168 ** Sets the data characteristics.
169 **
170 ** @param wordLength The encoded word length to set.
171 **
172 ** @param stopBits The encoded number of stop bits to set.
173 **
174 ** @param parity The encoded parity value to set.
175 **
176 ** @throws FtdiException
177 **
178 ***************************************************************************
179 */
180 @Override
181 public void setDataCharacteristics(int wordLength, int stopBits,
182 int parity)
183 throws FtdiException
184 {
185 checkOpen();
186 ftdi.setDataCharacteristics(wordLength, stopBits, parity);
187 }
188
189
190 /**
191 ***************************************************************************
192 **
193 ** Sets the timeouts.
194 **
195 ** @param rcveTimeout The receive timeout to set (ms). A value of 0
196 ** means no timeout.
197 **
198 ** @param xmitTimeout The transmit timeout to set (ms). A value of 0
199 ** means no timeout.
200 **
201 ** @throws FtdiException
202 **
203 ***************************************************************************
204 */
205 @Override
206 public void setTimeouts(int rcveTimeout, int xmitTimeout)
207 throws FtdiException
208 {
209 checkOpen();
210 ftdi.setTimeouts(rcveTimeout, xmitTimeout);
211 }
212
213
214 /**
215 ***************************************************************************
216 **
217 ** Reads data.
218 **
219 ** Execution is blocked until either the byte array is filled, or a
220 ** timeout occurs. In the latter case the number of bytes read will be
221 ** less than the array size.
222 **
223 ** @param data A byte array to receive the read data.
224 **
225 ** @return The number of bytes read.
226 **
227 ** @throws FtdiException
228 **
229 ***************************************************************************
230 */
231 @Override
232 public int read(byte[] data) throws FtdiException
233 {
234 checkOpen();
235 return ftdi.read(data, 0, data.length);
236 }
237
238
239 /**
240 ***************************************************************************
241 **
242 ** Reads data.
243 **
244 ** Execution is blocked until either the requested number of bytes has
245 ** been read, or a timeout occurs. In the latter case the number of
246 ** bytes read will be less than the requested number.
247 **
248 ** @param data A byte array to receive the read data.
249 **
250 ** @param offset The offset in the array to the start of the data.
251 **
252 ** @param count The maximum number of bytes to read.
253 **
254 ** @return The number of bytes read.
255 **
256 ** @throws FtdiException
257 **
258 ***************************************************************************
259 */
260 @Override
261 public int read(byte[] data, int offset, int count) throws FtdiException
262 {
263 checkOpen();
264 return ftdi.read(data, offset, count);
265 }
266
267
268 /**
269 ***************************************************************************
270 **
271 ** Writes data.
272 **
273 ** @param data A byte array containing the data to write.
274 **
275 ** @return The number of bytes written.
276 **
277 ** @throws FtdiException
278 **
279 ***************************************************************************
280 */
281 @Override
282 public int write(byte[] data) throws FtdiException
283 {
284 checkOpen();
285 return ftdi.write(data, 0, data.length);
286 }
287
288
289 /**
290 ***************************************************************************
291 **
292 ** Writes data.
293 **
294 ** @param data A byte array containing the data to write.
295 **
296 ** @param offset The offset in the array to the start of the data.
297 **
298 ** @param count The number of bytes to write.
299 **
300 ** @return The number of bytes written.
301 **
302 ** @throws FtdiException
303 **
304 ***************************************************************************
305 */
306 @Override
307 public int write(byte[] data, int offset, int count) throws FtdiException
308 {
309 checkOpen();
310 return ftdi.write(data, offset, count);
311 }
312
313
314 /**
315 ***************************************************************************
316 **
317 ** Gets the read queue status.
318 **
319 ** This is the number of bytes available for immediate read, without
320 ** execution being blocked.
321 **
322 ** @return The number of bytes in the read queue.
323 **
324 ** @throws FtdiException
325 **
326 ***************************************************************************
327 */
328 @Override
329 public int getQueueStatus() throws FtdiException
330 {
331 checkOpen();
332 return ftdi.getQueueStatus();
333 }
334
335
336 /**
337 ***************************************************************************
338 **
339 ** Gets the modem status.
340 **
341 ** @return The modem status as a set of bits.
342 **
343 ** @throws FtdiException
344 **
345 ***************************************************************************
346 */
347 @Override
348 public int getModemStatus() throws FtdiException
349 {
350 checkOpen();
351 return ftdi.getModemStatus();
352 }
353
354
355 /**
356 ***************************************************************************
357 **
358 ** Checks that the device is not open
359 **
360 ** @throws FtdiException
361 **
362 ***************************************************************************
363 */
364 private void checkNotOpen() throws FtdiException
365 {
366 if (ftdi != null) {
367 throw new FtdiException("Device already open");
368 }
369 }
370
371
372 /**
373 ***************************************************************************
374 **
375 ** Checks that the device is open
376 **
377 ** @throws FtdiException
378 **
379 ***************************************************************************
380 */
381 private void checkOpen() throws FtdiException
382 {
383 if (ftdi == null) {
384 throw new FtdiException("Device not open");
385 }
386 }
387
388 }