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

1    package serialPorts; 
2     
3    /* 
4    * This class is one end of a test pair of programs 
5    * SimpleRead.class is the other end. 
6    * To run the test requires two com ports 
7    * and a null modem cable. 
8    * Start SimpleSnuV1 and then run SimpleRead 
9    * on the other machine/com port. 
10   * This code is supplied for demonstration purposes only. 
11   * You are free to use it as you please. 
12   */ 
13    
14    
15   import gnu.io.*; 
16    
17   import java.io.IOException; 
18   import java.io.InputStream; 
19   import java.io.OutputStream; 
20   import java.util.Enumeration; 
21   import java.util.TooManyListenersException; 
22    
23   public class SimpleSnuV1 implements Runnable, SerialPortEventListener { 
24    
25       static CommPortIdentifier portId; 
26       static Enumeration portList; 
27    
28       InputStream inputStream; 
29       OutputStream outputStream; 
30       SerialPort serialPort; 
31       Thread readThread; 
32    
33       int numBytes = 0; 
34       String num = "021891383"; 
35       String rst = "atz"; 
36       String dial = "atd"; 
37       String outData = ""; 
38       String outDataBuff = ""; 
39       boolean running = true; 
40       boolean process = true; 
41       boolean waitForInput = true; 
42    
43       public static void main(String[] args) { 
44           if (args.length < 1) { 
45               System.out.print("SimpleSnuV1.class /dev/ttyxx\n"); 
46               System.exit(-1); 
47           } 
48           portList = CommPortIdentifier.getPortIdentifiers(); 
49           while (portList.hasMoreElements()) { 
50               portId = (CommPortIdentifier) portList.nextElement(); 
51               if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 
52                   if (portId.getName().equals(args[0])) { 
53                       SimpleSnuV1 reader = new SimpleSnuV1(); 
54                   } 
55               } 
56           } 
57       } 
58    
59       public SimpleSnuV1() { 
60           try { 
61               serialPort = (SerialPort) portId.open("SimpleSnu", 2000); 
62           } catch (PortInUseException e) { 
63           } 
64           try { 
65               inputStream = serialPort.getInputStream(); 
66               outputStream = serialPort.getOutputStream(); 
67           } catch (IOException e) { 
68           } 
69           try { 
70               serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
71           } catch (UnsupportedCommOperationException e) { 
72           } 
73           byte[] readBuffer = new byte[20]; 
74           try { 
75               serialPort.addEventListener(this); 
76           } catch (TooManyListenersException e) { 
77               System.out.print("To Many Event Listeners\n"); 
78               System.exit(-1); 
79           } 
80           serialPort.notifyOnDataAvailable(true); 
81           readThread = new Thread(this); 
82           readThread.start(); 
83       } 
84    
85       public void run() { 
86           int resetCount = 0; 
87           int numBytes = 0; 
88           byte[] readBuffer = new byte[20]; 
89           String sReadBuff = ""; 
90           boolean connected = false; 
91           while (running) { 
92               if (!connected) { 
93                   try { 
94                       while (inputStream.available() > 0) { 
95                           numBytes = inputStream.read(readBuffer); 
96                           String tmpR = new String(readBuffer); 
97                           sReadBuff += tmpR.substring(0, numBytes); 
98                       } 
99                   } catch (IOException e) { 
100                      System.exit(1); 
101                  } 
102                  if (!sReadBuff.equals("")) { 
103                      System.out.print(sReadBuff + "\n"); 
104                  } else { 
105   
106                  } 
107                  int pos = 0; 
108                  if ((pos = sReadBuff.indexOf("atz")) != -1) { 
109                      try { 
110                          Thread.sleep(1000); 
111                      } catch (InterruptedException e) { 
112                      } 
113                      try { 
114                          outputStream.write(new String("OK").getBytes()); 
115                          outputStream.write((byte) 0x0D); 
116                          outputStream.write((byte) 0x0A); 
117                          System.out.print("OK Sent\n"); 
118                      } catch (IOException e) { 
119                          System.exit(1); 
120                      } 
121                      sReadBuff = ""; 
122                  } else if ((pos = sReadBuff.indexOf("atd")) != -1) { 
123                      sReadBuff = ""; 
124                      try { 
125                          Thread.sleep(1000); 
126                      } catch (InterruptedException e) { 
127                      } 
128                      try { 
129                          outputStream.write(new String("CONNECT 9600").getBytes()); 
130                          outputStream.write((byte) 0x0D); 
131                          outputStream.write((byte) 0x0A); 
132  //                      connected = true; 
133                      } catch (IOException e) { 
134                          System.exit(1); 
135                      } 
136                      try { 
137                          Thread.sleep(2000); 
138                      } catch (InterruptedException e) { 
139                      } 
140                  } 
141              } else { 
142   
143              } 
144              try { 
145                  Thread.sleep(100); 
146              } catch (InterruptedException e) { 
147              } 
148          } 
149   
150          System.out.print("Normal Exit...\n"); 
151      } 
152   
153      public void serialEvent(SerialPortEvent event) { 
154  //      try { 
155  //          Thread.sleep(500); 
156  //      } catch (InterruptedException e) {} 
157          switch (event.getEventType()) { 
158              case SerialPortEvent.BI: 
159                  System.out.print("BI\n"); 
160                  break; 
161              case SerialPortEvent.OE: 
162                  System.out.print("OE\n"); 
163                  break; 
164              case SerialPortEvent.FE: 
165                  System.out.print("FE\n"); 
166                  break; 
167              case SerialPortEvent.PE: 
168                  System.out.print("PE\n"); 
169                  break; 
170              case SerialPortEvent.CD: 
171                  System.out.print("CD\n"); 
172                  break; 
173              case SerialPortEvent.CTS: 
174                  System.out.print("CTS\n"); 
175                  break; 
176              case SerialPortEvent.DSR: 
177                  System.out.print("DSR\n"); 
178                  break; 
179              case SerialPortEvent.RI: 
180                  System.out.print("RI\n"); 
181                  break; 
182              case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 
183                  System.out.print("Out Buff Empty\n"); 
184                  break; 
185              case SerialPortEvent.DATA_AVAILABLE: 
186  //              waitForInput = false; 
187                  System.out.print("Data Available\n"); 
188                  break; 
189          } 
190      } 
191  } 
192   
193