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;
}
}

]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值