public classSerialPortManager {/*** 查找可用端口
*
*@return可用端口名称列表*/
public static final ListfindPorts() {//获得当前所有可用串口
Enumeration portList =CommPortIdentifier.getPortIdentifiers();
List ports = new ArrayList<>();while(portList.hasMoreElements()) {
String portName=portList.nextElement().getName();
ports.add(portName);
}returnports;
}/*** 打开串口
*
*@paramportName 端口名称
*@parambaudrate 波特率
*@return串口对象
*@throwsPortInUseException 端口被占用*/
public static SerialPort openPort(String portName, int baudrate) throwsPortInUseException {try{//通过端口名识别端口
CommPortIdentifier portIdentifier =CommPortIdentifier.getPortIdentifier(portName);//打开端口,并给端口名字和一个Timeout
CommPort commPort = portIdentifier.open(portName, 2000);//判断是不是串口
if (commPort instanceofSerialPort) {
SerialPort serialPort=(SerialPort) commPort;try{//设置一下串口的波特率等参数//数据位:8//停止位:1//校验位:None
serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e) {
e.printStackTrace();
}returnserialPort;
}
}catch(NoSuchPortException e) {
e.printStackTrace();
}return null;
}/*** 关闭串口*/
public static voidclosePort(SerialPort serialPort) {if (serialPort != null) {
serialPort.close();
}
}/*** 发送数据
*
*@paramserialPort 串口对象
*@paramorder 待发送数据*/
public static void sendMessageToPort(SerialPort serialPort, byte[] order) {
OutputStream out= null;try{
out=serialPort.getOutputStream();
out.write(order);
out.flush();
}catch(IOException e) {
e.printStackTrace();
}finally{if (out != null) {try{
out.close();
out= null;
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 读取数据*/
public static byte[] readFromPort(SerialPort serialPort) {
InputStream in= null;byte[] bytes ={};try{
in=serialPort.getInputStream();//缓冲区大小为一个字节
byte[] readBuffer = new byte[1];int bytesNum =in.read(readBuffer);while (bytesNum > 0) {
bytes=ArrayUtils.concat(bytes, readBuffer);
bytesNum=in.read(readBuffer);
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (in != null) {
in.close();
in= null;
}
}catch(IOException e) {
e.printStackTrace();
}
}returnbytes;
}/*** 添加监听器
*
*@paramserialPort 串口对象
*@paramlistener 串口存在有效数据监听*/
public static voidaddListener(SerialPort serialPort, DataAvailableListener listener) {try{//给串口添加监听器
serialPort.addEventListener(newSerialPortListener(listener));//设置当有数据到达时唤醒监听接收线程
serialPort.notifyOnDataAvailable(true);//设置当通信中断时唤醒中断线程
serialPort.notifyOnBreakInterrupt(true);
}catch(TooManyListenersException e) {
e.printStackTrace();
}
}/*** 串口监听*/
public static class SerialPortListener implementsSerialPortEventListener {privateDataAvailableListener mDataAvailableListener;publicSerialPortListener(DataAvailableListener mDataAvailableListener) {this.mDataAvailableListener =mDataAvailableListener;
}public voidserialEvent(SerialPortEvent serialPortEvent) {switch(serialPortEvent.getEventType()) {case SerialPortEvent.DATA_AVAILABLE: //1.串口存在有效数据
if (mDataAvailableListener != null) {
mDataAvailableListener.dataAvailable();
}break;case SerialPortEvent.OUTPUT_BUFFER_EMPTY: //2.输出缓冲区已清空
break;case SerialPortEvent.CTS: //3.清除待发送数据
break;case SerialPortEvent.DSR: //4.待发送数据准备好了
break;case SerialPortEvent.RI: //5.振铃指示
break;case SerialPortEvent.CD: //6.载波检测
break;case SerialPortEvent.OE: //7.溢位(溢出)错误
break;case SerialPortEvent.PE: //8.奇偶校验错误
break;case SerialPortEvent.FE: //9.帧错误
break;case SerialPortEvent.BI: //10.通讯中断
ShowUtils.errorMessage("与串口设备通讯中断");break;default:break;
}
}
}/*** 串口存在有效数据监听*/
public interfaceDataAvailableListener {/*** 串口存在有效数据*/
voiddataAvailable();
}
}