copy RXTXcomm.jar into your c:\program files\java\jre-version\lib\ext dir
把下载包中Windows\i368-mingw32\rxtxSerial.dll 放到你%java_home%\jre\bin下面
把下载包中RXTXcomm.jar放到%java_home%\jre\lib\ext下面
OK,可以写程序了
/**
*
*/
package cn.zhongxiaogang.test;
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SimpleRead implements SerialPortEventListener { //SerialPortEventListener 监听器,我的理解是独立开辟一个线程监听串口数据
static CommPortIdentifier portId; //串口通信管理类
static Enumeration portList; //已经连接上的端口的枚举
InputStream inputStream; //从串口来的输入流
OutputStream outputStream;//向串口输出的流
SerialPort serialPort; //串口的引用
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("myApp", 2000);//打开串口名字为myapp,延迟为2毫秒
} catch (PortInUseException e) {
}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
serialPort.addEventListener(this); //给当前串口天加一个监听器
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true); //当有数据时通知
try {
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, //设置串口读写参数
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
}
public void serialEvent(SerialPortEvent event) {//SerialPortEventListener 的方法,监听的时候会不断执行
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE://当有可用数据时读取数据,并且给串口返回数据
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
outputStream.write("xiaogang".getBytes());
System.out.println(new String(readBuffer));
} catch (IOException e) {
}
break;
}
}
public static void main(String[] args) {
try {
portList = CommPortIdentifier.getPortIdentifiers(); //得到当前连接上的端口
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {//判断如果端口类型是串口
if (portId.getName().equals("COM3")) { //判断如果COM3端口已经启动就连接
SimpleRead reader = new SimpleRead(); //实例一个
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
程序调试成功,不过还有很多问题,比如有乱码,还有程序不面向对象,etc.