package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.TooManyListenersException;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
/**
* 串口通信测试
* @author Danny
*
*/
public class SerialPortTest {
public static void main(String[] args) {
//获取所有的端口
SerialPortTest.getSystemPorts();
//开启端口com2,波特率9600
final SerialPort serialPort = SerialPortTest.openPort("COM4", 9600);
String str = "hello";
//发送数据
SerialPortTest.sendData(serialPort, str.getBytes());
//设置监听
SerialPortTest.setListenerToSerialPort(serialPort, new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent arg0) {
//是否为数据监听
if(arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
byte [] data = SerialPortTest.getData(serialPort);
System.out.println("接受数据的长度:"+data.length);
System.out.println("接受的数据:"+new String(data));
}
}
});
}
/**
* 获取端口列表
* @return
*/
@SuppressWarnings("unchecked")
public static List<String> getSystemPorts(){
List<String> sysPorts = new ArrayList<String>();
Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()) {
String portName = portList.nextElement().getName();
sysPorts.add(portName);
}
System.out.println("端口:"+sysPorts);
return sysPorts;
}
/**
* 开启串口
* @param portName 端口名
* @param baudRate 波特率
* @return
*/
public static SerialPort openPort(String portName,int baudRate) {
try {
//获取端口
CommPortIdentifier comPortIdentifier = CommPortIdentifier.getPortIdentifier(portName);
//打开端口
CommPort commPort= comPortIdentifier.open(portName, 3000);
if (commPort instanceof SerialPort) {
//端口转化为串口
SerialPort serialPort = (SerialPort) commPort;
//设置串口参数
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("串口"+serialPort.getName()+"开启成功");
return serialPort;
}
} catch (NoSuchPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭 串口
* @param serialPort 串口对象
*/
public static void closeSerialPort(SerialPort serialPort) {
if(serialPort != null) {
System.out.println("关闭了串口"+serialPort.getName());
serialPort.close();
serialPort = null;
}
}
/**
* 串口发消息
* @param serialPort 串口对象
* @param bytes 发送的数据
*/
public static void sendData(SerialPort serialPort,byte[] bytes) {
OutputStream out = null;
try {
out = serialPort.getOutputStream();
out.write(bytes);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(out!=null) {
try {
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 串口读消息
* @param serialPort 串口对象
* @return
*/
public static byte[] getData(SerialPort serialPort) {
InputStream is = null;
byte[] data = null;
try {
is = serialPort.getInputStream();//获取串口输入流
int bufflen = is.available();//获得输入流的长度
while(bufflen!=0) {
data = new byte[bufflen];//初始化byte数字
is.read(data);//读取数据
bufflen = is.available();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
/**
* 串口监听
* @param serialPort 串口对象
* @param eventListener 监听
*/
public static void setListenerToSerialPort(SerialPort serialPort,SerialPortEventListener eventListener) {
try {
//串口添加监听
serialPort.addEventListener(eventListener);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);//开启串口的数据监听
serialPort.notifyOnBreakInterrupt(true);//终端事件监听
}
}

5371

被折叠的 条评论
为什么被折叠?



