Menu

Simple dynamic content in a slave?

2016-02-03
2016-02-03
  • James M Chan

    James M Chan - 2016-02-03

    UPDATE: this has been resolved. See following post.

    ORIGINAL POST

    Hi we're evaluating j2mod as a modbus library for to use in our product. I'm still trying to figure this out so please excuese any silly questions. What I've tried to do is implement the Register interface with this:

        public class SPTRegister implements Register {
    
            Random generator = new Random();
            int value = generator.nextInt();
            ...
    
            @Override
            public int getValue() {
                value = generator.nextInt(10);
                System.out.println("Value: "+value);
                return value;
            }
    
            @Override
            public byte[] toBytes() {
                //value = generator.nextInt(10);
                value = (int) System.currentTimeMillis();
                System.out.println("toBytes... value is: "+value);
                return ByteBuffer.allocate(4).putInt(value).array();
            }
    
        }
    

    and then i added my new register class to the TCPSlaveTest.java like this:

    spi.addRegister(50, new SPTRegister());
    

    But when I use my client to retrieve the value it does not seem to update and is always reporting the cached value while scanning. I'm testing with qModMaster and Radzio Modbus Simulator. I have to shutdown and reconnect to get an updated value which appears to be the next value instead of a recently updated value. So i'm confused shouldn't my implementation just return the latest value that was taken from toBytes()? When i run my client it continually scans and the server/slave appears to be calling toBytes repeaditly but of course as mentioned the value retrieved is unchanging unless i stop and reconnect.

    toBytes... value is: -1531314939
    toBytes... value is: -1531313921
    toBytes... value is: -1531312907
    toBytes... value is: -1531311892
    toBytes... value is: -1531310876
    toBytes... value is: -1531309860
    toBytes... value is: -1531308844
    ...
    

    With it as it is I'm not sure how to update any registers dynamically besides adding a new register to the SimpleProcessImage everytime the registers changes which seems very waistful . Any advice is would be very appreciated.

    A second question is with qmodmaster putting the app in scan mode gives me a

    Read data failed.
    System Exception. [Broken pipe]
    

    Error in red. Wondering if there is some special configuration i need to do to have the continuous scan work on qmodmaster. I have the default settings in the TCPSlaveTest.java

    --James

     

    Last edit: James M Chan 2016-02-10
  • James M Chan

    James M Chan - 2016-02-03

    UPDATE I got my issue resolved! My issues was that in my int variable that i was publishing over the protocol i was sending the MSBytes which was not changing frequently. When i adjusted the test code so the LSBytes were used the behavior was much more ideal in that I was seeing my data update by two as expected. On top of that I also extended the class SynchronizedAbstractRegister which is probably intended by the library developors.

            @Override
            public byte[] toBytes() {
                value++;
                value++;
                System.out.print("toBytes... value is: "+value+" ");
                byte [] my_buf = ByteBuffer.allocate(4).putInt(value).array();
                m_Register[0] = my_buf[2];
                m_Register[1] = my_buf[3];
                System.out.println("m_Register is: "+bytesToHex(m_Register));
    
                return m_Register;
            }
    
     

    Last edit: James M Chan 2016-02-10

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.