Debian Codegraph @

src/gnu/io/RXTXPort.java

raw symbols ◀ ▶ SCIP
2 Debian patches 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-2008 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.InputStream;
60 import java.io.OutputStream;
61 import java.io.IOException;
62 import java.util.TooManyListenersException;
63 import java.lang.Math;
64
65 /**
66 * An extension of gnu.io.SerialPort
67 * @see gnu.io.SerialPort
68 */
69
70 final public class RXTXPort extends SerialPort
71 {
72 /* I had a report that some JRE's complain when MonitorThread
73 tries to access private variables
74 */
75
76 protected final static boolean debug = false;
77 protected final static boolean debug_read = false;
78 protected final static boolean debug_read_results = false;
79 protected final static boolean debug_write = false;
80 protected final static boolean debug_events = false;
81 protected final static boolean debug_verbose = false;
82
83 private static Zystem z;
84
85 static
86 {
87 try {
88 z = new Zystem();
•89 } catch ( Exception e ) {
90 throw new Error(e.toString());
91 }
92
93 if(debug )
94 z.reportln( "RXTXPort {}");
95 System.loadLibrary( "rxtxSerial" );
96 Initialize();
97 }
98
99 /** Initialize the native library */
100 private native static void Initialize();
101 boolean MonitorThreadAlive=false;
102
103 /**
104 * Open the named port
105 * @param name the name of the device to open
106 * @throws PortInUseException
107 * @see gnu.io.SerialPort
108 */
109 public RXTXPort( String name ) throws PortInUseException
110 {
111 if (debug)
112 z.reportln( "RXTXPort:RXTXPort("+name+") called");
113 /*
114 commapi/javadocs/API_users_guide.html specifies that whenever
115 an application tries to open a port in use by another application
116 the PortInUseException will be thrown
117
118 I know some didnt like it this way but I'm not sure how to avoid
119 it. We will just be writing to a bogus fd if we catch the
120 exeption
121
122 Trent
123 */
124 // try {
125 fd = open( name );
126 this.name = name;
127
128 MonitorThreadLock = true;
129 monThread = new MonitorThread();
130 monThread.start();
131 waitForTheNativeCodeSilly();
132 MonitorThreadAlive=true;
133 // } catch ( PortInUseException e ){}
134 timeout = -1; /* default disabled timeout */
135 if (debug)
136 z.reportln( "RXTXPort:RXTXPort("+name+") returns with fd = " +
137 fd);
138 }
139 private native synchronized int open( String name )
140 throws PortInUseException;
141
142
143 /* dont close the file while accessing the fd */
144 int IOLocked = 0;
145 Object IOLockedMutex = new Object();
146
147 /** File descriptor */
148 private int fd = 0;
149
150 /** a pointer to the event info structure used to share information
151 between threads so write threads can send output buffer empty
152 from a pthread if need be.
153
154 long for 64 bit pointers.
155 */
156 long eis = 0;
157 /** pid for lock files */
158 int pid = 0;
159
160 /** DSR flag **/
161 static boolean dsrFlag = false;
162
163 /** Output stream */
164 private final SerialOutputStream out = new SerialOutputStream();
165 /**
166 * get the OutputStream
167 * @return OutputStream
168 */
169 public OutputStream getOutputStream()
170 {
171 if (debug)
172 z.reportln( "RXTXPort:getOutputStream() called and returning");
173 return out;
174 }
175
176 /** Input stream */
177 private final SerialInputStream in = new SerialInputStream();
178 /**
179 * get the InputStream
180 * @return InputStream
181 * @see java.io.InputStream
182 */
183 public InputStream getInputStream()
184 {
185 if (debug)
186 z.reportln( "RXTXPort:getInputStream() called and returning");
187 return in;
188 }
189
190 /**
191 * Set the SerialPort parameters
192 * 1.5 stop bits requires 5 databits
193 * @param b baudrate
194 * @param d databits
195 * @param s stopbits
196 * @param p parity
197 * @throws UnsupportedCommOperationException
198 * @see gnu.io.UnsupportedCommOperationException
199
200 * If speed is not a predifined speed it is assumed to be
201 * the actual speed desired.
202 */
203 private native int nativeGetParity( int fd );
204 private native int nativeGetFlowControlMode( int fd );
205 public synchronized void setSerialPortParams( int b, int d, int s,
206 int p )
207 throws UnsupportedCommOperationException
208 {
209 if (debug)
210 z.reportln( "RXTXPort:setSerialPortParams(" +
211 b + " " + d + " " + s + " " + p + ") called");
212 if ( nativeSetSerialPortParams( b, d, s, p ) )
213 throw new UnsupportedCommOperationException(
214 "Invalid Parameter" );
215 speed = b;
216 if( s== STOPBITS_1_5 ) dataBits = DATABITS_5;
217 else dataBits = d;
218 stopBits = s;
219 parity = p;
220 z.reportln( "RXTXPort:setSerialPortParams(" +
221 b + " " + d + " " + s + " " + p +
222 ") returning");
223 }
224
225 /**
226 * Set the native serial port parameters
227 * If speed is not a predifined speed it is assumed to be
228 * the actual speed desired.
229 */
230 private native boolean nativeSetSerialPortParams( int speed,
231 int dataBits, int stopBits, int parity )
232 throws UnsupportedCommOperationException;
233
234 /** Line speed in bits-per-second */
235 private int speed=9600;
236 /**
237 * @return int representing the baudrate
238 * This will not behave as expected with custom speeds
239 */
240 public int getBaudRate()
241 {
242 if (debug)
243 z.reportln( "RXTXPort:getBaudRate() called and returning " + speed);
244 return speed;
245 }
246
247 /** Data bits port parameter */
248 private int dataBits=DATABITS_8;
249 /**
250 * @return int representing the databits
251 */
252 public int getDataBits()
253 {
254 if (debug)
255 z.reportln( "RXTXPort:getDataBits() called and returning " + dataBits);
256 return dataBits;
257 }
258
259 /** Stop bits port parameter */
260 private int stopBits=SerialPort.STOPBITS_1;
261 /**
262 * @return int representing the stopbits
263 */
264 public int getStopBits()
265 {
266 if (debug)
267 z.reportln( "RXTXPort:getStopBits() called and returning " + stopBits);
268 return stopBits;
269 }
270
271 /** Parity port parameter */
272 private int parity= SerialPort.PARITY_NONE;
273 /**
274 * @return int representing the parity
275 */
276 public int getParity()
277 {
278 if (debug)
279 z.reportln( "RXTXPort:getParity() called and returning " + parity );
280 return parity;
281 }
282
283
284 /** Flow control */
285 private int flowmode = SerialPort.FLOWCONTROL_NONE;
286 /**
287 * @param flowcontrol FLOWCONTROL_NONE is default
288 * @see gnu.io.SerialPort#FLOWCONTROL_NONE
289 */
290 public void setFlowControlMode( int flowcontrol )
291 {
292 if (debug)
293 z.reportln( "RXTXPort:setFlowControlMode( " + flowcontrol + " ) called");
294 if(monThreadisInterrupted)
295 {
296 if( debug_events )
297 z.reportln( "RXTXPort:setFlowControlMode MonThread is Interrupeted returning" );
298 return;
299 }
300 try {
301 setflowcontrol( flowcontrol );
302 }
303 catch( IOException e )
304 {
305 e.printStackTrace();
306 return;
307 }
308 flowmode=flowcontrol;
309 if (debug)
310 z.reportln( "RXTXPort:setFlowControlMode( " + flowcontrol + " ) returning");
311 }
312 /**
313 * @return int representing the flowmode
314 */
315 public int getFlowControlMode()
316 {
317 if (debug)
318 z.reportln( "RXTXPort:getFlowControlMode() returning " + flowmode );
319 return flowmode;
320 }
321 native void setflowcontrol( int flowcontrol ) throws IOException;
322
323
324 /*
325 linux/drivers/char/n_hdlc.c? FIXME
326 [email protected]
327 */
328 /**
329 * Receive framing control
330 * @param f framming
331 * @throws UnsupportedCommOperationException
332 */
333 public void enableReceiveFraming( int f )
334 throws UnsupportedCommOperationException
335 {
336 if (debug)
337 z.reportln( "RXTXPort:enableReceiveFramming() throwing exception");
338 throw new UnsupportedCommOperationException( "Not supported" );
339 }
340 /**
341 */
342 public void disableReceiveFraming()
343 {
344 if (debug)
345 z.reportln( "RXTXPort:disableReceiveFramming() called and returning (noop)");
346 }
347 /**
348 * @return true if framing is enabled
349 */
350 public boolean isReceiveFramingEnabled()
351 {
352 if (debug)
353 z.reportln( "RXTXPort:isReceiveFrammingEnabled() called and returning " + false );
354 return false;
355 }
356 /**
357 * @return int representing the framing byte
358 */
359 public int getReceiveFramingByte()
360 {
361 if (debug)
362 z.reportln( "RXTXPort:getReceiveFrammingByte() called and returning " + 0 );
363 return 0;
364 }
365
366
367 /** Receive timeout control */
368 private int timeout;
369
370 /**
371 * @return int the timeout
372 */
373 public native int NativegetReceiveTimeout();
374 /**
375 * @return bloolean true if recieve timeout is enabled
376 */
377 private native boolean NativeisReceiveTimeoutEnabled();
378 /**
379 * @param time
380 * @param threshold
381 * @param InputBuffer
382 */
383 private native void NativeEnableReceiveTimeoutThreshold(int time,
384 int threshold,int InputBuffer);
385 /**
386 */
387 public void disableReceiveTimeout()
388 {
389 if (debug)
390 z.reportln( "RXTXPort:disableReceiveTimeout() called");
391 timeout = -1;
392 NativeEnableReceiveTimeoutThreshold( timeout , threshold, InputBuffer );
393 if (debug)
394 z.reportln( "RXTXPort:disableReceiveTimeout() returning");
395 }
396 /**
397 * @param time
398 */
399 public void enableReceiveTimeout( int time )
400 {
401 if (debug)
402 z.reportln( "RXTXPort:enableReceiveTimeout() called");
403 if( time >= 0 )
404 {
405 timeout = time;
406 NativeEnableReceiveTimeoutThreshold( time , threshold,
407 InputBuffer );
408 }
409 else
410 {
411 throw new IllegalArgumentException
412 (
413 "Unexpected negative timeout value"
414 );
415 }
416 if (debug)
417 z.reportln( "RXTXPort:enableReceiveTimeout() returning");
418 }
419 /**
420 * @return boolean true if recieve timeout is enabled
421 */
422 public boolean isReceiveTimeoutEnabled()
423 {
424 if (debug)
425 z.reportln( "RXTXPort:isReceiveTimeoutEnabled() called and returning " + NativeisReceiveTimeoutEnabled() );
426 return( NativeisReceiveTimeoutEnabled() );
427 }
428 /**
429 * @return int the timeout
430 */
431 public int getReceiveTimeout()
432 {
433 if (debug)
434 z.reportln( "RXTXPort:getReceiveTimeout() called and returning " + NativegetReceiveTimeout() );
435 return(NativegetReceiveTimeout( ));
436 }
437
438 /** Receive threshold control */
439
440 private int threshold = 0;
441
442 /**
443 * @param thresh threshold
444 */
445 public void enableReceiveThreshold( int thresh )
446 {
447 if (debug)
448 z.reportln( "RXTXPort:enableReceiveThreshold( " + thresh + " ) called");
449 if(thresh >=0)
450 {
451 threshold=thresh;
452 NativeEnableReceiveTimeoutThreshold(timeout, threshold,
453 InputBuffer);
454 }
455 else /* invalid thresh */
456 {
457 throw new IllegalArgumentException
458 (
459 "Unexpected negative threshold value"
460 );
461 }
462 if (debug)
463 z.reportln( "RXTXPort:enableReceiveThreshold( " + thresh + " ) returned");
464 }
465 /**
466 */
467 public void disableReceiveThreshold()
468 {
469 if (debug)
470 z.reportln( "RXTXPort:disableReceiveThreshold() called and returning");
471 enableReceiveThreshold(0);
472 }
473 /**
474 * @return int the recieve threshold
475 */
476 public int getReceiveThreshold()
477 {
478 if (debug)
479 z.reportln( "RXTXPort:getReceiveThreshold() called and returning " + threshold);
480 return threshold;
481 }
482 /**
483 * @return boolean true if receive threshold is enabled
484 */
485 public boolean isReceiveThresholdEnabled()
486 {
487 if (debug)
488 z.reportln( "RXTXPort:isReceiveThresholdEnable() called and returning" + (threshold > 0) );
489 return(threshold>0);
490 }
491
492 /** Input/output buffers */
493 /** FIXME I think this refers to
494 FOPEN(3)/SETBUF(3)/FREAD(3)/FCLOSE(3)
495 [email protected]
496
497 These are native stubs...
498 */
499 private int InputBuffer=0;
500 private int OutputBuffer=0;
501 /**
502 * @param size
503 */
504 public void setInputBufferSize( int size )
505 {
506 if (debug)
507 z.reportln( "RXTXPort:setInputBufferSize( " +
508 size + ") called");
509 if( size < 0 )
510 throw new IllegalArgumentException
511 (
512 "Unexpected negative buffer size value"
513 );
514 else InputBuffer=size;
515 if (debug)
516 z.reportln( "RXTXPort:setInputBufferSize( " +
517 size + ") returning");
518 }
519 /**
520 */
521 public int getInputBufferSize()
522 {
523 if (debug)
524 z.reportln( "RXTXPort:getInputBufferSize() called and returning " + InputBuffer );
525 return(InputBuffer);
526 }
527 /**
528 * @param size
529 */
530 public void setOutputBufferSize( int size )
531 {
532 if (debug)
533 z.reportln( "RXTXPort:setOutputBufferSize( " +
534 size + ") called");
535 if( size < 0 )
536 throw new IllegalArgumentException
537 (
538 "Unexpected negative buffer size value"
539 );
540 else OutputBuffer=size;
541 if (debug)
542 z.reportln( "RXTXPort:setOutputBufferSize( " +
543 size + ") returned");
544
545 }
546 /**
547 * @return in the output buffer size
548 */
549 public int getOutputBufferSize()
550 {
551 if (debug)
552 z.reportln( "RXTXPort:getOutputBufferSize() called and returning " + OutputBuffer );
553 return(OutputBuffer);
554 }
555
556 /* =================== cleaned messages to here */
557
558 /**
559 * Line status methods
560 */
561 /**
562 * @return true if DTR is set
563 */
564 public native boolean isDTR();
565 /**
566 * @param state
567 */
568 public native void setDTR( boolean state );
569 /**
570 * @param state
571 */
572 public native void setRTS( boolean state );
573 private native void setDSR( boolean state );
574 /**
575 * @return boolean true if CTS is set
576 */
577 public native boolean isCTS();
578 /**
579 * @return boolean true if DSR is set
580 */
581 public native boolean isDSR();
582 /**
583 * @return boolean true if CD is set
584 */
585 public native boolean isCD();
586 /**
587 * @return boolean true if RI is set
588 */
589 public native boolean isRI();
590 /**
591 * @return boolean true if RTS is set
592 */
593 public native boolean isRTS();
594
595
596 /**
597 * Write to the port
598 * @param duration
599 */
600 public native void sendBreak( int duration );
601 protected native void writeByte( int b, boolean i ) throws IOException;
602 protected native void writeArray( byte b[], int off, int len, boolean i )
603 throws IOException;
604 protected native boolean nativeDrain( boolean i ) throws IOException;
605
606 /** RXTXPort read methods */
607 protected native int nativeavailable() throws IOException;
608 protected native int readByte() throws IOException;
609 protected native int readArray( byte b[], int off, int len )
610 throws IOException;
611 protected native int readTerminatedArray( byte b[], int off, int len, byte t[] )
612 throws IOException;
613
614
615 /** Serial Port Event listener */
616 private SerialPortEventListener SPEventListener;
617
618 /** Thread to monitor data */
619 private MonitorThread monThread;
620
621 /** Process SerialPortEvents */
622 native void eventLoop();
623
624 /**
625 * @return boolean true if monitor thread is interrupted
626 */
627 boolean monThreadisInterrupted=true;
628 private native void interruptEventLoop( );
629 public boolean checkMonitorThread()
630 {
631 if (debug)
632 z.reportln( "RXTXPort:checkMonitorThread()");
633 if(monThread != null)
634 {
635 if ( debug )
636 z.reportln(
637 "monThreadisInterrupted = " +
638 monThreadisInterrupted );
639 return monThreadisInterrupted;
640 }
641 if ( debug )
642 z.reportln( "monThread is null " );
643 return(true);
644 }
645
646 /**
647 * @param event
648 * @param state
649 * @return boolean true if the port is closing
650 */
651 public boolean sendEvent( int event, boolean state )
652 {
653 if (debug_events)
654 z.report( "RXTXPort:sendEvent(");
655 /* Let the native side know its time to die */
656
657 if ( fd == 0 || SPEventListener == null || monThread == null)
658 {
659 return(true);
660 }
661
662 switch( event )
663 {
664 case SerialPortEvent.DATA_AVAILABLE:
665 if( debug_events )
666 z.reportln( "DATA_AVAILABLE " +
667 monThread.Data + ")" );
668 break;
669 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
670 if( debug_events )
671 z.reportln(
672 "OUTPUT_BUFFER_EMPTY " +
673 monThread.Output + ")" );
674 break;
675 case SerialPortEvent.CTS:
676 if( debug_events )
677 z.reportln( "CTS " +
678 monThread.CTS + ")" );
679 break;
680 case SerialPortEvent.DSR:
681 if( debug_events )
682 z.reportln( "DSR " +
683 monThread.Output + ")" );
684 break;
685 case SerialPortEvent.RI:
686 if( debug_events )
687 z.reportln( "RI " +
688 monThread.RI + ")" );
689 break;
690 case SerialPortEvent.CD:
691 if( debug_events )
692 z.reportln( "CD " +
693 monThread.CD + ")" );
694 break;
695 case SerialPortEvent.OE:
696 if( debug_events )
697 z.reportln( "OE " +
698 monThread.OE + ")" );
699 break;
700 case SerialPortEvent.PE:
701 if( debug_events )
702 z.reportln( "PE " +
703 monThread.PE + ")" );
704 break;
705 case SerialPortEvent.FE:
706 if( debug_events )
707 z.reportln( "FE " +
708 monThread.FE + ")" );
709 break;
710 case SerialPortEvent.BI:
711 if( debug_events )
712 z.reportln( "BI " +
713 monThread.BI + ")" );
714 break;
715 default:
716 if( debug_events )
717 z.reportln( "XXXXXXXXXXXXXX " +
718 event + ")" );
719 break;
720 }
721 if( debug_events && debug_verbose )
722 z.reportln( " checking flags " );
723
724 switch( event )
725 {
726 case SerialPortEvent.DATA_AVAILABLE:
727 if( monThread.Data ) break;
728 return(false);
729 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
730 if( monThread.Output ) break;
731 return(false);
732 case SerialPortEvent.CTS:
733 if( monThread.CTS ) break;
734 return(false);
735 case SerialPortEvent.DSR:
736 if( monThread.DSR ) break;
737 return(false);
738 case SerialPortEvent.RI:
739 if( monThread.RI ) break;
740 return(false);
741 case SerialPortEvent.CD:
742 if( monThread.CD ) break;
743 return(false);
744 case SerialPortEvent.OE:
745 if( monThread.OE ) break;
746 return(false);
747 case SerialPortEvent.PE:
748 if( monThread.PE ) break;
749 return(false);
750 case SerialPortEvent.FE:
751 if( monThread.FE ) break;
752 return(false);
753 case SerialPortEvent.BI:
754 if( monThread.BI ) break;
755 return(false);
756 default:
757 System.err.println( "unknown event: " + event);
758 return(false);
759 }
760 if( debug_events && debug_verbose )
761 z.reportln( " getting event" );
762 SerialPortEvent e = new SerialPortEvent(this, event, !state,
763 state );
764 if( debug_events && debug_verbose )
765 z.reportln( " sending event" );
766 if(monThreadisInterrupted)
767 {
768 if( debug_events )
769 z.reportln( " sendEvent return" );
770 return(true);
771 }
772 if( SPEventListener != null )
773 {
774 SPEventListener.serialEvent( e );
775 }
776
777 if( debug_events && debug_verbose )
778 z.reportln( " sendEvent return" );
779
780 if (fd == 0 || SPEventListener == null || monThread == null)
781 {
782 return(true);
783 }
784 else
785 {
786 return(false);
787 }
788 }
789
790 /**
791 * Add an event listener
792 * @param lsnr SerialPortEventListener
793 * @throws TooManyListenersException
794 */
795
796 boolean MonitorThreadLock = true;
797
798 public void addEventListener(
799 SerialPortEventListener lsnr ) throws TooManyListenersException
800 {
801 /* Don't let and notification requests happen until the
802 Eventloop is ready
803 */
804
805 if (debug)
806 z.reportln( "RXTXPort:addEventListener()");
807 if( SPEventListener != null )
808 {
809 throw new TooManyListenersException();
810 }
811 SPEventListener = lsnr;
812 if( !MonitorThreadAlive )
813 {
814 MonitorThreadLock = true;
815 monThread = new MonitorThread();
816 monThread.start();
817 waitForTheNativeCodeSilly();
818 MonitorThreadAlive=true;
819 }
820 if (debug)
821 z.reportln( "RXTXPort:Interrupt=false");
822 }
823 /**
824 * Remove the serial port event listener
825 */
826 public void removeEventListener()
827 {
828 if (debug)
829 z.reportln( "RXTXPort:removeEventListener() called");
830 waitForTheNativeCodeSilly();
831 //if( monThread != null && monThread.isAlive() )
832 if( monThreadisInterrupted == true )
833 {
834 z.reportln( " RXTXPort:removeEventListener() already interrupted");
835 monThread = null;
836 SPEventListener = null;
837 return;
838 }
839 else if( monThread != null && monThread.isAlive() )
840 {
841 if (debug)
842 z.reportln( " RXTXPort:Interrupt=true");
843 monThreadisInterrupted=true;
844 /*
845 Notify all threads in this PID that something is up
846 They will call back to see if its their thread
847 using isInterrupted().
848 */
849 if (debug)
850 z.reportln( " RXTXPort:calling interruptEventLoop");
851 interruptEventLoop( );
852
853 if (debug)
854 z.reportln( " RXTXPort:calling monThread.join()");
855 try {
856
857 // wait a reasonable moment for the death of the monitor thread
858 monThread.join(3000);
859 } catch (InterruptedException ex) {
860 // somebody called interrupt() on us (ie wants us to abort)
861 // we dont propagate InterruptedExceptions so lets re-set the flag
862 Thread.currentThread().interrupt();
863 return;
864 }
865
866 if ( debug && monThread.isAlive() )
867 {
868 z.reportln( " MonThread is still alive!");
869
870 }
871
872 }
873 monThread = null;
874 SPEventListener = null;
875 MonitorThreadLock = false;
876 MonitorThreadAlive=false;
877 monThreadisInterrupted=true;
878 z.reportln( "RXTXPort:removeEventListener() returning");
879 }
880 /**
881 * Give the native code a chance to start listening to the hardware
882 * or should we say give the native code control of the issue.
883 *
884 * This is important for applications that flicker the Monitor
885 * thread while keeping the port open.
886 * In worst case test cases this loops once or twice every time.
887 */
888
889 protected void waitForTheNativeCodeSilly()
890 {
891 while( MonitorThreadLock )
892 {
893 try {
894 Thread.sleep(5);
895 } catch( Exception e ) {}
896 }
897 }
898 /**
899 * @param enable
900 */
901 private native void nativeSetEventFlag( int fd, int event,
902 boolean flag );
903 public void notifyOnDataAvailable( boolean enable )
904 {
905 if (debug)
906 z.reportln( "RXTXPort:notifyOnDataAvailable( " +
907 enable+" )");
908
909 waitForTheNativeCodeSilly();
910
911 MonitorThreadLock = true;
912 nativeSetEventFlag( fd, SerialPortEvent.DATA_AVAILABLE,
913 enable );
914 monThread.Data = enable;
915 MonitorThreadLock = false;
916 }
917
918 /**
919 * @param enable
920 */
921 public void notifyOnOutputEmpty( boolean enable )
922 {
923 if (debug)
924 z.reportln( "RXTXPort:notifyOnOutputEmpty( " +
925 enable+" )");
926 waitForTheNativeCodeSilly();
927 MonitorThreadLock = true;
928 nativeSetEventFlag( fd, SerialPortEvent.OUTPUT_BUFFER_EMPTY,
929 enable );
930 monThread.Output = enable;
931 MonitorThreadLock = false;
932 }
933
934 /**
935 * @param enable
936 */
937 public void notifyOnCTS( boolean enable )
938 {
939 if (debug)
940 z.reportln( "RXTXPort:notifyOnCTS( " +
941 enable+" )");
942 waitForTheNativeCodeSilly();
943 MonitorThreadLock = true;
944 nativeSetEventFlag( fd, SerialPortEvent.CTS, enable );
945 monThread.CTS = enable;
946 MonitorThreadLock = false;
947 }
948 /**
949 * @param enable
950 */
951 public void notifyOnDSR( boolean enable )
952 {
953 if (debug)
954 z.reportln( "RXTXPort:notifyOnDSR( " +
955 enable+" )");
956 waitForTheNativeCodeSilly();
957 MonitorThreadLock = true;
958 nativeSetEventFlag( fd, SerialPortEvent.DSR, enable );
959 monThread.DSR = enable;
960 MonitorThreadLock = false;
961 }
962 /**
963 * @param enable
964 */
965 public void notifyOnRingIndicator( boolean enable )
966 {
967 if (debug)
968 z.reportln( "RXTXPort:notifyOnRingIndicator( " +
969 enable+" )");
970 waitForTheNativeCodeSilly();
971 MonitorThreadLock = true;
972 nativeSetEventFlag( fd, SerialPortEvent.RI, enable );
973 monThread.RI = enable;
974 MonitorThreadLock = false;
975 }
976 /**
977 * @param enable
978 */
979 public void notifyOnCarrierDetect( boolean enable )
980 {
981 if (debug)
982 z.reportln( "RXTXPort:notifyOnCarrierDetect( " +
983 enable+" )");
984 waitForTheNativeCodeSilly();
985 MonitorThreadLock = true;
986 nativeSetEventFlag( fd, SerialPortEvent.CD, enable );
987 monThread.CD = enable;
988 MonitorThreadLock = false;
989 }
990 /**
991 * @param enable
992 */
993 public void notifyOnOverrunError( boolean enable )
994 {
995 if (debug)
996 z.reportln( "RXTXPort:notifyOnOverrunError( " +
997 enable+" )");
998 waitForTheNativeCodeSilly();
999 MonitorThreadLock = true;
1000 nativeSetEventFlag( fd, SerialPortEvent.OE, enable );
1001 monThread.OE = enable;
1002 MonitorThreadLock = false;
1003 }
1004 /**
1005 * @param enable
1006 */
1007 public void notifyOnParityError( boolean enable )
1008 {
1009 if (debug)
1010 z.reportln( "RXTXPort:notifyOnParityError( " +
1011 enable+" )");
1012 waitForTheNativeCodeSilly();
1013 MonitorThreadLock = true;
1014 nativeSetEventFlag( fd, SerialPortEvent.PE, enable );
1015 monThread.PE = enable;
1016 MonitorThreadLock = false;
1017 }
1018 /**
1019 * @param enable
1020 */
1021 public void notifyOnFramingError( boolean enable )
1022 {
1023 if (debug)
1024 z.reportln( "RXTXPort:notifyOnFramingError( " +
1025 enable+" )");
1026 waitForTheNativeCodeSilly();
1027 MonitorThreadLock = true;
1028 nativeSetEventFlag( fd, SerialPortEvent.FE, enable );
1029 monThread.FE = enable;
1030 MonitorThreadLock = false;
1031 }
1032 /**
1033 * @param enable
1034 */
1035 public void notifyOnBreakInterrupt( boolean enable )
1036 {
1037 if (debug)
1038 z.reportln( "RXTXPort:notifyOnBreakInterrupt( " +
1039 enable+" )");
1040 waitForTheNativeCodeSilly();
1041 MonitorThreadLock = true;
1042 nativeSetEventFlag( fd, SerialPortEvent.BI, enable );
1043 monThread.BI = enable;
1044 MonitorThreadLock = false;
1045 }
1046
1047 /** Close the port */
1048 private native void nativeClose( String name );
1049 /**
1050 */
1051 boolean closeLock = false;
1052 public void close()
1053 {
1054 synchronized (this) {
1055 if (debug)
1056 z.reportln( "RXTXPort:close( " + this.name + " )");
1057
1058 while( IOLocked > 0 )
1059 {
1060 if( debug )
1061 z.reportln("IO is locked " + IOLocked);
1062 try {
1063 this.wait(500);
1064 } catch( InterruptedException ie ) {
1065 // somebody called interrupt() on us
1066 // we obbey and return without without closing the socket
1067 Thread.currentThread().interrupt();
1068 return;
1069 }
1070 }
1071
1072 // we set the closeLock after the above check because we might
1073 // have returned without proceeding
1074 if( closeLock ) return;
1075 closeLock = true;
1076 }
1077
1078 if ( fd <= 0 )
1079 {
1080 z.reportln( "RXTXPort:close detected bad File Descriptor" );
1081 return;
1082 }
1083 setDTR(false);
1084 setDSR(false);
1085 if (debug)
1086 z.reportln( "RXTXPort:close( " + this.name + " ) setting monThreadisInterrupted");
1087 if ( ! monThreadisInterrupted )
1088 {
1089 removeEventListener();
1090 }
1091 if (debug)
1092 z.reportln( "RXTXPort:close( " + this.name + " ) calling nativeClose");
1093 nativeClose( this.name );
1094 if (debug)
1095 z.reportln( "RXTXPort:close( " + this.name + " ) calling super.close");
1096 super.close();
1097 fd = 0;
1098 closeLock = false;
1099 if (debug)
1100 z.reportln( "RXTXPort:close( " + this.name + " ) leaving");
1101 }
1102
1103
1104 /** Finalize the port */
1105 protected void finalize()
1106 {
1107 if (debug)
1108 z.reportln( "RXTXPort:finalize()");
1109 if( fd > 0 )
1110 {
1111 if (debug)
1112 z.reportln( "RXTXPort:calling close()");
1113 close();
1114 }
1115 z.finalize();
1116 }
1117
1118 /** Inner class for SerialOutputStream */
1119 class SerialOutputStream extends OutputStream
1120 {
1121 /**
1122 * @param b
1123 * @throws IOException
1124 */
1125 public void write( int b ) throws IOException
1126 {
1127 if (debug_write)
1128 z.reportln( "RXTXPort:SerialOutputStream:write(int)");
1129 if( speed == 0 ) return;
1130 if ( monThreadisInterrupted == true )
1131 {
1132 return;
1133 }
1134 synchronized (IOLockedMutex) {
1135 IOLocked++;
1136 }
1137 try {
1138 waitForTheNativeCodeSilly();
1139 if ( fd == 0 )
1140 {
1141 throw new IOException();
1142 }
1143 writeByte( b, monThreadisInterrupted );
1144 if (debug_write)
1145 z.reportln( "Leaving RXTXPort:SerialOutputStream:write( int )");
1146 } finally {
1147 synchronized (IOLockedMutex) {
1148 IOLocked--;
1149 }
1150 }
1151 }
1152 /**
1153 * @param b[]
1154 * @throws IOException
1155 */
1156 public void write( byte b[] ) throws IOException
1157 {
1158 if (debug_write)
1159 {
1160 z.reportln( "Entering RXTXPort:SerialOutputStream:write(" + b.length + ") "/* + new String(b)*/ );
1161 }
1162 if( speed == 0 ) return;
1163 if ( monThreadisInterrupted == true )
1164 {
1165 return;
1166 }
1167 if ( fd == 0 ) throw new IOException();
1168 synchronized (IOLockedMutex) {
1169 IOLocked++;
1170 }
1171 try {
1172 waitForTheNativeCodeSilly();
1173 writeArray( b, 0, b.length, monThreadisInterrupted );
1174 if (debug_write)
1175 z.reportln( "Leaving RXTXPort:SerialOutputStream:write(" +b.length +")");
1176 } finally {
1177 synchronized(IOLockedMutex) {
1178 IOLocked--;
1179 }
1180 }
1181
1182 }
1183 /**
1184 * @param b[]
1185 * @param off
1186 * @param len
1187 * @throws IOException
1188 */
1189 public void write( byte b[], int off, int len )
1190 throws IOException
1191 {
1192 if( speed == 0 ) return;
1193 if( off + len > b.length )
1194 {
1195 throw new IndexOutOfBoundsException(
1196 "Invalid offset/length passed to read"
1197 );
1198 }
1199
1200 byte send[] = new byte[len];
1201 System.arraycopy( b, off, send, 0, len );
1202 if (debug_write)
1203 {
1204 z.reportln( "Entering RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send) */ );
1205 }
1206 if ( fd == 0 ) throw new IOException();
1207 if ( monThreadisInterrupted == true )
1208 {
1209 return;
1210 }
1211 synchronized (IOLockedMutex) {
1212 IOLocked++;
1213 }
1214 try
1215 {
1216 waitForTheNativeCodeSilly();
1217 writeArray( send, 0, len, monThreadisInterrupted );
1218 if( debug_write )
1219 z.reportln( "Leaving RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send)*/ );
1220 } finally {
1221 synchronized (IOLockedMutex) {
1222 IOLocked--;
1223 }
1224 }
1225 }
1226 /**
1227 */
1228 public void flush() throws IOException
1229 {
1230 if (debug)
1231 z.reportln( "RXTXPort:SerialOutputStream:flush() enter");
1232 if( speed == 0 ) return;
1233 if ( fd == 0 ) throw new IOException();
1234 if ( monThreadisInterrupted == true )
1235 {
1236 if (debug)
1237 z.reportln( "RXTXPort:SerialOutputStream:flush() Leaving Interrupted");
1238 return;
1239 }
1240 synchronized(IOLockedMutex) {
1241 IOLocked++;
1242 }
1243 try
1244 {
1245 waitForTheNativeCodeSilly();
1246 /*
1247 this is probably good on all OS's but for now
1248 just sendEvent from java on Sol
1249 */
1250 if ( nativeDrain( monThreadisInterrupted ) )
1251 sendEvent( SerialPortEvent.OUTPUT_BUFFER_EMPTY, true );
1252 if (debug)
1253 z.reportln( "RXTXPort:SerialOutputStream:flush() leave");
1254 }
1255 finally
1256 {
1257 synchronized (IOLockedMutex) {
1258 IOLocked--;
1259 }
1260 }
1261 }
1262 }
1263
1264 /** Inner class for SerialInputStream */
1265 class SerialInputStream extends InputStream
1266 {
1267 /**
1268 * @return int the int read
1269 * @throws IOException
1270 * @see java.io.InputStream
1271 *
1272 *timeout threshold Behavior
1273 *------------------------------------------------------------------------
1274 *0 0 blocks until 1 byte is available timeout > 0,
1275 * threshold = 0, blocks until timeout occurs, returns -1
1276 * on timeout
1277 *>0 >0 blocks until timeout, returns - 1 on timeout, magnitude
1278 * of threshold doesn't play a role.
1279 *0 >0 Blocks until 1 byte, magnitude of threshold doesn't
1280 * play a role
1281 */
1282 public synchronized int read() throws IOException
1283 {
1284 if (debug_read)
1285 z.reportln( "RXTXPort:SerialInputStream:read() called");
1286 if ( fd == 0 ) throw new IOException();
1287 if ( monThreadisInterrupted )
1288 {
1289 z.reportln( "+++++++++ read() monThreadisInterrupted" );
1290 }
1291 synchronized (IOLockedMutex) {
1292 IOLocked++;
1293 }
1294 try {
1295 if (debug_read_results)
1296 z.reportln( "RXTXPort:SerialInputStream:read() L" );
1297 waitForTheNativeCodeSilly();
1298 if (debug_read_results)
1299 z.reportln( "RXTXPort:SerialInputStream:read() N" );
1300 int result = readByte();
1301 if (debug_read_results)
1302 //z.reportln( "RXTXPort:SerialInputStream:read() returns byte = " + result );
1303 z.reportln( "RXTXPort:SerialInputStream:read() returns" );
1304 return( result );
1305 }
1306 finally
1307 {
1308 synchronized (IOLockedMutex) {
1309 IOLocked--;
1310 }
1311 }
1312 }
1313 /**
1314 * @param b[]
1315 * @return int number of bytes read
1316 * @throws IOException
1317 *
1318 *timeout threshold Behavior
1319 *------------------------------------------------------------------------
1320 *0 0 blocks until 1 byte is available
1321 *>0 0 blocks until timeout occurs, returns 0 on timeout
1322 *>0 >0 blocks until timeout or reads threshold bytes,
1323 returns 0 on timeout
1324 *0 >0 blocks until reads threshold bytes
1325 */
1326 public synchronized int read( byte b[] ) throws IOException
1327 {
1328 int result;
1329 if (debug_read)
1330 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + ") called");
1331 if ( monThreadisInterrupted == true )
1332 {
1333 return(0);
1334 }
1335 synchronized (IOLockedMutex) {
1336 IOLocked++;
1337 }
1338 try
1339 {
1340 waitForTheNativeCodeSilly();
1341 result = read( b, 0, b.length);
1342 if (debug_read_results)
1343 z.reportln( "RXTXPort:SerialInputStream:read() returned " + result + " bytes" );
1344 return( result );
1345 }
1346 finally
1347 {
1348 synchronized (IOLockedMutex) {
1349 IOLocked--;
1350 }
1351 }
1352 }
1353 /*
1354 read(byte b[], int, int)
1355 Documentation is at http://java.sun.com/products/jdk/1.2/docs/api/java/io/InputStream.html#read(byte[], int, int)
1356 */
1357 /**
1358 * @param b[]
1359 * @param off
1360 * @param len
1361 * @return int number of bytes read
1362 * @throws IOException
1363 *
1364 *timeout threshold Behavior
1365 *------------------------------------------------------------------------
1366 *0 0 blocks until 1 byte is available
1367 *>0 0 blocks until timeout occurs, returns 0 on timeout
1368 *>0 >0 blocks until timeout or reads threshold bytes,
1369 returns 0 on timeout
1370 *0 >0 blocks until either threshold # of bytes or len bytes,
1371 whichever was lower.
1372 */
1373 public synchronized int read( byte b[], int off, int len )
1374 throws IOException
1375 {
1376 if (debug_read)
1377 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") called" /*+ new String(b) */ );
1378 int result;
1379 /*
1380 * Some sanity checks
1381 */
1382 if ( fd == 0 )
1383 {
1384 if (debug_read)
1385 z.reportln( "RXTXPort:SerialInputStream:read() fd == 0");
1386 z.reportln("+++++++ IOException()\n");
1387 throw new IOException();
1388 }
1389
1390 if( b==null )
1391 {
1392 z.reportln("+++++++ NullPointerException()\n");
1393 if (debug_read)
1394 z.reportln( "RXTXPort:SerialInputStream:read() b == 0");
1395 throw new NullPointerException();
1396 }
1397
1398 if( (off < 0) || (len < 0) || (off+len > b.length))
1399 {
1400 z.reportln("+++++++ IndexOutOfBoundsException()\n");
1401 if (debug_read)
1402 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1403 throw new IndexOutOfBoundsException();
1404 }
1405
1406 /*
1407 * Return immediately if len==0
1408 */
1409 if( len==0 )
1410 {
1411 if (debug_read)
1412 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1413 return 0;
1414 }
1415 /*
1416 * See how many bytes we should read
1417 */
1418 int Minimum = len;
1419
1420 if( threshold==0 )
1421 {
1422 /*
1423 * If threshold is disabled, read should return as soon
1424 * as data are available (up to the amount of available
1425 * bytes in order to avoid blocking)
1426 * Read may return earlier depending of the receive time
1427 * out.
1428 */
1429 int a = nativeavailable();
1430 if( a == 0 )
1431 Minimum = 1;
1432 else
1433 Minimum = Math.min( Minimum, a );
1434 }
1435 else
1436 {
1437 /*
1438 * Threshold is enabled. Read should return when
1439 * 'threshold' bytes have been received (or when the
1440 * receive timeout expired)
1441 */
1442 Minimum = Math.min(Minimum, threshold);
1443 }
1444 if ( monThreadisInterrupted == true )
1445 {
1446 if (debug_read)
1447 z.reportln( "RXTXPort:SerialInputStream:read() Interrupted");
1448 return(0);
1449 }
1450 synchronized (IOLockedMutex) {
1451 IOLocked++;
1452 }
1453 try
1454 {
1455 waitForTheNativeCodeSilly();
1456 result = readArray( b, off, Minimum);
1457 if (debug_read_results)
1458 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
1459 return( result );
1460 }
1461 finally
1462 {
1463 synchronized (IOLockedMutex) {
1464 IOLocked--;
1465 }
1466 }
1467 }
1468
1469 /**
1470 * @param b[]
1471 * @param off
1472 * @param len
1473 * @param t[]
1474 * @return int number of bytes read
1475 * @throws IOException
1476
1477 We are trying to catch the terminator in the native code
1478 Right now it is assumed that t[] is an array of 2 bytes.
1479
1480 if the read encounters the two bytes, it will return and the
1481 array will contain the terminator. Otherwise read behavior should
1482 be the same as read( b[], off, len ). Timeouts have not been well
1483 tested.
1484 */
1485
1486 public synchronized int read( byte b[], int off, int len, byte t[] )
1487 throws IOException
1488 {
1489 if (debug_read)
1490 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") called" /*+ new String(b) */ );
1491 int result;
1492 /*
1493 * Some sanity checks
1494 */
1495 if ( fd == 0 )
1496 {
1497 if (debug_read)
1498 z.reportln( "RXTXPort:SerialInputStream:read() fd == 0");
1499 z.reportln("+++++++ IOException()\n");
1500 throw new IOException();
1501 }
1502
1503 if( b==null )
1504 {
1505 z.reportln("+++++++ NullPointerException()\n");
1506 if (debug_read)
1507 z.reportln( "RXTXPort:SerialInputStream:read() b == 0");
1508 throw new NullPointerException();
1509 }
1510
1511 if( (off < 0) || (len < 0) || (off+len > b.length))
1512 {
1513 z.reportln("+++++++ IndexOutOfBoundsException()\n");
1514 if (debug_read)
1515 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1516 throw new IndexOutOfBoundsException();
1517 }
1518
1519 /*
1520 * Return immediately if len==0
1521 */
1522 if( len==0 )
1523 {
1524 if (debug_read)
1525 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1526 return 0;
1527 }
1528 /*
1529 * See how many bytes we should read
1530 */
1531 int Minimum = len;
1532
1533 if( threshold==0 )
1534 {
1535 /*
1536 * If threshold is disabled, read should return as soon
1537 * as data are available (up to the amount of available
1538 * bytes in order to avoid blocking)
1539 * Read may return earlier depending of the receive time
1540 * out.
1541 */
1542 int a = nativeavailable();
1543 if( a == 0 )
1544 Minimum = 1;
1545 else
1546 Minimum = Math.min( Minimum, a );
1547 }
1548 else
1549 {
1550 /*
1551 * Threshold is enabled. Read should return when
1552 * 'threshold' bytes have been received (or when the
1553 * receive timeout expired)
1554 */
1555 Minimum = Math.min(Minimum, threshold);
1556 }
1557 if ( monThreadisInterrupted == true )
1558 {
1559 if (debug_read)
1560 z.reportln( "RXTXPort:SerialInputStream:read() Interrupted");
1561 return(0);
1562 }
1563 synchronized (IOLockedMutex) {
1564 IOLocked++;
1565 }
1566 try
1567 {
1568 waitForTheNativeCodeSilly();
1569 result = readTerminatedArray( b, off, Minimum, t );
1570 if (debug_read_results)
1571 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
1572 return( result );
1573 }
1574 finally
1575 {
1576 synchronized (IOLockedMutex) {
1577 IOLocked--;
1578 }
1579 }
1580 }
1581 /**
1582 * @return int bytes available
1583 * @throws IOException
1584 */
1585 public synchronized int available() throws IOException
1586 {
1587 if ( monThreadisInterrupted == true )
1588 {
1589 return(0);
1590 }
1591 if ( debug_verbose )
1592 z.reportln( "RXTXPort:available() called" );
1593 synchronized (IOLockedMutex) {
1594 IOLocked++;
1595 }
1596 try
1597 {
1598 int r = nativeavailable();
1599 if ( debug_verbose )
1600 z.reportln( "RXTXPort:available() returning " +
1601 r );
1602 return r;
1603 }
1604 finally
1605 {
1606 synchronized (IOLockedMutex) {
1607 IOLocked--;
1608 }
1609 }
1610 }
1611 }
1612 /**
1613 */
1614 class MonitorThread extends Thread
1615 {
1616 /** Note: these have to be separate boolean flags because the
1617 SerialPortEvent constants are NOT bit-flags, they are just
1618 defined as integers from 1 to 10 -DPL */
1619 private volatile boolean CTS=false;
1620 private volatile boolean DSR=false;
1621 private volatile boolean RI=false;
1622 private volatile boolean CD=false;
1623 private volatile boolean OE=false;
1624 private volatile boolean PE=false;
1625 private volatile boolean FE=false;
1626 private volatile boolean BI=false;
1627 private volatile boolean Data=false;
1628 private volatile boolean Output=false;
1629
1630 MonitorThread()
1631 {
•1632 setDaemon(true);
1633 if (debug)
1634 z.reportln( "RXTXPort:MontitorThread:MonitorThread()");
1635 }
1636 /**
1637 * run the thread and call the event loop.
1638 */
1639 public void run()
1640 {
1641 if (debug)
1642 z.reportln( "RXTXPort:MontitorThread:run()");
1643 monThreadisInterrupted=false;
1644 eventLoop();
1645 if (debug)
1646 z.reportln( "eventLoop() returned");
1647 }
1648 protected void finalize() throws Throwable
1649 {
1650 if (debug)
1651 z.reportln( "RXTXPort:MonitorThread exiting");
1652 }
1653 }
1654 /**
1655 * A dummy method added so RXTX compiles on Kaffee
1656 * @deprecated deprecated but used in Kaffe
1657 */
1658 public void setRcvFifoTrigger(int trigger){};
1659
1660 /*------------------------ END OF CommAPI -----------------------------*/
1661
1662 private native static void nativeStaticSetSerialPortParams( String f,
1663 int b, int d, int s, int p )
1664 throws UnsupportedCommOperationException;
1665 private native static boolean nativeStaticSetDSR( String port,
1666 boolean flag )
1667 throws UnsupportedCommOperationException;
1668 private native static boolean nativeStaticSetDTR( String port,
1669 boolean flag )
1670 throws UnsupportedCommOperationException;
1671 private native static boolean nativeStaticSetRTS( String port,
1672 boolean flag )
1673 throws UnsupportedCommOperationException;
1674
1675 private native static boolean nativeStaticIsDSR( String port )
1676 throws UnsupportedCommOperationException;
1677 private native static boolean nativeStaticIsDTR( String port )
1678 throws UnsupportedCommOperationException;
1679 private native static boolean nativeStaticIsRTS( String port )
1680 throws UnsupportedCommOperationException;
1681 private native static boolean nativeStaticIsCTS( String port )
1682 throws UnsupportedCommOperationException;
1683 private native static boolean nativeStaticIsCD( String port )
1684 throws UnsupportedCommOperationException;
1685 private native static boolean nativeStaticIsRI( String port )
1686 throws UnsupportedCommOperationException;
1687
1688 private native static int nativeStaticGetBaudRate( String port )
1689 throws UnsupportedCommOperationException;
1690 private native static int nativeStaticGetDataBits( String port )
1691 throws UnsupportedCommOperationException;
1692 private native static int nativeStaticGetParity( String port )
1693 throws UnsupportedCommOperationException;
1694 private native static int nativeStaticGetStopBits( String port )
1695 throws UnsupportedCommOperationException;
1696
1697
1698 private native byte nativeGetParityErrorChar( )
1699 throws UnsupportedCommOperationException;
1700 private native boolean nativeSetParityErrorChar( byte b )
1701 throws UnsupportedCommOperationException;
1702 private native byte nativeGetEndOfInputChar( )
1703 throws UnsupportedCommOperationException;
1704 private native boolean nativeSetEndOfInputChar( byte b )
1705 throws UnsupportedCommOperationException;
1706 private native boolean nativeSetUartType(String type, boolean test)
1707 throws UnsupportedCommOperationException;
1708 native String nativeGetUartType()
1709 throws UnsupportedCommOperationException;
1710 private native boolean nativeSetBaudBase(int BaudBase)
1711 throws UnsupportedCommOperationException;
1712 private native int nativeGetBaudBase()
1713 throws UnsupportedCommOperationException;
1714 private native boolean nativeSetDivisor(int Divisor)
1715 throws UnsupportedCommOperationException;
1716 private native int nativeGetDivisor()
1717 throws UnsupportedCommOperationException;
1718 private native boolean nativeSetLowLatency()
1719 throws UnsupportedCommOperationException;
1720 private native boolean nativeGetLowLatency()
1721 throws UnsupportedCommOperationException;
1722 private native boolean nativeSetCallOutHangup(boolean NoHup)
1723 throws UnsupportedCommOperationException;
1724 private native boolean nativeGetCallOutHangup()
1725 throws UnsupportedCommOperationException;
1726 private native boolean nativeClearCommInput()
1727 throws UnsupportedCommOperationException;
1728
1729 /**
1730 * Extension to CommAPI
1731 * This is an extension to CommAPI. It may not be supported on
1732 * all operating systems.
1733 *
1734 * This is only accurate up to 38600 baud currently.
1735 *
1736 * @param port the name of the port thats been preopened
1737 * @return BaudRate on success
1738 * @throws UnsupportedCommOperationException;
1739 * This will not behave as expected with custom speeds
1740 *
1741 */
1742 public static int staticGetBaudRate( String port )
1743 throws UnsupportedCommOperationException
1744 {
1745 if ( debug )
1746 z.reportln(
1747 "RXTXPort:staticGetBaudRate( " + port + " )");
1748 return(nativeStaticGetBaudRate( port ));
1749 }
1750 /**
1751 * Extension to CommAPI
1752 * This is an extension to CommAPI. It may not be supported on
1753 * all operating systems.
1754 *
1755 * @param port the name of the port thats been preopened
1756 * @return DataBits on success
1757 * @throws UnsupportedCommOperationException;
1758 *
1759 */
1760 public static int staticGetDataBits( String port )
1761 throws UnsupportedCommOperationException
1762 {
1763 if ( debug )
1764 z.reportln(
1765 "RXTXPort:staticGetDataBits( " + port + " )");
1766 return(nativeStaticGetDataBits( port ) );
1767 }
1768
1769 /**
1770 * Extension to CommAPI
1771 * This is an extension to CommAPI. It may not be supported on
1772 * all operating systems.
1773 *
1774 * @param port the name of the port thats been preopened
1775 * @return Parity on success
1776 * @throws UnsupportedCommOperationException;
1777 *
1778 */
1779 public static int staticGetParity( String port )
1780 throws UnsupportedCommOperationException
1781 {
1782 if ( debug )
1783 z.reportln(
1784 "RXTXPort:staticGetParity( " + port + " )");
1785 return( nativeStaticGetParity( port ) );
1786 }
1787
1788 /**
1789 * Extension to CommAPI
1790 * This is an extension to CommAPI. It may not be supported on
1791 * all operating systems.
1792 *
1793 * @param port the name of the port thats been preopened
1794 * @return StopBits on success
1795 * @throws UnsupportedCommOperationException;
1796 *
1797 */
1798 public static int staticGetStopBits( String port )
1799 throws UnsupportedCommOperationException
1800 {
1801 if ( debug )
1802 z.reportln(
1803 "RXTXPort:staticGetStopBits( " + port + " )");
1804 return(nativeStaticGetStopBits( port ) );
1805 }
1806
1807 /**
1808 * Extension to CommAPI
1809 * This is an extension to CommAPI. It may not be supported on
1810 * all operating systems.
1811 *
1812 * Set the SerialPort parameters
1813 * 1.5 stop bits requires 5 databits
1814 * @param f filename
1815 * @param b baudrate
1816 * @param d databits
1817 * @param s stopbits
1818 * @param p parity
1819 *
1820 * @throws UnsupportedCommOperationException
1821 * @see gnu.io.UnsupportedCommOperationException
1822 */
1823
1824 public static void staticSetSerialPortParams( String f, int b, int d,
1825 int s, int p )
1826 throws UnsupportedCommOperationException
1827 {
1828 if ( debug )
1829 z.reportln(
1830 "RXTXPort:staticSetSerialPortParams( " +
1831 f + " " + b + " " + d + " " + s + " " + p );
1832 nativeStaticSetSerialPortParams( f, b, d, s, p );
1833 }
1834
1835 /**
1836 * Extension to CommAPI
1837 * This is an extension to CommAPI. It may not be supported on
1838 * all operating systems.
1839 *
1840 * Open the port and set DSR. remove lockfile and do not close
1841 * This is so some software can appear to set the DSR before 'opening'
1842 * the port a second time later on.
1843 *
1844 * @return true on success
1845 * @throws UnsupportedCommOperationException;
1846 *
1847 */
1848
1849 public static boolean staticSetDSR( String port, boolean flag )
1850 throws UnsupportedCommOperationException
1851 {
1852 if ( debug )
1853 z.reportln( "RXTXPort:staticSetDSR( " + port +
1854 " " + flag );
1855 return( nativeStaticSetDSR( port, flag ) );
1856 }
1857
1858 /**
1859 * Extension to CommAPI
1860 * This is an extension to CommAPI. It may not be supported on
1861 * all operating systems.
1862 *
1863 * Open the port and set DTR. remove lockfile and do not close
1864 * This is so some software can appear to set the DTR before 'opening'
1865 * the port a second time later on.
1866 *
1867 * @return true on success
1868 * @throws UnsupportedCommOperationException;
1869 *
1870 */
1871
1872 public static boolean staticSetDTR( String port, boolean flag )
1873 throws UnsupportedCommOperationException
1874 {
1875 if ( debug )
1876 z.reportln( "RXTXPort:staticSetDTR( " + port +
1877 " " + flag );
1878 return( nativeStaticSetDTR( port, flag ) );
1879 }
1880
1881 /**
1882 * Extension to CommAPI
1883 * This is an extension to CommAPI. It may not be supported on
1884 * all operating systems.
1885 *
1886 * Open the port and set RTS. remove lockfile and do not close
1887 * This is so some software can appear to set the RTS before 'opening'
1888 * the port a second time later on.
1889 *
1890 * @return none
1891 * @throws UnsupportedCommOperationException;
1892 *
1893 */
1894
1895 public static boolean staticSetRTS( String port, boolean flag )
1896 throws UnsupportedCommOperationException
1897 {
1898 if ( debug )
1899 z.reportln( "RXTXPort:staticSetRTS( " + port +
1900 " " + flag );
1901 return( nativeStaticSetRTS( port, flag ) );
1902 }
1903
1904 /**
1905 * Extension to CommAPI
1906 * This is an extension to CommAPI. It may not be supported on
1907 * all operating systems.
1908 *
1909 * find the fd and return RTS without using a Java open() call
1910 *
1911 * @param port
1912 * @return true if asserted
1913 * @throws UnsupportedCommOperationException;
1914 *
1915 */
1916
1917 public static boolean staticIsRTS( String port )
1918 throws UnsupportedCommOperationException
1919 {
1920 if ( debug )
1921 z.reportln( "RXTXPort:staticIsRTS( " + port + " )" );
1922 return( nativeStaticIsRTS( port ) );
1923 }
1924 /**
1925 * Extension to CommAPI
1926 * This is an extension to CommAPI. It may not be supported on
1927 * all operating systems.
1928 *
1929 * find the fd and return CD without using a Java open() call
1930 *
1931 * @param port
1932 * @return true if asserted
1933 * @throws UnsupportedCommOperationException;
1934 *
1935 */
1936
1937 public static boolean staticIsCD( String port )
1938 throws UnsupportedCommOperationException
1939 {
1940 if ( debug )
1941 z.reportln( "RXTXPort:staticIsCD( " + port + " )" );
1942 return( nativeStaticIsCD( port ) );
1943 }
1944 /**
1945 * Extension to CommAPI
1946 * This is an extension to CommAPI. It may not be supported on
1947 * all operating systems.
1948 *
1949 * find the fd and return CTS without using a Java open() call
1950 *
1951 * @param port
1952 * @return true if asserted
1953 * @throws UnsupportedCommOperationException;
1954 *
1955 */
1956
1957 public static boolean staticIsCTS( String port )
1958 throws UnsupportedCommOperationException
1959 {
1960 if ( debug )
1961 z.reportln( "RXTXPort:staticIsCTS( " + port + " )" );
1962 return( nativeStaticIsCTS( port ) );
1963 }
1964 /**
1965 * Extension to CommAPI
1966 * This is an extension to CommAPI. It may not be supported on
1967 * all operating systems.
1968 *
1969 * find the fd and return DSR without using a Java open() call
1970 *
1971 * @param port
1972 * @return true if asserted
1973 * @throws UnsupportedCommOperationException;
1974 *
1975 */
1976
1977 public static boolean staticIsDSR( String port )
1978 throws UnsupportedCommOperationException
1979 {
1980 if ( debug )
1981 z.reportln( "RXTXPort:staticIsDSR( " + port + " )" );
1982 return( nativeStaticIsDSR( port ) );
1983 }
1984 /**
1985 * Extension to CommAPI
1986 * This is an extension to CommAPI. It may not be supported on
1987 * all operating systems.
1988 *
1989 * find the fd and return DTR without using a Java open() call
1990 *
1991 * @param port
1992 * @return true if asserted
1993 * @throws UnsupportedCommOperationException;
1994 *
1995 */
1996
1997 public static boolean staticIsDTR( String port )
1998 throws UnsupportedCommOperationException
1999 {
2000 if ( debug )
2001 z.reportln( "RXTXPort:staticIsDTR( " + port + " )" );
2002 return( nativeStaticIsDTR( port ) );
2003 }
2004 /**
2005 * Extension to CommAPI
2006 * This is an extension to CommAPI. It may not be supported on
2007 * all operating systems.
2008 *
2009 * find the fd and return RI without using a Java open() call
2010 *
2011 * @param port
2012 * @return true if asserted
2013 * @throws UnsupportedCommOperationException;
2014 *
2015 */
2016
2017 public static boolean staticIsRI( String port )
2018 throws UnsupportedCommOperationException
2019 {
2020 if ( debug )
2021 z.reportln( "RXTXPort:staticIsRI( " + port + " )" );
2022 return( nativeStaticIsRI( port ) );
2023 }
2024
2025
2026 /**
2027 * Extension to CommAPI
2028 * This is an extension to CommAPI. It may not be supported on
2029 * all operating systems.
2030 * @return int the Parity Error Character
2031 * @throws UnsupportedCommOperationException;
2032 *
2033 * Anyone know how to do this in Unix?
2034 */
2035
2036 public byte getParityErrorChar( )
2037 throws UnsupportedCommOperationException
2038 {
2039 byte ret;
2040 if ( debug )
2041 z.reportln( "getParityErrorChar()" );
2042 ret = nativeGetParityErrorChar();
2043 if ( debug )
2044 z.reportln( "getParityErrorChar() returns " +
2045 ret );
2046 return( ret );
2047 }
2048
2049 /**
2050 * Extension to CommAPI
2051 * This is an extension to CommAPI. It may not be supported on
2052 * all operating systems.
2053 * @param b Parity Error Character
2054 * @return boolean true on success
2055 * @throws UnsupportedCommOperationException;
2056 *
2057 * Anyone know how to do this in Unix?
2058 */
2059
2060 public boolean setParityErrorChar( byte b )
2061 throws UnsupportedCommOperationException
2062 {
2063 if ( debug )
2064 z.reportln( "setParityErrorChar(" + b + ")" );
2065 return( nativeSetParityErrorChar( b ) );
2066 }
2067
2068 /**
2069 * Extension to CommAPI
2070 * This is an extension to CommAPI. It may not be supported on
2071 * all operating systems.
2072 * @return int the End of Input Character
2073 * @throws UnsupportedCommOperationException;
2074 *
2075 * Anyone know how to do this in Unix?
2076 */
2077
2078 public byte getEndOfInputChar( )
2079 throws UnsupportedCommOperationException
2080 {
2081 byte ret;
2082 if ( debug )
2083 z.reportln( "getEndOfInputChar()" );
2084 ret = nativeGetEndOfInputChar();
2085 if ( debug )
2086 z.reportln( "getEndOfInputChar() returns " +
2087 ret );
2088 return( ret );
2089 }
2090
2091 /**
2092 * Extension to CommAPI
2093 * This is an extension to CommAPI. It may not be supported on
2094 * all operating systems.
2095 * @param b End Of Input Character
2096 * @return boolean true on success
2097 * @throws UnsupportedCommOperationException;
2098 */
2099
2100 public boolean setEndOfInputChar( byte b )
2101 throws UnsupportedCommOperationException
2102 {
2103 if ( debug )
2104 z.reportln( "setEndOfInputChar(" + b + ")" );
2105 return( nativeSetEndOfInputChar( b ) );
2106 }
2107
2108 /**
2109 * Extension to CommAPI
2110 * This is an extension to CommAPI. It may not be supported on
2111 * all operating systems.
2112 * @param type String representation of the UART type which mayb
2113 * be "none", "8250", "16450", "16550", "16550A", "16650", "16550V2"
2114 * or "16750".
2115 * @param test boolean flag to determin if the UART should be tested.
2116 * @return boolean true on success
2117 * @throws UnsupportedCommOperationException;
2118 */
2119 public boolean setUARTType(String type, boolean test)
2120 throws UnsupportedCommOperationException
2121 {
2122 if ( debug )
2123 z.reportln( "RXTXPort:setUARTType()");
2124 return nativeSetUartType(type, test);
2125 }
2126 /**
2127 * Extension to CommAPI
2128 * This is an extension to CommAPI. It may not be supported on
2129 * all operating systems.
2130 * @return type String representation of the UART type which mayb
2131 * be "none", "8250", "16450", "16550", "16550A", "16650", "16550V2"
2132 * or "16750".
2133 * @throws UnsupportedCommOperationException;
2134 */
2135 public String getUARTType() throws UnsupportedCommOperationException
2136 {
2137 return nativeGetUartType();
2138 }
2139
2140 /**
2141 * Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
2142 * before using.
2143 * @param BaudBase The clock frequency divided by 16. Default
2144 * BaudBase is 115200.
2145 * @return true on success
2146 * @throws UnsupportedCommOperationException, IOException
2147 */
2148
2149 public boolean setBaudBase(int BaudBase)
2150 throws UnsupportedCommOperationException,
2151 IOException
2152 {
2153 if ( debug )
2154 z.reportln( "RXTXPort:setBaudBase()");
2155 return nativeSetBaudBase(BaudBase);
2156 }
2157
2158 /**
2159 * Extension to CommAPI
2160 * @return BaudBase
2161 * @throws UnsupportedCommOperationException, IOException
2162 */
2163
2164 public int getBaudBase() throws UnsupportedCommOperationException,
2165 IOException
2166 {
2167 if ( debug )
2168 z.reportln( "RXTXPort:getBaudBase()");
2169 return nativeGetBaudBase();
2170 }
2171
2172 /**
2173 * Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
2174 * before using.
2175 * @param Divisor
2176 * @throws UnsupportedCommOperationException, IOException
2177 */
2178
2179 public boolean setDivisor(int Divisor)
2180 throws UnsupportedCommOperationException, IOException
2181 {
2182 if ( debug )
2183 z.reportln( "RXTXPort:setDivisor()");
2184 return nativeSetDivisor(Divisor);
2185 }
2186
2187 /**
2188 * Extension to CommAPI
2189 * @return Divisor;
2190 * @throws UnsupportedCommOperationException, IOException
2191 */
2192
2193 public int getDivisor() throws UnsupportedCommOperationException,
2194 IOException
2195 {
2196 if ( debug )
2197 z.reportln( "RXTXPort:getDivisor()");
2198 return nativeGetDivisor();
2199 }
2200
2201 /**
2202 * Extension to CommAPI
2203 * returns boolean true on success
2204 * @throws UnsupportedCommOperationException
2205 */
2206
2207 public boolean setLowLatency() throws UnsupportedCommOperationException
2208 {
2209 if ( debug )
2210 z.reportln( "RXTXPort:setLowLatency()");
2211 return nativeSetLowLatency();
2212 }
2213
2214 /**
2215 * Extension to CommAPI
2216 * returns boolean true on success
2217 * @throws UnsupportedCommOperationException
2218 */
2219
2220 public boolean getLowLatency() throws UnsupportedCommOperationException
2221 {
2222 if ( debug )
2223 z.reportln( "RXTXPort:getLowLatency()");
2224 return nativeGetLowLatency();
2225 }
2226
2227 /**
2228 * Extension to CommAPI
2229 * returns boolean true on success
2230 * @throws UnsupportedCommOperationException
2231 */
2232
2233 public boolean setCallOutHangup(boolean NoHup)
2234 throws UnsupportedCommOperationException
2235 {
2236 if ( debug )
2237 z.reportln( "RXTXPort:setCallOutHangup()");
2238 return nativeSetCallOutHangup(NoHup);
2239 }
2240
2241 /**
2242 * Extension to CommAPI
2243 * returns boolean true on success
2244 * @throws UnsupportedCommOperationException
2245 */
2246
2247 public boolean getCallOutHangup()
2248 throws UnsupportedCommOperationException
2249 {
2250 if ( debug )
2251 z.reportln( "RXTXPort:getCallOutHangup()");
2252 return nativeGetCallOutHangup();
2253 }
2254
2255 /**
2256 * Extension to CommAPI
2257 * returns boolean true on success
2258 * @throws UnsupportedCommOperationException
2259 */
2260
2261 public boolean clearCommInput()
2262 throws UnsupportedCommOperationException
2263 {
2264 if ( debug )
2265 z.reportln( "RXTXPort:clearCommInput()");
2266 return nativeClearCommInput();
2267 }
2268
2269 /*------------------------ END OF CommAPI Extensions -----------------------*/
2270 }