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