Debian Codegraph @

CNI/RXTXPort.java

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