View Javadoc

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