麒麟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,745评论 0 8
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 752评论 0 0
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,916评论 0 2
  • 2 幻灯片内容准备的3个步骤 内容是一份幻灯片的灵魂所在,是幻灯片制作过程中,最需要打磨的一部分,也是决定一份幻灯...
    无天下阅读 517评论 0 0
  • 时间过得真快!不知不觉己进群学习两个月了,期间见证了自己调和心态得到亲子关系的融洽,女儿一周比一周有越来越大的变化...
    周丽1阅读 286评论 1 4