package cn.iocoder.yudao.module.system.util.modbus3;


import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.locator.BaseLocator;

public class SerialPortMasterTest {
    public static void main(String[] args) throws ModbusTransportException, ErrorResponseException, ModbusInitException {
        //ModbusMaster master = ModbusUtils.getSerialPortRtuMaster("COM2", 9600, 8, 1, 0);
        //float v = ModbusUtils.readHoldingRegister(master, 1, 2, DataType.FOUR_BYTE_FLOAT).floatValue();
        //System.out.println(v);

//        ModbusMaster asciiMaster = ModbusUtils.getSerialPortAsciiMaster("COM2", 38400, 8, 1, 0);
        ModbusMaster asciiMaster = ModbusUtils.getMaster("192.168.10.234", 8899);
        /*float f = ModbusUtils.readHoldingRegister(asciiMaster, 1, 0, DataType.FOUR_BYTE_FLOAT).floatValue();
        System.out.println(f);*/

        //缩回去
        short[] arr = {1};

        //伸出来
//        short[] arr = {1};
        ModbusUtils.writeHoldingRegisters(asciiMaster,1,0,arr);
        asciiMaster.destroy();
    }

    public void query() {
        try {
            ModbusMaster master = ModbusUtils.getMaster("192.168.10.234", 8899);
            BaseLocator<Number> loc = BaseLocator.holdingRegister(1, 9, DataType.TWO_BYTE_INT_UNSIGNED);
            Number value = master.getValue(loc);
            byte b = value.byteValue();
            System.out.println("----------"+b);
            System.out.println("结果 "+ value.longValue());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}