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.

99 lines
3.1 KiB

5 years ago
  1. /*-------------------------------------------------------------------------
  2. | rxtx is a native interface to serial ports in java.
  3. | Copyright 1997-2004 by Trent Jarvi taj@parcelfarce.linux.theplanet.co.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. /*
  20. test : TestMonitorThread
  21. Author : Trent Jarvi
  22. added : Sat Oct 13 17:45:31 MDT 2001
  23. Problem: Monitor Thread didnt go away in the past. This makes sure
  24. the thread does exit and is GC's. You can watch the threads
  25. in a native threads jvm with top.
  26. todo : There are still some issues with the way BlackBox open/closes
  27. the ports and the removal of the eventListener.
  28. */
  29. import gnu.io.*;
  30. import java.util.*;
  31. public class TestMonitorThread implements SerialPortEventListener
  32. {
  33. public TestMonitorThread()
  34. {
  35. CommPortIdentifier cpi;
  36. Enumeration ports;
  37. SerialPort port = null;
  38. Date d = new Date();
  39. long result, t1 = d.getTime(), t2 = d.getTime();
  40. ports = CommPortIdentifier.getPortIdentifiers();
  41. while ( ports.hasMoreElements() )
  42. {
  43. cpi = (CommPortIdentifier) ports.nextElement();
  44. if ( cpi.getPortType() == CommPortIdentifier.PORT_SERIAL )
  45. {
  46. if ( cpi.getName().equals( "/dev/ttyS0" ) )
  47. {
  48. try {
  49. port = (SerialPort) cpi.open("TestMonitorThread", 2000);
  50. } catch (PortInUseException e) {}
  51. break;
  52. }
  53. }
  54. }
  55. for( int i=0;i<30;i++ )
  56. {
  57. try {
  58. port.addEventListener(this);
  59. } catch (TooManyListenersException e ) {
  60. e.printStackTrace();
  61. }
  62. t2 = new Date().getTime();
  63. port.removeEventListener();
  64. System.out.println( t2 - t1 );
  65. t1 = t2;
  66. }
  67. port.close();
  68. }
  69. public static void main( String[] args )
  70. {
  71. System.out.println(">my TestMonitorThread");
  72. TestMonitorThread thisTestMonitorThread = new TestMonitorThread();
  73. System.out.println("<my TestMonitorThread");
  74. }
  75. public void serialEvent(SerialPortEvent event)
  76. {
  77. switch (event.getEventType())
  78. {
  79. case SerialPortEvent.BI:
  80. case SerialPortEvent.OE:
  81. case SerialPortEvent.FE:
  82. case SerialPortEvent.PE:
  83. case SerialPortEvent.CD:
  84. case SerialPortEvent.CTS:
  85. case SerialPortEvent.DSR:
  86. case SerialPortEvent.RI:
  87. case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
  88. case SerialPortEvent.DATA_AVAILABLE:
  89. System.out.println("Event");
  90. break;
  91. }
  92. }
  93. }