Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:03:08+00:00 2026-06-12T12:03:08+00:00

I was able to make a connection with a PLC to read data from

  • 0

I was able to make a connection with a PLC to read data from it. Now there’s one problem and it is that I have to write a method to modify the data from the PLC. To achieve this, I have to send two values to the PLC: an int value and a boolean value. I got the int value solved via the classes from the net.wimpi.modbus package. But when it comes to the boolean value I have no clue what to do.

If someone had the same problem as I do now, could you please send me a reference where I can find a solution or a link of a really good tutorial to solve my problem? Someone posted a couple of links in this question but it sends me to tutorials that doesn’t have much to do with the communication with the PLC’s and how to treat the data of the PLC.

EDIT

I made the connection with a Modicon M340 PLC, and for the connection I use the classes of the net.wimpi.modbus package. I made the connection in my code through the classes ModbusTCPTransaction and TCPMasterConnection, and I read the values through the classes ReadMultipleRegistersRequest and ReadMultipleRegistersResponse.

The code I made for the connection:

    private InetAddress m_Address;
private ModbusTCPTransaction m_Transaction = null;
private TCPMasterConnection m_Connection = null;

int port = Modbus.DEFAULT_PORT;
private Activity activity;


public ModbusConnection(Activity activity, String ip)
{
    this.activity = activity;

    try {
        m_Address = InetAddress.getByName(ip); 
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // the slave'saddress
}

public void getTransaction(InetAddress inet) throws Exception
{
    /*Variables for the reading of the PLC data*/
    int port = Modbus.DEFAULT_PORT;

    /*Data initialization for the reading of the PLC data*/
    m_Connection = new TCPMasterConnection(inet);
    m_Connection.setPort(port);
    m_Connection.connect();
    m_Transaction = new ModbusTCPTransaction(m_Connection);
}

And to read the values, I call the next code all the time. I accomplished only reading and writing int, String and float values through words that I read from an offset declared on the PLC:

    private ReadMultipleRegistersResponse readValues(int offset, int count) 
{
    ReadMultipleRegistersRequest rReq = null; // the request
    ReadMultipleRegistersResponse rRes = null; // the response

    try {

        rReq = new ReadMultipleRegistersRequest(offset, count);
        m_Transaction.setRequest(rReq);
        m_Transaction.execute();
        rRes = (ReadMultipleRegistersResponse) m_Transaction.getResponse();

    } catch (Exception e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: Exception");
    }
    return rRes;    
}

EDIT 2

I think I accomplished what I wanted. There are 4 classes that I use to read the coils:

ReadCoilsRequest
ReadCoilsResponse
WriteMultipleCoilsRequest
WriteMultileCoilsResponse

What I did is two methods to read and write coils into the PLC:

    private ReadCoilsResponse readBytes(int offset, int count) 
{
    ReadCoilsRequest rReq = null; // the request
    ReadCoilsResponse rRes = null; // the response

    try {

        rReq = new ReadCoilsRequest(offset, count);
        m_Transaction.setRequest(rReq);
        m_Transaction.execute();
        rRes = (ReadCoilsResponse) m_Transaction.getResponse();

    } catch (Exception e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: Exception");
    }
    return rRes;    
}

    public void writeBytes(int wordNumber, BitVector b) {
    try {               
        WriteMultipleCoilsRequest wReq = null; //
        WriteMultipleCoilsResponse wRes = null; //

        wReq = new WriteMultipleCoilsRequest(211, b);
        m_Transaction.setRequest(wReq);
        m_Transaction.execute();    
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: Exception");
    }
}

Also, I made a method to read BitVector variables using the Coils classes:

    public BitVector readBitVector(int offset, int count) 
{
    BitVector myBitVector;  

    ReadCoilsResponse rRes = readBytes(offset, count);
    rRes = (ReadCoilsResponse) m_Transaction.getResponse();
    myBitVector = rRes.getCoils();

    return myBitVector; 
}

After this, what I used to set the bit to 1 or 0 is using a native function from the BitVector class from the net.wimpi.modbus.util package in my code:

test.setBit(2, true);

NOTE: It is important to remember that everytime that you want to read or write values to the plc, the best way to do it is opening a closing the connection to the PLC.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:03:10+00:00Added an answer on June 12, 2026 at 12:03 pm

    My definitive answer is: you have to treat the registers as bits. So if you want to write the second bit of a register represented in an int value in your code, you will have to do something like this:

    intValue = m_Data[1].getValue();
    intValue = intValue | 2;
    m_Data[1].setValue(intValue);
    

    The second line modifies the bit I want to write in my PLC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel spreadsheet that needs to display data from our SQL database.
I have a thread that goes out and attempts to make a connection. In
I have been able to make my photos large for single photo posts on
I am not able to make a clear decision on this one. I am
I need to be able to make a program that looks through a mailbox
I have this method which tries to catch an exception that might be thrown
I have Java ME Application that uses HttpConnection API, Here I am easily able
I am able to successfully make web service requests however was curious how one
I´ve downloaded the UDPEcho sample code from Apple to make a simple UDP connection
I have a problem with my connection tunneling. So I'm using two ubuntu server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.