首先要把环境搭建好,也就是jdk还有tomcat,要是不需要再web上使用就不需要装了! 还有就是配置,也就是默认的comm.jar ,javax.comm.properties , win32com.dll这几个文件要放对地方 comm.jar放到C:\Program Files (x86)\Java\jdk1.7.0_01\jre\lib\ext 同时也放到jre相同目录下 javax.comm.properties放到 C:\Program Files (x86)\Java\jdk1.7.0_01\jre\lib 也放到jre下 win32com.dll放到C:\Program Files (x86)\Java\jdk1.7.0_01\jre\bin也放到jre下 同时 win32com.dll也放到c:windows下的System32下 也把comm.jar配置到classpath下 这个弄好了就是编程序了 package com.serial; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.TooManyListenersException; import javax.comm.CommPortIdentifier; import javax.comm.PortInUseException; import javax.comm.SerialPort; import javax.comm.SerialPortEvent; import javax.comm.SerialPortEventListener; /** * @项目名称 :illegalsms * @文件名称 :SerialPort.java * @所在包 :org.serial * @功能描述 : 串口类 * @创建日期 :2012-9-13 * @修改记录 : */ public class DSerialPort implements Runnable, SerialPortEventListener { private String appName = "串口通讯测试"; private int timeout = 2000;// open 端口时的等待时间 private int threadTime = 0; private CommPortIdentifier commPort; private SerialPort serialPort; private InputStream inputStream; private OutputStream outputStream; /** * @方法名称 :listPort * @功能描述 :列出所有可用的串口 * @返回值类型 :void */ @SuppressWarnings("rawtypes") public void listPort() { CommPortIdentifier cpid; Enumeration en = CommPortIdentifier.getPortIdentifiers(); System.out.println("now to list all Port of this PC:" + en); while (en.hasMoreElements()) { cpid = (CommPortIdentifier) en.nextElement(); if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(cpid.getName() + ", " + cpid.getCurrentOwner()); } } } /** * @方法名称 :selectPort * @功能描述 :选择一个端口,比如:COM1 * @返回值类型 :void * @param portName */ @SuppressWarnings("rawtypes") public void selectPort(String portName) { this.commPort = null; CommPortIdentifier cpid; Enumeration en = CommPortIdentifier.getPortIdentifiers(); while (en.hasMoreElements()) { cpid = (CommPortIdentifier) en.nextElement(); if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL && cpid.getName().equals(portName)) { this.commPort = cpid; break; } } openPort(); } /** * @方法名称 :openPort * @功能描述 :打开SerialPort * @返回值类型 :void */ private void openPort() { if (commPort == null) log(String.format("无法找到名字为'%1$s'的串口!", commPort.getName())); else { log("端口选择成功,当前端口:" + commPort.getName() + ",现在实例化 SerialPort:"); try { serialPort = (SerialPort) commPort.open(appName, timeout); log("实例 SerialPort 成功!"); } catch (PortInUseException e) { throw new RuntimeException(String.format("端口'%1$s'正在使用中!", commPort.getName())); } } } /** * @方法名称 :checkPort * @功能描述 :检查端口是否正确连接 * @返回值类型 :void */ private void checkPort() { if (commPort == null) throw new RuntimeException("没有选择端口,请使用 " + "selectPort(String portName) 方法选择端口"); if (serialPort == null) { throw new RuntimeException("SerialPort 对象无效!"); } } /** * @方法名称 :write * @功能描述 :向端口发送数据,请在调用此方法前 先选择端口,并确定SerialPort正常打开! * @返回值类型 :void * @param message */ public void write(String message) { checkPort(); try { outputStream = new BufferedOutputStream( serialPort.getOutputStream