Modbus TCP library for Java

Hi All,

I want to view some variables with processing from a PLC in a LAN network,
I came across the modbus library for java while surfing the internet.
Is it possible for me to run the code in the link I added below in processing?
If possible, an example about it would be welcome.
I could not understand how to use this library, the processing is also done.
I would like to thank those who helped me in this matter.

Thanks
Halil

public class run {
public static void main(String args)
{
ModbusClient modbusClient = new ModbusClient(“127.0.0.1”,502);
try
{
modbusClient.Connect();
modbusClient.WriteSingleCoil(0, true);
modbusClient.WriteSingleRegister(0, 1234);
modbusClient.WriteMultipleRegisters(11, ModbusClient.ConvertFloatToTwoRegisters((float) 123.56));
System.out.println(modbusClient.ReadCoils(0, 1)[0]);
System.out.println(modbusClient.ReadHoldingRegisters(0, 1)[0]);
System.out.println(ModbusClient.ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(11, 2)));
}
catch (Exception e)
{
}
}
}

1 Like

First of all, is it possible to find and share the library file?

Hi GWAK,

You can download this link

Regards
Halil

1 Like

@herdal

import de.re.easymodbus.modbusclient.*;

void setup(){
  
  size(500,500);
    ModbusClient modbusClient = new ModbusClient("127.0.0.1",502);
    try
    {
      modbusClient.Connect();
      modbusClient.WriteSingleCoil(0, true);
      modbusClient.WriteSingleRegister(0, 1234);
      modbusClient.WriteMultipleRegisters(11, ModbusClient.ConvertFloatToTwoRegisters((float) 123.56));
      System.out.println(modbusClient.ReadCoils(0, 1)[0]);
      System.out.println(modbusClient.ReadHoldingRegisters(0, 1)[0]);
      System.out.println(ModbusClient.ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(11, 2)));
    }
    catch (Exception e)
    {    
    }  
    
    
}

a1
a2

Would you like to test by putting the ‘jar library’ in the ‘code folder’?

1 Like

@GWAK

I ran the program as you described, but I understand that it does not go beyond the “try” line.
At the same time, I am running Modbus master simulator on my computer.
I see that the program cannot send data to the simulator.

Regards

1 Like

@herdal


image
test video

Find and use the modbus slave program.
Then it will be solved.

1 Like

@GWAK

Thank you for helping me with this matter.
It works great.
Thank you very much.
:ok_hand:

1 Like

@herdal

fig1

If it was helpful, please click the ‘Solution’ button.

I have invested time for you.
have a good day.

2 Likes

@GWAK

I realize that you have devoted your precious time to my question, and I am grateful. I’d like to buy you coffee.

have a good day.

1 Like

@herdal

Dear herdal,

I am not a ‘Processing’ employee.
But what you asked is what I asked before.
How to use ‘JAVA library’

So, I posted a reply. I feel that I have to give because I have received something.

Thank you heart. I hope you have a nice day.

1 Like