链接:https://pan.baidu.com/s/1u5WiebTLED4WztkcGgfmdg 提取码:pvgy(32位)
链接:https://pan.baidu.com/s/1LD06gK2g69qE96dmauw3hw 提取码:yqo1 (64位)
rxtx解压包中的 rxtxParallel.dll,rxtxSerial.dll 这两个文件复制到 C:\Windows\System32 目录下
maven引入rxtxcomm
<dependency>
<groupId>org.bidib.jbidib.org.qbang.rxtx</groupId>
<artifactId>rxtxcomm</artifactId>
<version>2.2</version>
</dependency>
public class ReadCom implements SerialPortEventListener {
private Map<String,Object> map;
private List<Parameter> parameterList;
public ReadCom(Map<String,Object> map){
this.map = map;
parameterList=(List<Parameter>)map.get("parameterList");
}
//枚举类型
Enumeration<CommPortIdentifier> portList;
// 检测系统可用端口
private CommPortIdentifier portIdentifier;
// 端口
private SerialPort port;
// 输入/输出流
private InputStream inputStream;
private OutputStream outputStream;
public void openCom(String com) {
// 获得系统支持的所有端口(串口,并口)
portList = CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()) {
portIdentifier = (CommPortIdentifier)portList.nextElement();
// CommPortIdentifier.PORT_SERIAL :串口
// CommPortIdentifier.PORT_PARALLEL :并口
// CommPortIdentifier.PORT_RS485 :RS485
// CommPortIdentifier.PORT_I2C :I2C
// CommPortIdentifier.PORT_RAW
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// System.out.println(portIdentifier.getName()); 获取串口名字
if (portIdentifier.getName().equals(com)) {
try {
// open:打开串口,第一个参数应用程序名称 字符串可随意填写,第二个参数阻塞时等待多少毫秒
port = (SerialPort)portIdentifier.open(com, 2000);
// 串口设置监听
port.addEventListener(this);
// 设置可监听
port.notifyOnDataAvailable(true);
// 设置串口通信参数
// 波特率,数据位,停止位,校验方式
port.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream = port.getOutputStream();
inputStream = port.getInputStream();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TooManyListenersException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
//添加监听
public void serialEvent(SerialPortEvent event) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
/*
* SerialPortEvent.BI:/*Break interrupt,通讯中断
* SerialPortEvent.OE:/*Overrun error,溢位错误
* SerialPortEvent.FE:/*Framing error,传帧错误
* SerialPortEvent.PE:/*Parity error,校验错误
* SerialPortEvent.CD:/*Carrier detect,载波检测
* SerialPortEvent.CTS:/*Clear to send,清除发送
* SerialPortEvent.DSR:/*Data set ready,数据设备就绪
* SerialPortEvent.RI:/*Ring indicator,响铃指示
* SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空
* SerialPortEvent.DATA_AVAILABLE:
*/
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:
readData();
break;
default:
break;
}
}
//保存回复消息的字符串(有的数据会分包发过来 其实是一条数据 需要给数据拼接成一条数据)
public static String data="";
//读取数据
public void readData() {
byte[] rbuff = new byte[1024];
int hasRead = 0;
try {
while((hasRead=inputStream.read(rbuff)) > 0) {
System.out.println(rbuff);
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//发送数据
public void sendData(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("!!!串口输出流异常");
}
}
}