RXTXComm在使用時(shí)總是不正常,在使用JSSC后串口收發(fā)數(shù)據(jù)OK猖吴。
一、JSSC相關(guān)
package com.demo;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class JSSC {
public static SerialPort openSerialPort(String portName,int baudRate)throws SerialPortException {
final SerialPort serialPort =new SerialPort(portName);//串口號(hào);
? ? ? ? serialPort.openPort();
serialPort.setParams(baudRate,8,1,0);
if(serialPort.isOpened()) {
System.out.println("打開(kāi)串口:" + serialPort.getPortName());
}
serialPort.addEventListener(new SerialPortEventListener() {
public void serialEvent(SerialPortEvent serialPortEvent) {
if (serialPortEvent.isRXCHAR()) {
try {
if (serialPortEvent.getEventValue() >0) {
byte[] bytes =serialPort.readBytes(serialPortEvent.getEventValue());
//以16進(jìn)制的方式讀取串口返回?cái)?shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("串口:" + serialPortEvent.getPortName() +"宽气,接收數(shù)據(jù):" +serialPort.readHexString(serialPortEvent.getEventValue()));
}
Thread.sleep(50);
}catch (Exception e) {
e.printStackTrace();
}
}
}
});
return serialPort;
}
/**
? ? *發(fā)送串口命令
? ? */
? ? public static void sendData(SerialPort serialPort,byte[] bytes) {
if (serialPort !=null && serialPort.isOpened()) {
try{
serialPort.writeBytes(bytes);
System.out.print("---發(fā)送數(shù)據(jù):");
for(byte item : bytes) {
System.out.print(item +" ");
}
System.out.println("---");
}catch (SerialPortException e) {
e.printStackTrace();
}
}
}
/**
? ? * 字節(jié)轉(zhuǎn)十六進(jìn)制
? ? * @param b 需要進(jìn)行轉(zhuǎn)換的byte字節(jié)
? ? * @return? 轉(zhuǎn)換后的Hex字符串
? ? */
? ? public static String byteToHex(byte b){
String hex = Integer.toHexString(b &0xFF);
if(hex.length() <2){
hex ="0" + hex;
}
return hex;
}
/**
? ? * 字節(jié)數(shù)組轉(zhuǎn)十六進(jìn)制
? ? * @param bytes 需要轉(zhuǎn)換的byte數(shù)組
? ? * @return? 轉(zhuǎn)換后的Hex字符串
? ? */
? ? public static String bytesToHex(byte[] bytes) {
StringBuffer sb =new StringBuffer();
for(int i =0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] &0xFF);
if(hex.length() <2){
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
/**
? ? * Hex字符串轉(zhuǎn)byte
? ? * @param inHex 待轉(zhuǎn)換的Hex字符串
? ? * @return? 轉(zhuǎn)換后的byte
*/
? ? public static byte hexToByte(String inHex){
return (byte)Integer.parseInt(inHex,16);
}
/**
? ? * hex字符串轉(zhuǎn)byte數(shù)組
? ? * @param inHex 待轉(zhuǎn)換的Hex字符串
? ? * @return? 轉(zhuǎn)換后的byte數(shù)組結(jié)果
? ? */
? ? public static byte[] hexToBytes(String inHex){
int hexlen = inHex.length();
byte[] result;
if (hexlen %2 ==1){
//奇數(shù)
? ? ? ? ? ? hexlen++;
result =new byte[(hexlen/2)];
inHex="0"+inHex;
}else {
//偶數(shù)
? ? ? ? ? ? result =new byte[(hexlen/2)];
}
int j=0;
for (int i =0; i < hexlen; i+=2){
result[j] =hexToByte(inHex.substring(i,i+2));
j++;
}
return result;
}
}
二佩憾、使用
package com.demo;
import jssc.SerialPort;
import jssc.SerialPortException;
public class JSSCMain {
public static void main(String[] args)throws InterruptedException {
byte a =intToByte(0x7e);
short b =1600;
byte c =intToByte(0xc4);
byte [] test =new byte[1024];
test[0] = a;
test[1] =intToByte(b%256);
test[2] =intToByte(b/256);
test[3] = c;
final byte[] res =new byte[4];
System.arraycopy(test,0, res,0,4);
SerialPort serialPort =null;
try{
serialPort = JSSC.openSerialPort("/dev/ttyS1",9600);
}catch (SerialPortException e) {
System.out.println("打開(kāi)串口失敗");
}
if(serialPort !=null) {
JSSC.sendData(serialPort, res);
}
}
//byte 與 int 的相互轉(zhuǎn)換
? ? public static byte intToByte(int x) {
return (byte) x;
}
public static int byteToInt(byte b) {
//Java 總是把 byte 當(dāng)做有符處理;我們可以通過(guò)將其和 0xFF 進(jìn)行二進(jìn)制與得到它的無(wú)符值
? ? ? ? return b &0xFF;
}
}