# Modbus TCP library for Java

**URL:** <https://discourse.processing.org/t/modbus-tcp-library-for-java/39017>\
**Category:** Libraries\
**Created:** [September 30, 2022, 5:44pm UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017 "2022-09-30T17:44:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![herdal](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@herdal](https://discourse.processing.org/u/herdal)\
**Post date:** [September 30, 2022, 5:44pm UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/1 "2022-09-30T17:44:57Z")

</div>

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

> **[Java Modbus Client - Pupli](https://pupli.net/2018/06/java-modbus-client/)**
>
> Modbus TCP Client Example 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);...

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)  
{  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 1, 2022, 11:21am UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/2 "2022-10-01T11:21:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![herdal](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@herdal](https://discourse.processing.org/u/herdal)\
**Post date:** [October 1, 2022, 5:00pm UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/3 "2022-10-01T17:00:36Z")

</div>

Hi GWAK,

> **[EasyModbusTCP/UDP/RTU .java](https://sourceforge.net/projects/easymodbustcp-udp-java/)**
>
> Download EasyModbusTCP/UDP/RTU .java for free. Modbus TCP, Modbus UDP and Modbus RTU client/server Java library. Modbus TCP, Modbus UDP and Modbus RTU client/server library and Simulators for JAVA
> 
> Suitable for data exchange between JAVA-Applications...

You can download this link

Regards  
Halil

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 2, 2022, 1:04am UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/4 "2022-10-02T01:04:28Z")

</div>

@herdal

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/b/6b1a64562312ee43b428daaac540bad95b04f0f2.jpeg)  
 ![a2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/9/a9b16918ec69a33cd47598f4892a884049d2950e.jpeg)

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

---

<div class="post-metadata">

**Author:** ![herdal](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@herdal](https://discourse.processing.org/u/herdal)\
**Post date:** [October 2, 2022, 8:04am UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/5 "2022-10-02T08:04:17Z")

</div>

@GWAK

 ![Modbus](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/4/b4ff4b82cee768bac80cb7d668c7d478364d5dd9.png)

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

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 2, 2022, 8:55am UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/6 "2022-10-02T08:55:03Z")

</div>

@herdal

 ![fig](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/e/ce9b0d8d33811f7666ea2dbc8e8b4dade0e230e9.jpeg)  
image  
test video

[![](https://img.youtube.com/vi/xS5Kd7XDxgM/hqdefault.jpg "easymodbus tcp ip modbus client test") ](https://www.youtube.com/watch?v=xS5Kd7XDxgM)

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

---

<div class="post-metadata">

**Author:** ![herdal](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@herdal](https://discourse.processing.org/u/herdal)\
**Post date:** [October 2, 2022, 9:51am UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/7 "2022-10-02T09:51:02Z")

</div>

@GWAK

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

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 2, 2022, 12:32pm UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/8 "2022-10-02T12:32:55Z")

</div>

@herdal

![fig1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/a/5ad767226f5a8f92414ca4ad6787fc943386d932.jpeg)

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

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

---

<div class="post-metadata">

**Author:** ![herdal](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@herdal](https://discourse.processing.org/u/herdal)\
**Post date:** [October 2, 2022, 4:43pm UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/9 "2022-10-02T16:43:28Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 3, 2022, 10:39am UTC](https://discourse.processing.org/t/modbus-tcp-library-for-java/39017/10 "2022-10-03T10:39:10Z")

</div>

@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.
