java 串口编程----监听方式获取串口数据

http://blog.sina.com.cn/s/blog_667528fd0100yuve.html


//以添加监听事件方式获取串口数据
import java.io.InputStream;
import java.io.OutputStream;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
public class test {
public void listPort(){
try {
CommPortIdentifierportId=CommPortIdentifier.getPortIdentifier("COM1");
SerialPort Sport=(SerialPort)portId.open("test", 2000);
System.out.println("串口 "+Sport.getName()+" 连接成功");
final SerialPort sp=Sport;
Sport.setSerialPortParams(2400, SerialPort.DATABITS_8,SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
OutputStream os=Sport.getOutputStream();
Sport.notifyOnDataAvailable(true);
Sport.notifyOnBreakInterrupt(true);
Sport.enableReceiveTimeout(500);
Sport.addEventListener(new SerialPortEventListener(){
public void serialEvent(SerialPortEvent e) {
InputStream is=null;
StringBuffer msgBuffer=new StringBuffer();
try {
is=sp.getInputStream();
} catch (Exception h) {
h.printStackTrace();
}
int newData=0;
switch (e.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
while(newData!=-1){
try {
newData=is.read();
if(newData==-1){
break;
}
if('\r'==(char)newData){
}else{
msgBuffer.append((char)newData);
}
} catch (Exception f) {
f.printStackTrace();
}
}
try {
System.out.println("Resultis:"+Double.valueOf(msgBuffer.toString()));
} catch (Exception b) {
b.printStackTrace();
}finally{
try {
is.close();
sp.close();
} catch (Exception c) {
c.printStackTrace();
}
}
break;
case SerialPortEvent.BI:
System.out.println("break recieve");
break;
default:
break;
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test t=new test();
t.listPort();
}
}


你可能感兴趣的:(Java)