1、以下是java 串口调试助手的源码供学者参考
2、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
3、import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.TooManyListenersException;public class Comm implements SerialPortEventListener, Runnable {private InputStream is = null;private OutputStream os =null;private byte[] chars = new byte[64];private SerialPort port = null
4、 public ArrayList listPortChoices(){ ArrayList list = new ArrayList(); CommPortIdentifier portId; Enumeration en = CommPortIdentifier.getPortIdentifiers(); while (en.hasMoreElements()) { portId = (CommPortIdentifier)en.nextElement(); if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){ list.add(portId.getName()); } } return list; }
5、 public boolean openPort(String portName) throws TooManyListenersException{ boolean bl = false; try { CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);//获得串口管理对象 port = (SerialPort) portId.open(portName,3000);//打开并获得串口对象 port.setSerialPortParams(9600,8,1,0);//配置串口 is = port.getInputStream();//获得串口输入流 os = port.getOutputStream();//获得串口输出流 bl = true; } catch (NoSuchPortException e) { e.printStackTrace(); } catch (PortInUseException e) { e.printStackTrace(); System.out.println("该串口已被其他程序占用"); } catch (UnsupportedCommOperationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bl;
6、public boolean checkPort(){if(port == null){return true;}else{return false;}} public void closePort(){ if (port != null) { try { os.close(); is.close(); } catch (IOException e) { System.err.println(e); } } port.removeEventListener(); port.close(); port = null; }
7、public void write(byte[] out,int len){ try{ for(int i=0;i<len;i ){ os.write(out[i]); os.flush(); } return; }catch(IOException e1){ System.out.println(e1); return; }catch(ArrayIndexOutOfBoundsException e2){ System.out.println(e2); } }public void Read() throws TooManyListenersException{try{is = new BufferedInputStream(port.getInputStream());}catch(IOException e){throw new RuntimeException();}try{port.addEventListener(this);}catch(TooManyListenersException e){throw new RuntimeException(e.getMessage());}port.notifyOnDataAvailable(true)
8、if(true){Thread thread = new Thread(this);thread.start();魈胺闹臣}}@Overridepublic void serialEvent(SerialPortEvent event) {// TODO Auto-generated method stubswitch (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: byte[] readbuffer = new byte[1024]; String readstr = null; String s2 = null; try{ while(is.available()>0){ is.read(readbuffer); readstr = new String(readbuffer).trim(); } s2 = new String(readbuffer).trim(); }catch(IOException e){ } } }
9、@Overridepublic void run() {// TODO Auto-generated method stubString temp = null;int len = 0;try {len = is.read(chars);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}while(true){if(checkPort()){while(len!=-1){temp = new String(chars,0,len);System.out.println(temp);}}else{}}}}