You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1990 lines
53 KiB

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