麒麟linux + java + JSSC 串口通信

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;

}

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. ASCII 編碼 ASCII(American Standard Code for Information ...
    s酸菜閱讀 8,713評論 0 8
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周書達閱讀 728評論 0 0
  • Java經典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 1,894評論 0 2
  • 2 幻燈片內容準備的3個步驟 內容是一份幻燈片的靈魂所在,是幻燈片制作過程中,最需要打磨的一部分,也是決定一份幻燈...
    無天下閱讀 510評論 0 0
  • 時間過得真快!不知不覺己進群學習兩個月了,期間見證了自己調和心態得到親子關系的融洽,女兒一周比一周有越來越大的變化...
    周麗1閱讀 283評論 1 4