RXTXComm在使用时总是不正常,在使用JSSC后串口收发数据OK。
一、JSSC相关
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);//串口号;
serialPort.openPort();
serialPort.setParams(baudRate,8,1,0);
if(serialPort.isOpened()) {
System.out.println("打开串口:" + 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进制的方式读取串口返回数据
System.out.println("串口:" + serialPortEvent.getPortName() +",接收数据:" +serialPort.readHexString(serialPortEvent.getEventValue()));
}
Thread.sleep(50);
}catch (Exception e) {
e.printStackTrace();
}
}
}
});
return serialPort;
}
/**
*发送串口命令
*/
public static void sendData(SerialPort serialPort,byte[] bytes) {
if (serialPort !=null && serialPort.isOpened()) {
try{
serialPort.writeBytes(bytes);
System.out.print("---发送数据:");
for(byte item : bytes) {
System.out.print(item +" ");
}
System.out.println("---");
}catch (SerialPortException e) {
e.printStackTrace();
}
}
}
/**
* 字节转十六进制
* @param b 需要进行转换的byte字节
* @return 转换后的Hex字符串
*/
public static String byteToHex(byte b){
String hex = Integer.toHexString(b &0xFF);
if(hex.length() <2){
hex ="0" + hex;
}
return hex;
}
/**
* 字节数组转十六进制
* @param bytes 需要转换的byte数组
* @return 转换后的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字符串转byte
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
public static byte hexToByte(String inHex){
return (byte)Integer.parseInt(inHex,16);
}
/**
* hex字符串转byte数组
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public static byte[] hexToBytes(String inHex){
int hexlen = inHex.length();
byte[] result;
if (hexlen %2 ==1){
//奇数
hexlen++;
result =new byte[(hexlen/2)];
inHex="0"+inHex;
}else {
//偶数
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("打开串口失败");
}
if(serialPort !=null) {
JSSC.sendData(serialPort, res);
}
}
//byte 与 int 的相互转换
public static byte intToByte(int x) {
return (byte) x;
}
public static int byteToInt(byte b) {
//Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
return b &0xFF;
}
}