java串口通讯分享

本文推荐一个简单好用java实现串口通讯方法
如果是非标准协议的串口通讯可以采用一下方法,如果是标准的modbus协议可以参考我的另外一篇博文:java实现modbus_rtu通讯

首先引入jar包

        <dependency>
            <groupId>com.github.purejavacomm</groupId>
            <artifactId>purejavacomm</artifactId>
            <version>1.0.1.RELEASE</version>
        </dependency>

其他的jar包或多或少还有点坑,我这暂不推荐,有兴趣的同学可以自行尝试,使用方法大致相同
废话不多说,上代码:直接引入工具类

package com.yixinhong.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Author:lpj
 * @name:CommUtil
 * @Date:2023/5/29 15:09
 * @describe:
 */
@Slf4j
public class CommUtil implements SerialPortEventListener {

    private String PORT_NAME ;
    private static final int BIT_RATE = 9600;
    public static final int DATA_BITS = SerialPort.DATABITS_8;
    public static final int STOP_BIT = SerialPort.STOPBITS_1;
    public static final int PARITY_BIT = SerialPort.PARITY_NONE;

    public static SerialPort serialPort;
    private static InputStream in;
    private static OutputStream out;
    private static CommUtil commUtil;

    // 保存串口返回信息
    public String data;
    // 保存串口返回信息十六进制
    public String dataHex;

    private CommUtil(String PORT_NAME) {
        this.PORT_NAME = PORT_NAME;
    }

    public static synchronized CommUtil getInstance(String PORT_NAME) {
        if (commUtil == null) {
            commUtil = new CommUtil(PORT_NAME);
            commUtil.init();
        }else if(!commUtil.PORT_NAME.equals(PORT_NAME))
        {
            commUtil = new CommUtil(PORT_NAME);
            commUtil.init();
        }
//        commUtil = new CommUtil(PORT_NAME);
//        commUtil.init();
        return commUtil;
    }

    public void init( ) {
        try {
            if(StringUtils.isEmpty(PORT_NAME)){
                log.error("init PORT_NAME is null");
            }else {
                log.info("init PORT_NAME is :{}",PORT_NAME);
            }
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(PORT_NAME);
            if (portIdentifier.isCurrentlyOwned()) {
                log.error("Port is currently in use");
            } else if (portIdentifier.getPortType() == 1) {
                serialPort = (SerialPort) portIdentifier.open(PORT_NAME, 1000);
                serialPort.setSerialPortParams(BIT_RATE, DATA_BITS, STOP_BIT, PARITY_BIT);

                in = serialPort.getInputStream();
                out = serialPort.getOutputStream();

                serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
            } else {
                log.error("Error: Only serial ports are handled by this example.");
            }
        } catch (Exception e) {
            log.error("init failed",e);
        }
    }

    @Override
    public void serialEvent(SerialPortEvent serialPortEvent) {
        switch (serialPortEvent.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE:
                receive();
                break;
        }
    }

    public void send(String message) {
        try {
            log.info("sendmsg:{}",message);
            byte[] bytes = hexStrToByteArray(message);
            out.write(bytes);
//            Thread.sleep(1000);
        } catch (Exception e) {
            log.error("send failed",e);
        }
    }

    public void receive() {
        try {
            in = serialPort.getInputStream();
            // 通过输入流对象的available方法获取数组字节长度
            byte[] readBuffer = new byte[in.available()];
            // 从线路上读取数据流
            int len = 0;
//            int read = in.read(readBuffer);
            while ((len = in.read(readBuffer)) != -1) {
                // 直接获取到的数据
                data = new String(readBuffer, 0, len).trim();
                // 转为十六进制数据
                dataHex = bytesToHexString(readBuffer);
//                System.out.println("data:" + data);
//                System.out.println("dataHex:" + dataHex);// 读取后置空流对象
                in.close();
                in = null;
                break;
            }
        } catch (IOException e) {
            System.out.println(("读取串口数据时发生IO异常"));
        }
    }


    public void close() {
        try {
            in.close();
            out.close();
            serialPort.notifyOnDataAvailable(false);
            serialPort.removeEventListener();
            serialPort.close();
        } catch (Exception e) {
            log.error("close",e);
        }
    }



    //16进制转byte数组
    public static byte[] hexStrToByteArray(String str) {
        if (str == null) {
            return null;
        }
        if (str.length() == 0) {
            return new byte[0];
        }
        byte[] byteArray = new byte[str.length() / 2];
        for (int i = 0; i < byteArray.length; i++) {
            String subStr = str.substring(2 * i, 2 * i + 2);
            byteArray[i] = ((byte) Integer.parseInt(subStr, 16));
        }
        return byteArray;
    }

    public static String ByteArrayToString(byte[] by) {
        String str = "";
        for (int i = 0; i < by.length; i++) {
            String hex = Integer.toHexString(by[i] & 0xFF);
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            str += hex.toUpperCase();
        }
        return str;
    }

    /**
     * 数组转换成十六进制字符串
     * @param
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 获取源数据和验证码的组合byte数组
     * @param aa 字节数组
     * @return
     */
    public static byte[] getData(byte[] aa) {
//        byte[] bb = getCrc16(aa);
        byte[] cc = new byte[aa.length];
        System.arraycopy(aa,0,cc,0,aa.length);
        return cc;
    }

    public static byte[] getData(String...strings) {
        byte[] data = new byte[]{};
        for (int i = 0; i<strings.length;i++) {
            int x = Integer.parseInt(strings[i], 16);
            byte n = (byte)x;
            byte[] buffer = new byte[data.length+1];
            byte[] aa = {n};
            System.arraycopy( data,0,buffer,0,data.length);
            System.arraycopy( aa,0,buffer,data.length,aa.length);
            data = buffer;
        }
        return getData(data);
    }

    public static String byteTo16String(byte b) {
        StringBuffer buffer = new StringBuffer();
        int aa = (int)b;
        if (aa<0) {
            buffer.append(Integer.toString(aa+256, 16)+" ");
        }else if (aa==0) {
            buffer.append("00 ");
        }else if (aa>0 && aa<=15) {
            buffer.append("0"+Integer.toString(aa, 16)+" ");
        }else if (aa>15) {
            buffer.append(Integer.toString(aa, 16)+" ");
        }
        return buffer.toString();
    }
}

解释:
在send方法中,发送数据后要等待1s的原因是,需要监听串口的应答数据,不等待应答数据就读取不到,如果是只考虑发送,不考虑应答的话可以不用等待。

调用代码示例

        String dataHex = null;
        try{
            CommUtil util = CommUtil.getInstance(commPortId);
            util.send("FF0101050F010118");
            Thread.sleep(1000);
            util.send("FF0101050F020119");
            dataHex = util.dataHex;
            CommUtil.serialPort.close();
        }catch (Exception e)
        {
            log.error("指令发送失败~"+e.getMessage());
        }
        return dataHex;
posted @ 2023-05-31 10:32  劉鵬杰  阅读(286)  评论(0编辑  收藏  举报