天天看點

java序列槽 來電顯示

一.使用sun公司的comm.jar 

  1.配置(下載下傳comm.jar包http://code.google.com/p/smslib/downloads/detail?name=javacomm20-win32.zip&can=2&q=) 

    (1).将包下的javax.comm.properties放到jdk home/jre/lib下 

    (2).将包下的win32com.dll放到jdk home/jre/bin下(也可以放到windows下的system32下) 

    (3).将comm.jar放到jdk home/jre/lib/ext下 

  2.java代碼 

Java代碼   

java序列槽 來電顯示
  1. package comm;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.util.Enumeration;  
  6. import java.util.TooManyListenersException;  
  7. import javax.comm.CommDriver;  
  8. import javax.comm.CommPortIdentifier;  
  9. import javax.comm.PortInUseException;  
  10. import javax.comm.SerialPort;  
  11. import javax.comm.SerialPortEvent;  
  12. import javax.comm.SerialPortEventListener;  
  13. import javax.comm.UnsupportedCommOperationException;  
  14. public class SimpleWrite {  
  15.     public static void main(String[] args) {  
  16.         // System.loadLibrary("win32com");  
  17.         CommDriver driver = null;  
  18.         String driverName = "com.sun.comm.Win32Driver";  
  19.         // SerialPort sPort = (SerialPort) driver.getCommPort("COM4",  
  20.         // CommPortIdentifier.PORT_SERIAL);  
  21.         Enumeration<CommPortIdentifier> enumeration = CommPortIdentifier  
  22.                 .getPortIdentifiers();  
  23.         while (enumeration.hasMoreElements()) {  
  24.             CommPortIdentifier portId = enumeration.nextElement();  
  25.             System.out.println(portId.getName() + "============");  
  26.             if (portId.getName().equals("COM1")) {  
  27.                 try {  
  28.                     System.out.println("jjj");  
  29.                     final SerialPort sp = (SerialPort) portId.open(  
  30.                             "SimpleWrite", 1000);  
  31.                     sp.setSerialPortParams(2400, SerialPort.DATABITS_8,  
  32.                             SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);  
  33.                     final InputStream is = sp.getInputStream();  
  34.                     final OutputStream os = sp.getOutputStream();  
  35.                     os.write(100);  
  36.                     os.flush();  
  37.                     os.close();  
  38.                     //Set notifyOnDataAvailable to true to allow event driven input.     
  39.                     sp.notifyOnDataAvailable(true);  
  40.                     // Set notifyOnBreakInterrup to allow event driven break  
  41.                     // handling.  
  42.                     sp.notifyOnBreakInterrupt(true);  
  43.                     // Set receive timeout to allow breaking out of polling loop  
  44.                     // during input handling.  
  45.                     sp.enableReceiveTimeout(30);  
  46.                     final StringBuffer linkWgt = new StringBuffer();// 存放擷取的資料  
  47.                     sp.addEventListener(new SerialPortEventListener() {  
  48.                         @Override  
  49.                         public void serialEvent(SerialPortEvent e) {  
  50.                             int newData = 0;  
  51.                             // Determine type of event.  
  52.                             switch (e.getEventType()) {  
  53.                             // Read data until -1 is returned. If \r is received  
  54.                             // substitute  
  55.                             // \n for correct newline handling.  
  56.                             case SerialPortEvent.DATA_AVAILABLE:  
  57.                                 while (newData != -1) {  
  58.                                     try {  
  59.                                         newData = is.read();  
  60.                                         if (newData == -1) {  
  61.                                             break;  
  62.                                         }  
  63.                                         if ('\r' == (char) newData) {  
  64.                                         } else {  
  65.                                             linkWgt.append((char) newData);  
  66.                                         }  
  67.                                     } catch (IOException ex) {  
  68.                                         System.err.println(ex);  
  69.                                         return;  
  70.                                     }  
  71.                                 }  
  72.                                 // Append received data to messageAreaIn.  
  73.                                 try {  
  74.                                     System.out  
  75.                                             .println("linkWgt ---------|||||          "  
  76.                                                     + Double.valueOf(linkWgt  
  77.                                                             .toString()));  
  78.                                 } catch (Exception ew) {  
  79.                                     ew.printStackTrace();  
  80.                                 } finally {  
  81.                                     try {  
  82.                                         // 用完了,記得關閉端口。  
  83.                                         is.close();  
  84.                                         sp.close();  
  85.                                     } catch (Exception c) {  
  86.                                         c.printStackTrace();  
  87.                                     }  
  88.                                 }  
  89.                                 break;  
  90.                             // If break event append BREAK RECEIVED message.  
  91.                             case SerialPortEvent.BI:  
  92.                                 System.out  
  93.                                         .println("\n--- BREAK RECEIVED ---\n");  
  94.                             }  
  95.                         }  
  96.                     });  
  97.                 } catch (PortInUseException e) {  
  98.                     e.printStackTrace();  
  99.                 } catch (UnsupportedCommOperationException e) {  
  100.                     e.printStackTrace();  
  101.                 } catch (IOException e) {  
  102.                     e.printStackTrace();  
  103.                 } catch (TooManyListenersException e) {  
  104.                     e.printStackTrace();  
  105.                 }  
  106.             }  
  107.         }  
  108.     }  
  109. }  

二.用rxtx.jar代替comm.jar 

   # copy rxtxSerial.dll into your c:\program files\java\jre-version\bin dir 

   # copy RXTXcomm.jar into your c:\program files\java\jre-version\lib\ext dir 

   # change all references from 'javax.comm' to 'gnu.io' 

三.然後就可以測試了,推薦使用Virtual Serial Port Driver 6.9 by Eltima Software 

   它可以虛拟出兩個相連的序列槽,你可以通過包裡的一個小例子:SimpleWrite,SimpleRead 

   來測試 

四.檢查下你的modem支不支援來電顯示: 

  1.打開超級終端,随便輸入一個連接配接名稱,例如TEST。(也可以用其它終端件,       如Bitware) 

  2.按确定後,下個視窗中,看到你的MODEM,不要輸入電話号碼。再下一步。 

  3.在這個視窗中按取消。 

  4.這樣一個可以輸入的空白視窗就有了。 

    輸入:ATE1回車 

    如果出現OK,說明MODEM支援AT指令,不然,其他也不用試了。 

    然後輸入下面的指令,隻要一條反應有OK,就說明MODEM本身晶片支援來電顯示。 

    AT#CID=1 比較常用 

    AT%CCID=1 

    AT+VCID=1 比較常用 

    AT#CC1 

    AT*ID1 

  5.如果有一條有OK,接下來,你打入電話,(事先接好電話線到MODEM)。 

    如果你的MODEM真的支援來電顯示,則會出現如下類似的内容: 

    RING 

    DATE = 1010 

    TIME = 1600 

    NMBR = **********(你撥入的主叫号碼) 

    RING 

    如果你的MODEM不支援來電顯示,則此時可能會顯示: 

    RING 

    RING 

    RING 

    還有的MODEM,什麼也不顯示,那就更不可能支援來電顯示了。 

http://www.ititgo.com.cn/goods-144.html

下面我也把comm的包貼上來,估摸着很多朋友進不去那個googlecode