Skip to content

xtrinch/moka7-live

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central

moka7-live

This is a library built around moka7 created by Dave Nardella. Moka7 is is the Java port of Snap7 Client. It’s a pure Java implementation of the S7Protocol used to communicate with S7 PLCs.

Installation

Package can be installed via maven by adding the following to your pom.xml:

<dependency>
    <groupId>si.trina</groupId>
    <artifactId>moka7-live</artifactId>
    <version>0.0.11</version>
</dependency>

How to use

1. Create classes that implement interface PLCListener

2. Create PLC class instances for every PLC you wish to receive bit changes / read integers, bits from

import si.trina.moka7.live.PLC;
import com.sourceforge.snap7.moka7.S7;

/*
    args: 
        ** name of PLC
        ** IP of PLC
        ** byte array with length of db PLC->PC
        ** byte array with length of PC->PLC
        ** db (DataBase) number PLC->PC
        ** db (DataBase) number PC->PLC
        ** array of addresses of booleans to listen to changes to
*/
PLC plc1 = new PLC("Test PLC1","10.10.21.10",new byte[32],new byte[36],112,114,new double[]{0.1,0.2});


/*
    args: 
        ** name of PLC
        ** IP of PLC
        ** length of db PLC->PC
        ** length of PC->PLC
        ** db (DataBase) number PLC->PC
        ** db (DataBase) number PC->PLC
        ** array of addresses of booleans to listen to changes to
        ** rack number
        ** slot number
        ** area type of PLC->PC
        ** area type of PC->PLC
*/
PLC plc2 = new PLC("Test PLC2", "10.10.22.10", 18, 22, 45, 44, new double[]{0.1,0.2,0.3}, 0, 1, S7.AreaDB, S7AreaDB); 

3. Add classes that implement interface PLCListener to PLC's ArrayList<PLCListener> listener array

PLCListenerImplementation myListener = new PLCListenerImplementation();
plc1.listeners.add(myListener);
plc2.listeners.add(myListener);

4. Start a thread for each PLC instance

Thread t1 = new Thread(plc1).start();
Thread t2 = new Thread(plc2).start();

5. Receive bit changes from bits at addresses from last argument of PLC constructor

import si.trina.moka7.live.PLCListener;

public class PLCListenerImplementation implements PLCListener {
    @Override
    public void PLCBitChanged(int address, int pos, boolean val, String plcName) {
        switch (address) {
        case 0:
            switch (pos) {
            case 1:
                System.out.println("Bit at address 0.1 of PLC " + plcName + " changed to: " + val);
            }
        }
    }
}

6. Write shorts/integers/booleans to DB

/*
    args: 
        ** database to write to: from plc = true, from pc = false
        ** address to write to
        ** short/integer to write to db
*/
plc1.putInt(false, 12, (short)3);
plc1.putDInt(false, 12, 3);

/*
    args:
        ** database to write to: from plc = true, from pc = false
        ** address to write to
        ** bit offset at address to write to
        ** value to write
*/
plc1.putBool(false, 0, 1, true);
plc1.signalBoolean(false, 0, 1, true); // resets to false after 300ms

7. Read shorts/integers/booleans from DB

try {
    short aShort = plc1.getInt(true, 8); // 2 bytes
    int anInteger = plc1.getDInt(true, 8); // 4 bytes
    boolean aBoolean = plc1.getBool(true, 0, 2);
} catch (Exception e) { 
    e.printStackTrace(); 
}

Optional

Check communication status

Communication status can optionally be continuously checked with the help of a 'live bit'.

The following example settings set bit at address 0.0 in both DB's as the 'live bit', meaning it toggles it every 250ms and expects PLC to toggle it back every 500ms. Throws exception if it doesn't.

    plc1.liveBitEnabled = true;
    plc1.liveBitAddress = 0;
    plc1.liveBitPosition = 0;
    plc1.liveBitPCDuration = 250;
    plc1.liveBitPLCDuration = 500;

Misc

PLC connection status

Boolean indicating whether a PLC is connected or not can be found in PLC class' public variable connected.

Logging

All logging with various priorities inside the library is done with slf4j. Meaning, you need a logger binding (for example slf4j-simple) to see logs.