RXTXcomm的工具类

package com;

import java.io.IOException;

import java.io.InputStream;


import java.io.OutputStream;



import java.util.TooManyListenersException;

import gnu.io.CommPortIdentifier;

import gnu.io.PortInUseException;

import gnu.io.SerialPort;

import gnu.io.SerialPortEvent;

import gnu.io.SerialPortEventListener;

import gnu.io.UnsupportedCommOperationException;

public class CommUtil implements SerialPortEventListener,Runnable {

	InputStream inputStream; 
	
	OutputStream outputStream;
	
	SerialPort serialPort; 
	
	
	CommPortIdentifier portId = null;
	
	Thread readThread;
	
	String msg = "";
	
	String message = "";
	
	StringBuffer sb = new StringBuffer();

	public CommUtil(String portName) {
		
		try{
			portId = CommPortIdentifier.getPortIdentifier(portName);
			if ( portId.isCurrentlyOwned() )
	        {
	            System.out.println("端口正被其他程序占用");
	        }
		}catch(Exception e){}
		try {
			serialPort = (SerialPort) portId.open("CommUtil", 2000);
		} catch (PortInUseException e) {
			System.out.println("实例化失败");
		}
		try {
			inputStream = serialPort.getInputStream();
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {}
		try {
			serialPort.addEventListener(this); 
		} catch (TooManyListenersException e) {}
		serialPort.notifyOnDataAvailable(true); 
		try {
			serialPort.setSerialPortParams(1200, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
		}catch (UnsupportedCommOperationException e) {}
		readThread = new Thread(this);
    	readThread.start();
	}
	
	public String receive(){
		readThread = new Thread(this);
    	readThread.start();
		return sb.toString();
	}
	
	
	public void run() {
    	try {
    		Thread.sleep(100);
    	} catch (InterruptedException e) {}
	 }
	
	public void serialEvent(SerialPortEvent event) {
		switch (event.getEventType()) {
			case SerialPortEvent.BI:
			case SerialPortEvent.OE:
			case SerialPortEvent.FE:
			case SerialPortEvent.PE:
			case SerialPortEvent.CD:
			case SerialPortEvent.CTS:
			case SerialPortEvent.DSR:
			case SerialPortEvent.RI:
			case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
				break;
			case SerialPortEvent.DATA_AVAILABLE:
				try {
					byte[] readBuffer = new byte[inputStream.available()];
					System.out.println(inputStream.available());
					while (inputStream.available() > 0) {
						int numBytes = inputStream.read(readBuffer);
						System.out.println(numBytes);
					}
					System.out.println(readBuffer);
					System.out.println(readBuffer.toString());
					 msg = Char2HexString(readBuffer);
					 System.out.println(msg);
					 sb.append(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
		
	}
	
	public void send(String content){
		try {
			System.out.println(HexString2Bytes(content).toString());
			outputStream.write(HexString2Bytes(content));
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String read(){
		return sb.toString();
	}
	
	public void close(){
		if (serialPort != null) {
			serialPort.close();
		}
	}
	public static String Char2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}	
	
	public static byte[] HexString2Bytes(String src){
	    byte[] ret = new byte[src.length()/2];
	    byte[] tmp = src.getBytes();
	    for(int i=0; i<src.length()/2; i++){
	      ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
	    }
	    return ret;
	}
	public static byte uniteBytes(byte src0, byte src1) {
	    byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
	    _b0 = (byte)(_b0 << 4);
	    byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
	    byte ret = (byte)(_b0 ^ _b1);
	    return ret;
	}
}

]

你可能感兴趣的:(thread)