Java Modbus(jamod)访问

时间:2016-03-22 21:21:37

标签: java iot modbus

我正在尝试使用jamod库来访问支持modbus的设备。 我想根据modbus-profile设置一个特定的寄存器(40149) 期望的数据类型的设备必须是带符号的32位int。 我已经设置了以下代码(基于https://github.com/jeick/jamod/blob/master/src/main/java/net/wimpi/modbus/cmd/TCPMasterTest.java),但它不起作用。登记册 是只写,但设备有一个显示器,我可以看到寄存器值。 所以一个modbus寄存器是16位(根据器件的modbus-profile)我发送了一个两个长度的数组,第一个元素是实际所需值20000,第二个元素设置为0。

private static int requestNumber = 7;
public static void main(String[] args) {
    int port = Modbus.DEFAULT_PORT;
    int unitId = 3; //Same as TCPSlaveTest.java
    try {
        if (args != null && args.length == 1) {
            port = Integer.parseInt(args[0]);
        }
        //InetAddress addy = InetAddress.getLocalHost();
        InetAddress addy = InetAddress.getByName("192.168.2.11");
        TCPMasterConnection connection = new TCPMasterConnection(addy);
        connection.setTimeout(3000);
        connection.setPort(port);
        System.out.println("Trying to connect to "+addy.getCanonicalHostName()+" on port "+port);
        connection.connect();

        ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);

        ModbusRequest request;
        while ((request = getNextRequest()) != null) {
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            ModbusResponse response = transaction.getResponse();
            gotResponse(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void gotResponse(ModbusResponse response) {
    System.out.println("Got response: "+ response.getHexMessage());
}

private static ModbusRequest getNextRequest() {
    //Note: simple process image uses 0-based register addresses
    switch (requestNumber) {
        case 0:
            return new WriteCoilRequest(0,true);
        case 1:
            return new ReadCoilsRequest(0, 2);
        case 2:
            return new ReadInputDiscretesRequest(0,4);
        case 3:
            return new ReadInputRegistersRequest(0,1);
        case 4:
            return new ReadMultipleRegistersRequest(30052,2);
        case 5:
            Register r = ModbusCoupler.getReference().getProcessImageFactory().createRegister();
            r.setValue(420);
            return new WriteSingleRegisterRequest(0,r);
        case 6:
            return new ReadMultipleRegistersRequest(30051,2);
        case 7:
            SimpleRegister[] registers = new SimpleRegister[2];
            registers[0] = new SimpleRegister(0);
            registers[1] = new SimpleRegister(2000);
            return new WriteMultipleRegistersRequest(40149,registers);
        default:
            return null;
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

将值写入寄存器并不是一个复杂的过程,我以前使用的方法是。

import java.net.InetAddress;
import java.net.UnknownHostException;

import net.wimpi.modbus.ModbusException;
import net.wimpi.modbus.ModbusIOException;
import net.wimpi.modbus.ModbusSlaveException;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.msg.WriteSingleRegisterRequest;
import net.wimpi.modbus.net.TCPMasterConnection;
import net.wimpi.modbus.procimg.SimpleRegister;

public class WriteToRegister {

    TCPMasterConnection con = null; //the connection
    ModbusTCPTransaction trans = null; //the transaction
    ReadInputDiscretesRequest req = null; //the request
    ReadInputDiscretesResponse res = null; //the response

    WriteToRegister( String ipaddress , int port ) throws UnknownHostException {

        InetAddress ipaddr = null;

        ipaddr = InetAddress.getByName(ipaddress);

        con = new TCPMasterConnection(ipaddr);
        con.setPort(port);
        System.out.println("Making connection to Plc..");

        try {
            con.connect();
        } catch (Exception e) {
            System.err.println("Connection successful");
            e.printStackTrace();
        }
    }

    public void writeRegister(int regaddr, int value) throws ModbusIOException, ModbusSlaveException, ModbusException {

        WriteSingleRegisterRequest WriteReq = null; 
        SimpleRegister MyReg = new SimpleRegister(1);

        //3. Prepare the request
        WriteReq = new WriteSingleRegisterRequest();
        WriteReq.setReference(regaddr);  //register number
        MyReg.setValue(value);         //value for register
        WriteReq.setRegister(MyReg);

        //4. Prepare the transaction

        trans = new ModbusTCPTransaction(con);
        trans.setRequest(WriteReq);

        trans.execute();

    }

    public static void main(String[] args) throws UnknownHostException, ModbusIOException, ModbusSlaveException, ModbusException {

        WriteToRegister w = new WriteToRegister("192.168.0.34",502);
        // Make sure you insert register value in hexadecimal 
        // and value should be in range of -32,768 to 32,767 
        w.writeRegister(0x1000, 255);

    }

}

希望它有所帮助!适合我。