Code Monkey home page Code Monkey logo

moka7-live's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.