Java实现串口通讯

推荐两个个简单好用java实现基于modbus—rtu协议通讯方法:

目录

  • 标准485协议
  • 非标准协议的串口通讯

关于modbus的java开源库主要有以下四种:

  • Jamod:Java Modbus实现:Java Modbus库。该库由Dieter Wimberger实施。
  • ModbusPal:ModbusPal是一个正在进行的Java项目,用于创建逼真的Modbus从站模拟器。由于预定义的数学函数和/或Python脚本,寄存器值是动态生成的。ModbusPal依赖于RxTx进行串行通信,而Jython则依赖于脚本支持。
  • Modbus4J:Serotonin Software用Java编写的Modbus协议的高性能且易于使用的实现。支持ASCII,RTU,TCP和UDP传输作为从站或主站,自动请求分区,响应数据类型解析和节点扫描。
  • JLibModbus:JLibModbus是java语言中Modbus协议的一种实现。jSSC和RXTX用于通过串行端口进行通信。该库是一个经过积极测试和改进的项目。

标准485协议

如果是采用标准modbus协议数据通讯则可采用如下方法:

jar包选择

        
            com.intelligt.modbus
            jlibmodbus
            1.2.9.7
        
  • 源码地址1:https://sourceforge.net/projects/jlibmodbus/
  • 源码地址2:https://github.com/kochedykov/jlibmodbus

代码:

package com.yixinhong.modbus;

import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.serial.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author:lpj
 * @Package:com.yixinhong.modbus
 * @Project:BoxDataProcess
 * @name:ModbusRTU
 * @Date:2023/5/22 10:22
 * @Filename:ModbusRTU
 *
 *
 */
@Slf4j
@Service
public class ModbusRTU {


    /**
     * 初始化
     */
    public ModbusMaster init(String SerialPort) {
        SerialParameters sp = new SerialParameters();
        sp.setDevice(SerialPort);
        sp.setBaudRate(com.intelligt.modbus.jlibmodbus.serial.SerialPort.BaudRate.BAUD_RATE_9600);
//        SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC());
//        SerialUtils.setSerialPortFactory(new SerialPortFactoryRXTX());
//        SerialUtils.setSerialPortFactory(new SerialPortFactoryJavaComm());
        SerialUtils.setSerialPortFactory(new SerialPortFactoryPJC());
        ModbusMaster m = null;
        try {
            m = ModbusMasterFactory.createModbusMasterRTU(sp);
            log.error("初始化成功啦");
        } catch (SerialPortException e) {
            log.error("初始化RTU失败"+e.getMessage());
            e.printStackTrace();
        }
        return m;
    }


    /**
     *
     * @param slaveId  从机地址
     * @param quantity 读取寄存器数量
     */
    public int[] read(String SerialPort,Integer slaveId,Integer quantity ){
        ModbusMaster init = init(SerialPort);
//        int slaveId = 1;
        int offset = 0;
//        int quantity = 2;
        int[] registerValues = new int[quantity];
        try {
            init.connect();
            log.error("RTU连接成功啦");
            registerValues = init.readHoldingRegisters(slaveId, offset, quantity);
        } catch (ModbusIOException e) {
            log.error("RTU连接异常~"+e.getMessage()+e.getLocalizedMessage());
            e.printStackTrace();
        } catch (ModbusNumberException e) {
            log.error("数据读取失败~"+e.getMessage());
            e.printStackTrace();
        } catch (ModbusProtocolException e) {
            log.error("数据读取失败~" + e.getMessage());
            e.printStackTrace();
        }catch (Exception exception)
        {
            log.error("数据读取失败~" + exception.getMessage());
        } finally {
            try {
                init.disconnect();
            } catch (ModbusIOException e) {
                log.error("RTU断开异常~"+e.getMessage());
                e.printStackTrace();
            }
        }
        return registerValues;
    }
}

测试代码(读取温湿度传感器)
Java实现串口通讯_第1张图片

非标准协议的串口通讯

首先引入jar包

        
            com.github.purejavacomm
            purejavacomm
            1.0.1.RELEASE
        

其他的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; i0 && 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;

你可能感兴趣的:(java,开发语言,经验分享)