Warm tip: This article is reproduced from stackoverflow.com, please click
modbus-tcp

How do I send signal to modbus over TCP/IP in java?

发布于 2020-03-27 10:20:42

I am working on a client application that is supposed to send a signal to modbus that opens and close a gate with ip address and non-changeable port number 502. The instruction say to create a win-socket client application and send command buffer. I don't understand how to send the command buffer. This is a snapshot of commands. Modbus Commands

I have created a Java socket client class but I don't know what message to send and what datatype to use to send a message. or am I supposed to use one of the modbus libraries to send a signal. Thank you!

                socket = new Socket("192.168.0.8, 502);
                OutputStream output = socket.getOutputStream();
                OutputStreamWriter writer = new OutputStreamWriter(output);
                BufferedWriter bw = new BufferedWriter(writer);

                short buffer = ??; // don't know what to send here

                //String sendMesage = buffer;
                bw.write(buffer);
                bw.flush();
                System.out.println("Message sent to the server: " + buffer);

                InputStream receive = socket.getInputStream();
                InputStreamReader reader = new InputStreamReader(receive);
                BufferedReader bufferedReader = new BufferedReader(reader);
Questioner
sanki
Viewed
19
Marcos G. 2019-07-12 14:45

Based on the code snippet you show on your question, one gets the impression you are attempting to write a Modbus client from scratch.

Modbus is a quite simple protocol but it would take quite an effort to write an debug a new client and, since the protocol is open, there is plenty of code already written, debugged and tested. See, for instance, here.

On what command to send it's not easy to tell if you don't mention what device, in particular, you're connecting to and what exactly you want to do with it. In a frequent scenario, you have a machine or a sensor in a remote area of your factory that is logging data from its sensors and reacting from that data in a certain manner. With Modbus, you can send requests for data to your machine (reading coils/bits or registers/numeric values) to monitor what it's doing and send commands to control it (writing Modbus coils or registers) from a distant control room with an HMI or any other kind of computer.

EDIT: Now that you have decided to use EasyModbus you are closer to what you want. But it seems that what you're after is reading data from your device, so you don't need to write registers. You can try with this snippet (source):

public static void main(String[] args) 
    {
        ModbusClient modbusClient = new ModbusClient("127.0.0.1", 1536);
        try
        {
            modbusClient.Connect();
            //Read Int value from register 0 (Barrier Command)
            System.out.println(modbusClient.ReadHoldingRegisters(0, 1));
            //Read Float Value from Register 1 and 2 (Barrier Status)
            System.out.println(ModbusClient.ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(1, 2)));

        }
        catch (Exception e)
        {
        System.out.println(e.toString());
        }   
    }

If you look at the Modbus address map from your device (I took it from this manual, assuming that one is your device):

enter image description here

One important thing to note is that you have both 16 bit and 32 bit values on your device. Since Modbus registers are 16 bit, for 32 bit data types you need to read two registers. This only applies to register number 1 (Barrier Status according to the mapping above). For all others you can read and display as integer values.