root / trunk / extensions / extGPS / test / TestMonitorThread.java @ 4720
History | View | Annotate | Download (2.99 KB)
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 |
|
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 |
import gnu.io.*; |
32 |
import java.util.*; |
33 |
|
34 |
|
35 |
public class TestMonitorThread implements SerialPortEventListener |
36 |
{ |
37 |
|
38 |
public TestMonitorThread()
|
39 |
{ |
40 |
CommPortIdentifier cpi; |
41 |
Enumeration ports;
|
42 |
SerialPort port = null;
|
43 |
Date d = new Date(); |
44 |
long result, t1 = d.getTime(), t2 = d.getTime();
|
45 |
|
46 |
ports = CommPortIdentifier.getPortIdentifiers(); |
47 |
while ( ports.hasMoreElements() )
|
48 |
{ |
49 |
cpi = (CommPortIdentifier) ports.nextElement(); |
50 |
if ( cpi.getPortType() == CommPortIdentifier.PORT_SERIAL )
|
51 |
{ |
52 |
if ( cpi.getName().equals( "/dev/ttyS0" ) ) |
53 |
{ |
54 |
try {
|
55 |
port = (SerialPort) cpi.open("TestMonitorThread", 2000); |
56 |
} catch (PortInUseException e) {}
|
57 |
break;
|
58 |
} |
59 |
} |
60 |
} |
61 |
for( int i=0;i<30;i++ ) |
62 |
{ |
63 |
try {
|
64 |
port.addEventListener(this);
|
65 |
} catch (TooManyListenersException e ) { |
66 |
e.printStackTrace(); |
67 |
} |
68 |
t2 = new Date().getTime(); |
69 |
port.removeEventListener(); |
70 |
System.out.println( t2 - t1 );
|
71 |
t1 = t2; |
72 |
} |
73 |
port.close(); |
74 |
} |
75 |
public static void main( String[] args ) |
76 |
{ |
77 |
System.out.println(">my TestMonitorThread"); |
78 |
TestMonitorThread thisTestMonitorThread = new TestMonitorThread();
|
79 |
System.out.println("<my TestMonitorThread"); |
80 |
} |
81 |
public void serialEvent(SerialPortEvent event) |
82 |
{ |
83 |
switch (event.getEventType())
|
84 |
{ |
85 |
case SerialPortEvent.BI:
|
86 |
case SerialPortEvent.OE:
|
87 |
case SerialPortEvent.FE:
|
88 |
case SerialPortEvent.PE:
|
89 |
case SerialPortEvent.CD:
|
90 |
case SerialPortEvent.CTS:
|
91 |
case SerialPortEvent.DSR:
|
92 |
case SerialPortEvent.RI:
|
93 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
|
94 |
case SerialPortEvent.DATA_AVAILABLE:
|
95 |
System.out.println("Event"); |
96 |
break;
|
97 |
} |
98 |
} |
99 |
} |