package compackage;
import java.io.InputStream;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import systemfunction.*;
public class ComRunThr implements Runnable,SerialPortEventListener{
private Thread m_thisThread;
public SerialPort m_SerialPort=null;
public ComRunThr(){
}
//串口,波特率,数据位,停止位,校验位
public void openPort(String portnum,int baudrate,int databits,int stopbits,int parity)throws Exception{
try{
//防止重复打开
if(ComArg.SerialMap.get(portnum)!=null){
ComRunThr thiscls=(ComRunThr)ComArg.SerialMap.get(portnum);
thiscls.closePort();
}
String webname=MemoryClass.getIni(SysTag.Sys_WebName,"");//web.xml的display-name
if(!MemoryClass.getIni("RunUnderDebug","").equals(""))System.out.println("准备打开串口:"+portnum);//测试输出
Enumeration portList=CommPortIdentifier.getPortIdentifiers();//获得一个枚举对象,即端口对象
while(portList.hasMoreElements()){
CommPortIdentifier portId=(CommPortIdentifier)portList.nextElement();
if(!MemoryClass.getIni("RunUnderDebug","").equals(""))System.out.println("端口扫描:"+portId.getName()+"-"+portId.getPortType());//测试输出
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){//返回一个端口类型
if(!portId.getName().equals(portnum))continue;//找到端口对象
try{
m_SerialPort=(SerialPort)portId.open(webname,2000);//打开一个端口,程序名、阻塞时等待时间(毫秒)
/* open方法打开通讯端口,获得一个CommPort对象。它使程序独占端口。如果端口正被其他应用程序占用,将使用 CommPortOwnershipListener事件机制,传递一个PORT_OWNERSHIP_REQUESTED事件。每个端口都关联一个 InputStream 何一个OutputStream。如果端口是用open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非有close 被调用。有两个参数,第一个为应用程序名;第二个参数是在端口打开时阻塞等待的毫秒数。*/
m_SerialPort.addEventListener(this);//开启一个监听事件
m_SerialPort.notifyOnDataAvailable(true);//允许事件输入
//设置端口初始化数据,波特率、数据位、停止位、是否校检
m_SerialPort.setSerialPortParams(baudrate,databits,stopbits,parity);
if(!MemoryClass.getIni("RunUnderDebug","").equals(""))System.out.println("打开串口:"+portnum+":"+baudrate+":"+databits+":"+stopbits+":"+parity);//测试输出
}catch(Exception e){
System.out.println("打开串口失败:"+portnum+" "+e.toString());
e.printStackTrace();
}
}
}
if(m_SerialPort!=null){
m_thisThread=new Thread(this);
m_thisThread.start();
ComArg.SerialMap.put(portnum,this);
}
}catch(Exception e){e.printStackTrace();throw e;}
}
public void closePort(){
//if(m_SerialPort!=null)
this.m_SerialPort.close();
m_thisThread.stop();
ComArg.SerialMap.remove(m_SerialPort.getName());
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:/*Break interrupt,通讯中断*/
case SerialPortEvent.OE:/*Overrun error,溢位错误*/
case SerialPortEvent.FE:/*Framing error,传帧错误*/
case SerialPortEvent.PE:/*Parity error,校验错误*/
case SerialPortEvent.CD:/*Carrier detect,载波检测*/
case SerialPortEvent.CTS:/*Clear to send,清除发送*/
case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/
case SerialPortEvent.RI:/*Ring indicator,响铃指示*/
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: /*Output buffer is empty,输出缓冲区清空*/
break;
case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用数据。读到缓冲数组,输出到终端*/
try{
InputStream is=m_SerialPort.getInputStream();//获取端口传输的数据流
while (is.available() > 0) {
Integer ii=new Integer(is.read());
byte bt=ii.byteValue();
ComArg.SerialBuff.put(bt);
//if(!MemoryClass.getIni("RunUnderDebug","").equals("")){
//System.out.println(Integer.toHexString(bt).toUpperCase()+":"+ComArg.SerialBuff.position());//测试输出
//}
byte btr=0,btn=0;
if(ComArg.SerialBuff.position()>2){
btr=ComArg.SerialBuff.get(ComArg.SerialBuff.position()-2);
btn=ComArg.SerialBuff.get(ComArg.SerialBuff.position()-1);
}
if(btr==0xD && btn==0xA){
byte[] dst=new byte[ComArg.SerialBuff.position()];
for(int i=0;i
dst[i]=ComArg.SerialBuff.get(i);
}
//重新设置等待下次接收
ComArg.SerialBuff.clear();
ComDataProcThr datathr=new ComDataProcThr(dst);
datathr.start();
}
}
} catch (Exception e) {e.printStackTrace();}
break;
}
}
public void run() {
}
}