/Users/lyon/j4p/src/serialPorts/TestMonitorThread.java

1    package serialPorts; 
2     
3    /*------------------------------------------------------------------------- 
4    |   rxtx is a native interface to serial ports in java. 
5    |   Copyright 1997-2002 by Trent Jarvi taj@parcelfarce.linux.theplanet.co.uk 
6    | 
7    |   This library is free software; you can redistribute it and/or 
8    |   modify it under the terms of the GNU Library General Public 
9    |   License as published by the Free Software Foundation; either 
10   |   version 2 of the License, or (at your option) any later version. 
11   | 
12   |   This library is distributed in the hope that it will be useful, 
13   |   but WITHOUT ANY WARRANTY; without even the implied warranty of 
14   |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
15   |   Library General Public License for more details. 
16   | 
17   |   You should have received a copy of the GNU Library General Public 
18   |   License along with this library; if not, write to the Free 
19   |   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20   --------------------------------------------------------------------------*/ 
21   /* 
22    
23       test   :  TestMonitorThread 
24       Author :  Trent Jarvi 
25       added  :  Sat Oct 13 17:45:31 MDT 2001 
26       Problem:  Monitor Thread didnt go away in the past.  This makes sure 
27             the thread does exit and is GC's.  You can watch the threads 
28             in a native threads jvm with top. 
29       todo   :  There are still some issues with the way BlackBox open/closes 
30             the ports and the removal of the eventListener. 
31    
32   */ 
33    
34   import gnu.io.*; 
35    
36   import javax.comm.CommPortIdentifier; 
37   import javax.comm.SerialPortEventListener; 
38   import javax.comm.SerialPort; 
39   import javax.comm.SerialPortEvent; 
40   import javax.comm.PortInUseException; 
41   import java.util.Date; 
42   import java.util.TooManyListenersException; 
43    
44    
45   public class TestMonitorThread implements SerialPortEventListener { 
46    
47       public TestMonitorThread() { 
48           Utils.listPorts(); 
49           String portName = "/dev/cu.modem"; 
50           CommPortIdentifier cpi = Utils.getPortByName(portName); 
51           if (cpi == null){ 
52               System.out.println("portName:"+portName+" not found"); 
53               return; 
54           } 
55           SerialPort port = null; 
56           Date d = new Date(); 
57           long result, t1 = d.getTime(), t2 = d.getTime(); 
58    
59           try { 
60               port = (SerialPort) cpi.open("TestMonitorThread", 2000); 
61           } catch (PortInUseException e) { 
62           } 
63    
64           for (int i = 0; i < 30; i++) { 
65               try { 
66                   port.addEventListener(this); 
67               } catch (TooManyListenersException e) { 
68                   e.printStackTrace(); 
69               } 
70               t2 = new Date().getTime(); 
71               port.removeEventListener(); 
72               System.out.println(t2 - t1); 
73               t1 = t2; 
74           } 
75           port.close(); 
76       } 
77    
78       public static void main(String[] args) { 
79           System.out.println(">my TestMonitorThread"); 
80           TestMonitorThread thisTestMonitorThread = new TestMonitorThread(); 
81           System.out.println("<my TestMonitorThread"); 
82       } 
83    
84       public void serialEvent(SerialPortEvent event) { 
85           switch (event.getEventType()) { 
86               case SerialPortEvent.BI: 
87               case SerialPortEvent.OE: 
88               case SerialPortEvent.FE: 
89               case SerialPortEvent.PE: 
90               case SerialPortEvent.CD: 
91               case SerialPortEvent.CTS: 
92               case SerialPortEvent.DSR: 
93               case SerialPortEvent.RI: 
94               case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 
95               case SerialPortEvent.DATA_AVAILABLE: 
96                   System.out.println("Event"); 
97                   break; 
98           } 
99       } 
100  } 
101