一.使用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代码
- 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,下载地址是下载