一.使用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代碼
- package comm;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.TooManyListenersException;
- import javax.comm.CommDriver;
- import javax.comm.CommPortIdentifier;
- import javax.comm.PortInUseException;
- import javax.comm.SerialPort;
- import javax.comm.SerialPortEvent;
- import javax.comm.SerialPortEventListener;
- import javax.comm.UnsupportedCommOperationException;
- public class SimpleWrite {
- public static void main(String[] args) {
- // System.loadLibrary("win32com");
- CommDriver driver = null;
- String driverName = "com.sun.comm.Win32Driver";
- // SerialPort sPort = (SerialPort) driver.getCommPort("COM4",
- // CommPortIdentifier.PORT_SERIAL);
- Enumeration<CommPortIdentifier> enumeration = CommPortIdentifier
- .getPortIdentifiers();
- while (enumeration.hasMoreElements()) {
- CommPortIdentifier portId = enumeration.nextElement();
- System.out.println(portId.getName() + "============");
- if (portId.getName().equals("COM1")) {
- try {
- System.out.println("jjj");
- final SerialPort sp = (SerialPort) portId.open(
- "SimpleWrite", 1000);
- sp.setSerialPortParams(2400, SerialPort.DATABITS_8,
- SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
- final InputStream is = sp.getInputStream();
- final OutputStream os = sp.getOutputStream();
- os.write(100);
- os.flush();
- os.close();
- //Set notifyOnDataAvailable to true to allow event driven input.
- sp.notifyOnDataAvailable(true);
- // Set notifyOnBreakInterrup to allow event driven break
- // handling.
- sp.notifyOnBreakInterrupt(true);
- // Set receive timeout to allow breaking out of polling loop
- // during input handling.
- sp.enableReceiveTimeout(30);
- final StringBuffer linkWgt = new StringBuffer();// 存放擷取的資料
- sp.addEventListener(new SerialPortEventListener() {
- @Override
- public void serialEvent(SerialPortEvent e) {
- int newData = 0;
- // Determine type of event.
- switch (e.getEventType()) {
- // Read data until -1 is returned. If \r is received
- // substitute
- // \n for correct newline handling.
- case SerialPortEvent.DATA_AVAILABLE:
- while (newData != -1) {
- try {
- newData = is.read();
- if (newData == -1) {
- break;
- }
- if ('\r' == (char) newData) {
- } else {
- linkWgt.append((char) newData);
- }
- } catch (IOException ex) {
- System.err.println(ex);
- return;
- }
- }
- // Append received data to messageAreaIn.
- try {
- System.out
- .println("linkWgt ---------||||| "
- + Double.valueOf(linkWgt
- .toString()));
- } catch (Exception ew) {
- ew.printStackTrace();
- } finally {
- try {
- // 用完了,記得關閉端口。
- is.close();
- sp.close();
- } catch (Exception c) {
- c.printStackTrace();
- }
- }
- break;
- // If break event append BREAK RECEIVED message.
- case SerialPortEvent.BI:
- System.out
- .println("\n--- BREAK RECEIVED ---\n");
- }
- }
- });
- } catch (PortInUseException e) {
- e.printStackTrace();
- } catch (UnsupportedCommOperationException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TooManyListenersException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
二.用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