package com.jwk.spring.boot.autoconfigure;

import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
import com.jwk.spring.boot.modbus4j.ModbusRtuMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;


/**
 * @author JWK
 * @version 1.0
 * @date 2022/9/7 17:00
 */
public class ModbusRtuMasterTemplate {


    private ModbusRtuMaster modbusRtuMaster;
    private ModbusMasterUtil modbusMasterUtil;


    /**
     * 初始化串口连接
     *
     * @param port
     * @param baudRate
     * @param dataBits
     * @param stopBits
     * @param parity
     * @param flowControlIn
     * @param flowControlOut
     * @throws ModbusInitException
     */
    public ModbusRtuMasterTemplate(String port, int baudRate, int dataBits, int stopBits, int parity
            , int flowControlIn, int flowControlOut) {
        modbusRtuMaster = new ModbusRtuMaster(port, baudRate, dataBits, stopBits, parity
                , flowControlIn, flowControlOut);
        modbusRtuMaster.connectAsync();
        modbusMasterUtil = modbusRtuMaster.getModbusMasterUtil();
    }

    public ModbusMasterUtil getModbusMasterUtil() {
        return modbusMasterUtil;
    }

}
