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


二.用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,下载地址是下载

你可能感兴趣的:(java串口 来电显示)