Debian Codegraph @

src/gnu/io/RXTXPort.java

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