Debian Codegraph @

src/gnu/io/RS485.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*-------------------------------------------------------------------------
2 | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
3 | RXTX is a native interface to serial ports in java.
4 | Copyright 1997-2007 by Trent Jarvi [email protected] and others who
5 | actually wrote it. See individual source files for more information.
6 |
7 | A copy of the LGPL v 2.1 may be found at
8 | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is
9 | here for your convenience.
10 |
11 | This library is free software; you can redistribute it and/or
12 | modify it under the terms of the GNU Lesser General Public
13 | License as published by the Free Software Foundation; either
14 | version 2.1 of the License, or (at your option) any later version.
15 |
16 | This library is distributed in the hope that it will be useful,
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 | Lesser General Public License for more details.
20 |
21 | An executable that contains no derivative of any portion of RXTX, but
22 | is designed to work with RXTX by being dynamically linked with it,
23 | is considered a "work that uses the Library" subject to the terms and
24 | conditions of the GNU Lesser General Public License.
25 |
26 | The following has been added to the RXTX License to remove
27 | any confusion about linking to RXTX. We want to allow in part what
28 | section 5, paragraph 2 of the LGPL does not permit in the special
29 | case of linking over a controlled interface. The intent is to add a
30 | Java Specification Request or standards body defined interface in the
31 | future as another exception but one is not currently available.
32 |
33 | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
34 |
35 | As a special exception, the copyright holders of RXTX give you
36 | permission to link RXTX with independent modules that communicate with
37 | RXTX solely through the Sun Microsytems CommAPI interface version 2,
38 | regardless of the license terms of these independent modules, and to copy
39 | and distribute the resulting combined work under terms of your choice,
40 | provided that every copy of the combined work is accompanied by a complete
41 | copy of the source code of RXTX (the version of RXTX used to produce the
42 | combined work), being distributed under the terms of the GNU Lesser General
43 | Public License plus this exception. An independent module is a
44 | module which is not derived from or based on RXTX.
45 |
46 | Note that people who make modified versions of RXTX are not obligated
47 | to grant this special exception for their modified versions; it is
48 | their choice whether to do so. The GNU Lesser General Public License
49 | gives permission to release a modified version without this exception; this
50 | exception also makes it possible to release a modified version which
51 | carries forward this exception.
52 |
53 | You should have received a copy of the GNU Lesser General Public
54 | License along with this library; if not, write to the Free
55 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 | All trademarks belong to their respective owners.
57 --------------------------------------------------------------------------*/
58 package gnu.io;
59 import java.io.*;
60 import java.util.*;
61 import java.lang.Math;
62
63
64 /**
65 * @author Trent Jarvi
66 * @version %I%, %G%
67 * @since JDK1.0
68 */
69
70 final class RS485 extends RS485Port {
71
72 static
73 {
74 System.loadLibrary( "rxtxRS485" );
75 Initialize();
76 }
77
78
79 /** Initialize the native library */
80 private native static void Initialize();
81
82
83 /** Actual RS485Port wrapper class */
84
85
86 /** Open the named port */
87 public RS485( String name ) throws PortInUseException {
88 fd = open( name );
89 }
90 private native int open( String name ) throws PortInUseException;
91
92
93 /** File descriptor */
94 private int fd;
95
96 /** DSR flag **/
97 static boolean dsrFlag = false;
98
99 /** Output stream */
100 private final RS485OutputStream out = new RS485OutputStream();
101 public OutputStream getOutputStream() { return out; }
102
103
104 /** Input stream */
105 private final RS485InputStream in = new RS485InputStream();
106 public InputStream getInputStream() { return in; }
107
108
109
110
111 /** Set the RS485Port parameters */
112 public void setRS485PortParams( int b, int d, int s, int p )
113 throws UnsupportedCommOperationException
114 {
115 nativeSetRS485PortParams( b, d, s, p );
116 speed = b;
117 dataBits = d;
118 stopBits = s;
119 parity = p;
120 }
121
122 /** Set the native RS485 port parameters */
123 private native void nativeSetRS485PortParams( int speed, int dataBits,
124 int stopBits, int parity ) throws UnsupportedCommOperationException;
125
126 /** Line speed in bits-per-second */
127 private int speed=9600;
128 public int getBaudRate() { return speed; }
129
130 /** Data bits port parameter */
131 private int dataBits=DATABITS_8;
132 public int getDataBits() { return dataBits; }
133
134 /** Stop bits port parameter */
135 private int stopBits=RS485Port.STOPBITS_1;
136 public int getStopBits() { return stopBits; }
137
138 /** Parity port parameter */
139 private int parity= RS485Port.PARITY_NONE;
140 public int getParity() { return parity; }
141
142
143 /** Flow control */
144 private int flowmode = RS485Port.FLOWCONTROL_NONE;
145 public void setFlowControlMode( int flowcontrol ) {
146 try { setflowcontrol( flowcontrol ); }
147 catch( IOException e ) {
148 e.printStackTrace();
149 return;
150 }
151 flowmode=flowcontrol;
152 }
153 public int getFlowControlMode() { return flowmode; }
154 native void setflowcontrol( int flowcontrol ) throws IOException;
155
156
157 /*
158 linux/drivers/char/n_hdlc.c? FIXME
159 [email protected]
160 */
161 /** Receive framing control
162 */
163 public void enableReceiveFraming( int f )
164 throws UnsupportedCommOperationException
165 {
166 throw new UnsupportedCommOperationException( "Not supported" );
167 }
168 public void disableReceiveFraming() {}
169 public boolean isReceiveFramingEnabled() { return false; }
170 public int getReceiveFramingByte() { return 0; }
171
172
173 /** Receive timeout control */
174 private int timeout = 0;
175
176 public native int NativegetReceiveTimeout();
177 public native boolean NativeisReceiveTimeoutEnabled();
178 public native void NativeEnableReceiveTimeoutThreshold(int time, int threshold,int InputBuffer);
179 public void disableReceiveTimeout(){
180 enableReceiveTimeout(0);
181 }
182 public void enableReceiveTimeout( int time ){
183 if( time >= 0 ) {
184 timeout = time;
185 NativeEnableReceiveTimeoutThreshold( time , threshold, InputBuffer );
186 }
187 else {
188 System.out.println("Invalid timeout");
189 }
190 }
191 public boolean isReceiveTimeoutEnabled(){
192 return(NativeisReceiveTimeoutEnabled());
193 }
194 public int getReceiveTimeout(){
195 return(NativegetReceiveTimeout( ));
196 }
197
198 /** Receive threshold control */
199
200 private int threshold = 0;
201
202 public void enableReceiveThreshold( int thresh ){
203 if(thresh >=0)
204 {
205 threshold=thresh;
206 NativeEnableReceiveTimeoutThreshold(timeout, threshold, InputBuffer);
207 }
208 else /* invalid thresh */
209 {
210 System.out.println("Invalid Threshold");
211 }
212 }
213 public void disableReceiveThreshold() {
214 enableReceiveThreshold(0);
215 }
216 public int getReceiveThreshold(){
217 return threshold;
218 }
219 public boolean isReceiveThresholdEnabled(){
220 return(threshold>0);
221 }
222
223 /** Input/output buffers */
224 /** FIXME I think this refers to
225 FOPEN(3)/SETBUF(3)/FREAD(3)/FCLOSE(3)
226 [email protected]
227
228 These are native stubs...
229 */
230 private int InputBuffer=0;
231 private int OutputBuffer=0;
232 public void setInputBufferSize( int size )
233 {
234 InputBuffer=size;
235 }
236 public int getInputBufferSize()
237 {
238 return(InputBuffer);
239 }
240 public void setOutputBufferSize( int size )
241 {
242 OutputBuffer=size;
243 }
244 public int getOutputBufferSize()
245 {
246 return(OutputBuffer);
247 }
248
249 /** Line status methods */
250 public native boolean isDTR();
251 public native void setDTR( boolean state );
252 public native void setRTS( boolean state );
253 private native void setDSR( boolean state );
254 public native boolean isCTS();
255 public native boolean isDSR();
256 public native boolean isCD();
257 public native boolean isRI();
258 public native boolean isRTS();
259
260
261 /** Write to the port */
262 public native void sendBreak( int duration );
263 private native void writeByte( int b ) throws IOException;
264 private native void writeArray( byte b[], int off, int len )
265 throws IOException;
266 private native void drain() throws IOException;
267
268
269 /** RS485 read methods */
270 private native int nativeavailable() throws IOException;
271 private native int readByte() throws IOException;
272 private native int readArray( byte b[], int off, int len )
273 throws IOException;
274
275
276 /** RS485 Port Event listener */
277 private RS485PortEventListener SPEventListener;
278
279 /** Thread to monitor data */
280 private MonitorThread monThread;
281
282 /** Process RS485PortEvents */
283 native void eventLoop();
284 private int dataAvailable=0;
285 public void sendEvent( int event, boolean state ) {
286 switch( event ) {
287 case RS485PortEvent.DATA_AVAILABLE:
288 dataAvailable=1;
289 if( monThread.Data ) break;
290 return;
291 case RS485PortEvent.OUTPUT_BUFFER_EMPTY:
292 if( monThread.Output ) break;
293 return;
294 /*
295 if( monThread.DSR ) break;
296 return;
297 if (isDSR())
298 {
299 if (!dsrFlag)
300 {
301 dsrFlag = true;
302 RS485PortEvent e = new RS485PortEvent(this, RS485PortEvent.DSR, !dsrFlag, dsrFlag );
303 }
304 }
305 else if (dsrFlag)
306 {
307 dsrFlag = false;
308 RS485PortEvent e = new RS485PortEvent(this, RS485PortEvent.DSR, !dsrFlag, dsrFlag );
309 }
310 */
311 case RS485PortEvent.CTS:
312 if( monThread.CTS ) break;
313 return;
314 case RS485PortEvent.DSR:
315 if( monThread.DSR ) break;
316 return;
317 case RS485PortEvent.RI:
318 if( monThread.RI ) break;
319 return;
320 case RS485PortEvent.CD:
321 if( monThread.CD ) break;
322 return;
323 case RS485PortEvent.OE:
324 if( monThread.OE ) break;
325 return;
326 case RS485PortEvent.PE:
327 if( monThread.PE ) break;
328 return;
329 case RS485PortEvent.FE:
330 if( monThread.FE ) break;
331 return;
332 case RS485PortEvent.BI:
333 if( monThread.BI ) break;
334 return;
335 default:
336 System.err.println("unknown event:"+event);
337 return;
338 }
339 RS485PortEvent e = new RS485PortEvent(this, event, !state, state );
340 if( SPEventListener != null ) SPEventListener.RS485Event( e );
341 }
342
343 /** Add an event listener */
344 public void addEventListener( RS485PortEventListener lsnr )
345 throws TooManyListenersException
346 {
347 if( SPEventListener != null ) throw new TooManyListenersException();
348 SPEventListener = lsnr;
349 monThread = new MonitorThread();
350 monThread.start();
351 }
352 /** Remove the RS485 port event listener */
353 public void removeEventListener() {
354 SPEventListener = null;
355 if( monThread != null ) {
356 monThread.interrupt();
357 monThread = null;
358 }
359 }
360
361 public void notifyOnDataAvailable( boolean enable ) { monThread.Data = enable; }
362
363 public void notifyOnOutputEmpty( boolean enable ) { monThread.Output = enable; }
364
365 public void notifyOnCTS( boolean enable ) { monThread.CTS = enable; }
366 public void notifyOnDSR( boolean enable ) { monThread.DSR = enable; }
367 public void notifyOnRingIndicator( boolean enable ) { monThread.RI = enable; }
368 public void notifyOnCarrierDetect( boolean enable ) { monThread.CD = enable; }
369 public void notifyOnOverrunError( boolean enable ) { monThread.OE = enable; }
370 public void notifyOnParityError( boolean enable ) { monThread.PE = enable; }
371 public void notifyOnFramingError( boolean enable ) { monThread.FE = enable; }
372 public void notifyOnBreakInterrupt( boolean enable ) { monThread.BI = enable; }
373
374
375 /** Close the port */
376 private native void nativeClose();
377 public void close() {
378 setDTR(false);
379 setDSR(false);
380 nativeClose();
381 super.close();
382 fd = 0;
383 }
384
385
386 /** Finalize the port */
387 protected void finalize() {
388 if( fd > 0 ) close();
389 }
390
391
392 /** Inner class for RS485OutputStream */
393 class RS485OutputStream extends OutputStream {
394 public void write( int b ) throws IOException {
395 writeByte( b );
396 }
397 public void write( byte b[] ) throws IOException {
398 writeArray( b, 0, b.length );
399 }
400 public void write( byte b[], int off, int len ) throws IOException {
401 writeArray( b, off, len );
402 }
403 public void flush() throws IOException {
404 drain();
405 }
406 }
407
408 /** Inner class for RS485InputStream */
409 class RS485InputStream extends InputStream {
410 public int read() throws IOException {
411 dataAvailable=0;
412 return readByte();
413 }
414 public int read( byte b[] ) throws IOException
415 {
416 return read ( b, 0, b.length);
417 }
418 public int read( byte b[], int off, int len ) throws IOException
419 {
420 dataAvailable=0;
421 int i=0, Minimum=0;
422 int intArray[] =
423 {
424 b.length,
425 InputBuffer,
426 len
427 };
428 /*
429 find the lowest nonzero value
430 timeout and threshold are handled on the native side
431 see NativeEnableReceiveTimeoutThreshold in
432 RS485Imp.c
433 */
434 while(intArray[i]==0 && i < intArray.length) i++;
435 Minimum=intArray[i];
436 while( i < intArray.length )
437 {
438 if(intArray[i] > 0 )
439 {
440 Minimum=Math.min(Minimum,intArray[i]);
441 }
442 i++;
443 }
444 Minimum=Math.min(Minimum,threshold);
445 if(Minimum == 0) Minimum=1;
446 int Available=available();
447 int Ret = readArray( b, off, Minimum);
448 return Ret;
449 }
450 public int available() throws IOException {
451 return nativeavailable();
452 }
453 }
454 class MonitorThread extends Thread {
455 /** Note: these have to be separate boolean flags because the
456 RS485PortEvent constants are NOT bit-flags, they are just
457 defined as integers from 1 to 10 -DPL */
458 private boolean CTS=false;
459 private boolean DSR=false;
460 private boolean RI=false;
461 private boolean CD=false;
462 private boolean OE=false;
463 private boolean PE=false;
464 private boolean FE=false;
465 private boolean BI=false;
466 private boolean Data=false;
467 private boolean Output=false;
•468 MonitorThread() {
469 setDaemon(true);
470 }
471 public void run() {
472 eventLoop();
473 }
474 }
475 }