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

1    package serialPorts; 
2     
3    /* 
4    * This class is one end of a test pair of programs 
5    * SimpleSnuV1.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   import gnu.io.*; 
15    
16   import javax.comm.SerialPortEventListener; 
17   import javax.comm.CommPortIdentifier; 
18   import javax.comm.SerialPort; 
19   import javax.comm.PortInUseException; 
20   import javax.comm.UnsupportedCommOperationException; 
21   import javax.comm.SerialPortEvent; 
22   import java.io.IOException; 
23   import java.io.InputStream; 
24   import java.io.OutputStream; 
25   import java.util.Enumeration; 
26   import java.util.TooManyListenersException; 
27    
28   public class SimpleRead implements Runnable, SerialPortEventListener { 
29    
30       static CommPortIdentifier portId; 
31       static Enumeration portList; 
32       SerialPort serialPort = null; 
33       InputStream inputStream; 
34       OutputStream outputStream; 
35    
36       Thread readThread; 
37       String aS = ""; 
38       int numBytes = 0; 
39       String num = "021891383"; 
40       String rst = "atz"; 
41       String dial = "atd"; 
42       String outData = ""; 
43       String outDataBuff = ""; 
44       boolean running = true; 
45       boolean process = true; 
46       boolean waitForInput = true; 
47       boolean fatalErr = false; 
48       String errMessage = ""; 
49    
50       public static void main(String[] args) { 
51           String portName = "cu.modem"; 
52           portId = Utils.getPortByName(portName); 
53           if (portId != null) new SimpleRead(); 
54           else System.out.println("port"+portName+" not found."); 
55       } 
56    
57       public SimpleRead() { 
58           try { 
59               serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); 
60           } catch (PortInUseException e) { 
61           } 
62           try { 
63               inputStream = serialPort.getInputStream(); 
64               outputStream = serialPort.getOutputStream(); 
65           } catch (IOException e) { 
66           } 
67           try { 
68               serialPort.addEventListener(this); 
69           } catch (TooManyListenersException e) { 
70           } 
71           serialPort.notifyOnDataAvailable(true); 
72           try { 
73               serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
74           } catch (UnsupportedCommOperationException e) { 
75           } 
76           readThread = new Thread(this); 
77           readThread.start(); 
78       } 
79    
80       public void run() { 
81           int resetCount = 0; 
82           while (running) { 
83               if (fatalErr) { 
84                   System.out.print("Fatal Error...\n"); 
85                   System.exit(1); 
86               } 
87               if (!process) { 
88                   try { 
89                       Thread.sleep(500); 
90                   } catch (InterruptedException e) { 
91                   } 
92                   continue; 
93               } 
94               if (num.equals("Nil")) { 
95                   try { 
96                       Thread.sleep(500); 
97                   } catch (InterruptedException e) { 
98                   } 
99                   continue; 
100              } 
101              if (reset()) { 
102                  if (dial()) { 
103                      num = "Nil"; 
104                      System.out.print("Yahoooo OK...\n"); 
105                      //Here goes a class to handle any coms it gets passed the stuff it needs... 
106  //                  SNHdlcMessage myMess = new SNHdlcMessage(); 
107  //                  String rply = myMess.processInput("loc1", "text", inputStream, outputStream); 
108  //                  System.out.print(rply + "\n"); 
109                      process = false; 
110                      running = false; 
111                      System.out.print("Normal Exit...\n"); 
112                      System.exit(1); 
113                  } else { 
114                      System.out.print("Dial Error...\n"); 
115                      process = false; 
116                      running = false; 
117                      System.exit(1); 
118                  } 
119              } else { 
120                  System.out.print("Reset Error...\n"); 
121                  resetCount++; 
122                  if (resetCount > 2) { 
123                      System.out.print("Reset To Many Times Error ...\n"); 
124                      System.exit(1); 
125                  } 
126              } 
127              try { 
128                  Thread.sleep(100); 
129              } catch (InterruptedException e) { 
130              } 
131          } 
132      } 
133   
134      public void closePort(SerialPort serialPort) { 
135          if (serialPort != null) { 
136              serialPort.notifyOnDataAvailable(false); 
137              serialPort.removeEventListener(); 
138              if (inputStream != null) { 
139                  try { 
140                      inputStream.close(); 
141                      inputStream = null; 
142                  } catch (IOException e) { 
143                  } 
144              } 
145              if (outputStream != null) { 
146                  try { 
147                      outputStream.close(); 
148                      outputStream = null; 
149                  } catch (IOException e) { 
150                  } 
151              } 
152              serialPort.close(); 
153              serialPort = null; 
154          } 
155      } 
156   
157      public boolean reset() { 
158          try { 
159              outputStream.write(new String("atz").getBytes()); 
160              outputStream.write((byte) 0x0D); 
161              System.out.print("--> atz\n"); 
162          } catch (IOException e) { 
163              System.out.print("Reset Output IO Exception"); 
164              return false; 
165          } 
166          int waitingCount = 0; 
167          waitForInput = true; 
168          while (waitForInput) { 
169              try { 
170                  Thread.sleep(100); 
171              } catch (InterruptedException e) { 
172              } 
173              waitingCount++; 
174              //2 seconds for it to reset 
175              if (waitingCount > 20) { 
176                  return false; 
177              } 
178          } 
179          int numBytesTotal = 0; 
180          byte[] readBuffer = new byte[20]; 
181          String sReadBuff = ""; 
182          boolean dLoop = true; 
183          while (dLoop) { 
184              try { 
185                  while (inputStream.available() > 0) { 
186                      numBytes = inputStream.read(readBuffer); 
187                      numBytesTotal += numBytes; 
188                      String tmpR = new String(readBuffer); 
189                      sReadBuff += tmpR.substring(0, numBytes); 
190                  } 
191                  if (sReadBuff.indexOf("NO CARRIER") != -1) { 
192                      dLoop = false; 
193                  } else if (sReadBuff.indexOf("BUSY") != -1) { 
194                      dLoop = false; 
195                  } else if (sReadBuff.indexOf("NO DIALTONE") != -1) { 
196                      dLoop = false; 
197                  } else if (sReadBuff.indexOf("OK") != -1) { 
198                      dLoop = false; 
199                      System.out.print("<-- " + new String(sReadBuff)); 
200                      return true; 
201                  } else if (sReadBuff.indexOf("CONNECT") != -1) { 
202                      dLoop = false; 
203                  } 
204              } catch (IOException e) { 
205                  System.out.print("Reset Input IO Exception"); 
206                  return false; 
207              } 
208          } 
209          System.out.print("<-- " + new String(sReadBuff)); 
210          return false; 
211      } 
212   
213      public boolean dial() { 
214          try { 
215              outputStream.write(new String("atd").getBytes()); 
216              outputStream.write(num.getBytes()); 
217              outputStream.write((byte) 0x0D); 
218              System.out.print("--> atdxxxxx\n"); 
219          } catch (IOException e) { 
220              System.out.print("Dial Output IO Exception"); 
221              return false; 
222          } 
223          int waitingCount = 0; 
224          waitForInput = true; 
225          while (waitForInput) { 
226              try { 
227                  Thread.sleep(100); 
228              } catch (InterruptedException e) { 
229              } 
230              waitingCount++; 
231              if (waitingCount > 1000) { 
232                  return false; 
233              } 
234          } 
235          int numBytesTotal = 0; 
236          byte[] readBuffer = new byte[20]; 
237          String sReadBuff = ""; 
238          boolean dLoop = true; 
239          while (dLoop) { 
240              try { 
241                  while (inputStream.available() > 0) { 
242                      numBytes = inputStream.read(readBuffer); 
243                      numBytesTotal += numBytes; 
244                      String tmpR = new String(readBuffer); 
245                      sReadBuff += tmpR.substring(0, numBytes); 
246                  } 
247                  if (sReadBuff.indexOf("NO CARRIER") != -1) { 
248                      dLoop = false; 
249                  } else if (sReadBuff.indexOf("BUSY") != -1) { 
250                      dLoop = false; 
251                  } else if (sReadBuff.indexOf("NO DIALTONE") != -1) { 
252                      dLoop = false; 
253                  } else if (sReadBuff.indexOf("OK") != -1) { 
254                      dLoop = false; 
255                  } else if (sReadBuff.indexOf("CONNECT") != -1) { 
256                      System.out.print("<-- " + new String(sReadBuff)); 
257                      dLoop = false; 
258                      return true; 
259                  } 
260              } catch (IOException e) { 
261                  System.out.print("Dial Input IO Exception"); 
262                  return false; 
263              } 
264          } 
265          System.out.print("<-- " + new String(sReadBuff)); 
266          return false; 
267      } 
268   
269      public void serialEvent(SerialPortEvent event) { 
270          switch (event.getEventType()) { 
271              case SerialPortEvent.BI: 
272                  System.out.print("BI\n"); 
273              case SerialPortEvent.OE: 
274                  System.out.print("OE\n"); 
275              case SerialPortEvent.FE: 
276                  System.out.print("FE\n"); 
277              case SerialPortEvent.PE: 
278                  System.out.print("PE\n"); 
279              case SerialPortEvent.CD: 
280                  System.out.print("CD\n"); 
281              case SerialPortEvent.CTS: 
282                  System.out.print("CTS\n"); 
283              case SerialPortEvent.DSR: 
284                  System.out.print("DSR\n"); 
285              case SerialPortEvent.RI: 
286                  System.out.print("RI\n"); 
287              case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 
288                  System.out.print("Out Buff Empty\n"); 
289                  break; 
290              case SerialPortEvent.DATA_AVAILABLE: 
291                  waitForInput = false; 
292  //              System.out.print("Data Available\n"); 
293                  break; 
294          } 
295      } 
296   
297       
298  } 
299