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

1    package serialPorts; 
2     
3    import gnu.io.*; 
4     
5    import javax.comm.SerialPort; 
6    import javax.comm.UnsupportedCommOperationException; 
7    import javax.comm.PortInUseException; 
8    import javax.comm.CommPortIdentifier; 
9    import javax.comm.CommPort; 
10   import java.io.BufferedReader; 
11   import java.io.IOException; 
12   import java.io.InputStreamReader; 
13   import java.io.PrintStream; 
14    
15   public class ModemPort { 
16    
17    
18       /** holds the using application name - is good for sorting conflicts */ 
19       private String applicationName = "Modem Through Java Application"; 
20       /** How long to wait for the open to finish up. */ 
21       private int timeOutInSeconds = 30; 
22       /** The chosen Port itself */ 
23       private CommPort commPort; 
24       /** the requested port that the modem reside at this should be set as a string 
25        * in the format COMx and the x stand for the port number 
26        **/ 
27       private String portName = "/dev/cu.modem"; 
28       /** The baud rate to use. */ 
29       private int baud = 9600; 
30       /** will hold the current single port as we loop hrough all available in the system */ 
31       private CommPortIdentifier cpid = null; 
32       /** this holds the message we want to send to the modem */ 
33       private String messageString = "atdt8770890"; 
34       /** this will hold the serial port choosen to be used */ 
35       private SerialPort serialPort = null; 
36       /** this object is used to send data to the serial port */ 
37       private PrintStream ps = null; 
38       /** this object is used to receive data from the serial port */ 
39       private BufferedReader inputStream; 
40    
41    
42       public ModemPort() { 
43       } 
44    
45       public ModemPort(String portName, 
46                        String messageToSend) { 
47           this.portName = portName; 
48           messageString = messageToSend; 
49       } 
50    
51       /** 
52        * this method will open the port and prepare it for use 
53        */ 
54       public boolean openPort() { 
55           boolean returnedValue = true; 
56    
57           cpid = Utils.getPortByName(portName); 
58           try { 
59               returnedValue = initPort(returnedValue); 
60           } catch (PortInUseException err) { 
61               System.out.println( 
62                       "this port is in use by another application named: " 
63                       + err.currentOwner 
64                       + " please release it before running again"); 
65               returnedValue = false; 
66           } catch (UnsupportedCommOperationException err) { 
67               System.out.println( 
68                       "you have tried to set a" 
69                       + " property to a port that does not" 
70                       + " support this property maybe you are" 
71                       + " using paralel port instead of serial"); 
72               returnedValue = false; 
73           } 
74           return returnedValue; 
75       } 
76    
77       private boolean initPort(boolean returnedValue) throws 
78               PortInUseException, 
79               UnsupportedCommOperationException { 
80           // open the choosen port and set the 
81           // time to wait for it to open also state the opening application name 
82           commPort = cpid.open(applicationName, timeOutInSeconds * 1000); 
83           serialPort = (SerialPort) commPort; 
84           //set the serial port properties 
85           serialPort.setDTR(true); 
86           serialPort.setRTS(true); 
87           serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
88           try { 
89               initStreams(); 
90           } catch (IOException err) { 
91               System.out.println("could not get the ports output and input streams"); 
92               returnedValue = false; 
93           } 
94           return returnedValue; 
95       } 
96    
97       private void initStreams() throws IOException { 
98           //get the output stream from the serial port 
99           //object and then create a new print stream object with it 
100          ps = new PrintStream( 
101                  serialPort.getOutputStream()); 
102          //get the input stream from the serial port object 
103          //and then create a new BufferedReader object with it 
104          inputStream = new BufferedReader( 
105                  new InputStreamReader( 
106                          serialPort.getInputStream())); 
107      } 
108   
109      /**this method will send the current set message to the serial and make sure 
110       * that the modem echoed the command back, also the modem will send the prompt 
111       * line which this method will read and discard it */ 
112      public boolean sendToPort(String whatToSend) { 
113          boolean returnedValue = true; 
114   
115          try { 
116              sendMessage(whatToSend); 
117   
118          } catch (IOException err) { 
119              System.out.println("error occurred while trying to send data to the serial port through the print writer object"); 
120              returnedValue = false; 
121          } 
122          return returnedValue; 
123      } 
124   
125      private void sendMessage(String whatToSend) throws IOException { 
126          String junk = ""; 
127          //first set the string to send 
128          setMessageString(whatToSend); 
129          //System.out.println("user> " + whatToSend); 
130          ps.print(messageString); 
131          ps.print("\r\n"); 
132          //Expect the modem to echo the command we 
133          // have just sent if not then notify it 
134          if (!receive(messageString)) { 
135              System.err.println("WARNING: Modem did not echo command."); 
136          } 
137          //The modem sends an extra blank line by way of a prompt. 
138          //Here we read and discard it. 
139          junk = inputStream.readLine(); 
140          if (junk.length() != 0) { 
141              System.err.print("Warning unexpected response: "); 
142              System.err.println(junk); 
143          } 
144      } 
145   
146      /** same method as the above only allowing to preset the message */ 
147      public boolean sendToPort() { 
148          return sendToPort(messageString); 
149      } 
150   
151      /** this method will receive an expected result from the serial port and reply 
152       * if the received data is the expected or not 
153       **/ 
154      public boolean receive(String whatToExpect) { 
155          try { 
156              return isThisReturnedValue(whatToExpect); 
157          } catch (IOException err) { 
158              System.out.println("error occurred while " 
159                      +" to receive data from the " 
160                      +"serial port through " 
161                      +"the reader object"); 
162          } 
163   
164          return false; 
165      } 
166   
167      private boolean isThisReturnedValue(String whatToExpect) 
168              throws IOException { 
169          String response = inputStream.readLine(); 
170          System.out.println("modem> " + response); 
171          return response.indexOf(whatToExpect) >= 0; 
172      } 
173   
174      /** this method will close the writing and reading objects*/ 
175      public boolean closePort() { 
176          boolean returnedValue = true; 
177          //first rest so the modem can finish its job 
178          try { 
179              Thread.sleep(5000); 
180          } catch (InterruptedException e) { 
181          } 
182   
183   
184          //then close the port 
185          try { 
186              inputStream.close(); 
187              ps.close(); 
188              serialPort.close(); 
189          } catch (IOException err) { 
190              System.out.println( 
191                      "error occurred while trying to close" 
192                      +"the reader and writer objects"); 
193              returnedValue = false; 
194          } 
195          return returnedValue; 
196      } 
197   
198      public void printLightStatus() { 
199          boolean CDStatus = serialPort.isCD(); 
200          boolean CTSStatus = serialPort.isCTS(); 
201          boolean DSRStatus = serialPort.isDSR(); 
202          boolean DTRStatus = serialPort.isDTR(); 
203          System.out.println( 
204                  "CDStatus= " + CDStatus 
205                  + " CTSStatus= " + CTSStatus 
206                  + " DSRStatus= " + DSRStatus 
207                  + " DTRStatus= " + DTRStatus); 
208      } 
209   
210      /** this method uses the modem methods in order to call a phone number and keep the line bussy */ 
211      public void dialToPhone(String phoneNumber) { 
212          if (openPort()) { 
213              setLocalEchoOn(); 
214              // set local echo on 
215              receive("OK"); // expect OK as a response 
216              resetModem(); 
217              receive("OK"); // expect OK as a response 
218              sendToPort("ATDT" + phoneNumber); // dial number 
219              receive("RINGING"); // expect RINGING as a response 
220          } 
221      } 
222   
223      private void resetModem() { 
224          sendToPort("ATZ"); // reset modem 
225      } 
226   
227      private void setLocalEchoOn() { 
228          sendToPort("ATE1"); 
229      } 
230   
231      /** this method will send the modem the hang up command and will close the port */ 
232      public void hangUp() { 
233          sendToPort("ATH0"); 
234          // hang up modem call 
235          // (and allow hand set to be used without interference) 
236          receive("OK"); 
237          // expect OK as a response 
238          closePort(); 
239      } 
240   
241      /** this is a main test method */ 
242      public static void main(String[] args) { 
243          SetPath.loadNativeLib(); 
244          try { 
245              ModemPort mp = new ModemPort("/dev/cu.modem","atdt8770890"); 
246              mp.dialToPhone("877-0890"); 
247              System.out.println("the call is made please pickup the handset and press enter"); 
248              System.in.read(); 
249              mp.hangUp(); 
250          } catch (IOException err) { 
251              err.printStackTrace(); 
252          } 
253      } 
254   
255      public int getBaud() { 
256          return baud; 
257      } 
258   
259      public void setBaud(int baud) { 
260          this.baud = baud; 
261      } 
262   
263      public String getMessageString() { 
264          return messageString; 
265      } 
266   
267      public void setMessageString(String messageString) { 
268          this.messageString = messageString; 
269      } 
270   
271      public String getPortName() { 
272          return portName; 
273      } 
274   
275      public void setPortName(String portName) { 
276          this.portName = portName; 
277      } 
278   
279      public String getApplicationName() { 
280          return applicationName; 
281      } 
282   
283      public void setApplicationName(String myApplicationName) { 
284          this.applicationName = myApplicationName; 
285      } 
286   
287      public int getTimeOutInSeconds() { 
288          return timeOutInSeconds; 
289      } 
290   
291      public void setTimeOutInSeconds(int timeOut) { 
292          this.timeOutInSeconds = timeOut; 
293      } 
294  }