Modbus:1. Java reads slave-side data (TCP) using Modbus

Source: Internet
Author: User

Recently hand on a Unity3d project, this project needs and real machine linkage, and the real machine linkage we need to pass the Modbus protocol. So modbus this piece of me to achieve, because the project is best packaged as Android, so Java to do, see the next Java there are really a lot of relevant libraries, and finally decided to use Jamod this library, address: Jamod

In the use of the process found that the Jamod re-encapsulation of certain objects is a single case, for example, I want to simulate a computer on a slave end, but this library is limited to the same computer in the same program can only simulate one, if I want to simulate a number of the need to make a number of programs come out, This is obviously not in line with the actual situation, so I re-in this library of the approximate whole TCP this piece of code, modified to be able to instantiate a program in a number of listeners come out and can be in the same program to bind the same IP under the multiple ports, so that the same program can be simulated more than one slave,

Because this project needs another program inside to simulate the slave end and the master side (no words AH), so here will write more, will write more detailed, first of all, will say how to read the data on the real machine device. As for the Modbus protocol, I do not make a statement here, do not understand that you can go to see the information, and then I write this essay, I have not received the real machine Modbus, so all the tests I was based on the simulator, if the discrepancy I will be in the revision of essays, the following began to record,

Simulator I use this group, this good provides the master side and slave end, address, you can download the relevant simulator according to their own needs, here first record read slave data, so here to download slave simulator,

This software is only the Windows version, so I am in the virtual machine inside the use, after downloading, directly installed on the line, this slave and pull end all need registration code, trial 30 days,

Let's start with the Java code.

Because of the project needs, so I put this piece of code alone, so that can be packaged into a jar file, this code I put on GitHub, address: Https://github.com/Dn9x/dn-modbus

This explains that because of the needs and requirements of the project, I deal with the complexity here, but the code to read the slave above is actually very simple, in the Modbusutil class,

If you simply use, then you completely only look at this one file is enough, others do not look, because here I also only introduce these, the other does not explain,

If you need to read the data on the slave device, the data on the slave device can be divided into four types, as in the simulator:

My personal understanding is that:

    • Boolean type that can read and write (0x)
    • Read-only Boolean type (1x)
    • Read-only numeric type (3x)
    • Number types that can be read and written (4x)

Jamod also provides operations for each of the different types using different classes, here I make a list

    • Boolean type (0x) Request class that can read and write:readcoilsrequest Response class:readcoilsresponse
    • Read-only Boolean type (1x) Request Class: Readinputdiscretesrequest Response class: Readinputdiscretesresponse
    • Read-only numeric type (3x) Request class:readinputregistersrequest Response class:readinputregistersresponse
    • Number types that can be read and written (4x) Request class:readmultipleregistersrequest Response class:readmultipleregistersresponse

Write classes are corresponding, but only can write to have the corresponding class, the four read class in use when the basic is consistent, no matter what kind of re-read time three value must be very important, one is SlaveID, here set the value:

Be sure to correspond with the simulator,

If not, the exception should be thrown.

The second one is the corresponding address of each register must correspond, here:

And here corresponds

As for the program in the IP and port is better processing, IP on the machine with the simulator IP on the line, the port default is 502, you can also modify, Jamod also provides the default port,

1 modbus.default_port;

Port designation:

Simulator aspect:

This can be read to the value above the emulator, when reading, the simulator connection must be opened, so as to read the value,

Because my project is the program only the controller, each controller has a lot of registers, so my simulator opened a lot of registers, SlaveID are 3, just register the address and type of different,

Such as:

After the setup is complete, open the connection above the emulator, and the red "No connection" will disappear.

I have a bit of a messy record here, but I have recorded these for the convenience of my own at any time to view, and mainly I have to look at the code, if you do not understand can see the code at any time.

Modbus:1. Java reads slave-side data (TCP) using Modbus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.