项目作者: xtrinch

项目描述 :
用于Java的S7 PLC通信库,基于Moka7
高级语言: Java
项目地址: git://github.com/xtrinch/moka7-live.git
创建时间: 2017-06-06T10:09:21Z
项目社区:https://github.com/xtrinch/moka7-live

开源协议:Other

下载


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:

  1. <dependency>
  2. <groupId>si.trina</groupId>
  3. <artifactId>moka7-live</artifactId>
  4. <version>0.0.11</version>
  5. </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

  1. import si.trina.moka7.live.PLC;
  2. import com.sourceforge.snap7.moka7.S7;
  3. /*
  4. args:
  5. ** name of PLC
  6. ** IP of PLC
  7. ** byte array with length of db PLC->PC
  8. ** byte array with length of PC->PLC
  9. ** db (DataBase) number PLC->PC
  10. ** db (DataBase) number PC->PLC
  11. ** array of addresses of booleans to listen to changes to
  12. */
  13. PLC plc1 = new PLC("Test PLC1","10.10.21.10",new byte[32],new byte[36],112,114,new double[]{0.1,0.2});
  14. /*
  15. args:
  16. ** name of PLC
  17. ** IP of PLC
  18. ** length of db PLC->PC
  19. ** length of PC->PLC
  20. ** db (DataBase) number PLC->PC
  21. ** db (DataBase) number PC->PLC
  22. ** array of addresses of booleans to listen to changes to
  23. ** rack number
  24. ** slot number
  25. ** area type of PLC->PC
  26. ** area type of PC->PLC
  27. */
  28. 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

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

4. Start a thread for each PLC instance

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

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

  1. import si.trina.moka7.live.PLCListener;
  2. public class PLCListenerImplementation implements PLCListener {
  3. @Override
  4. public void PLCBitChanged(int address, int pos, boolean val, String plcName) {
  5. switch (address) {
  6. case 0:
  7. switch (pos) {
  8. case 1:
  9. System.out.println("Bit at address 0.1 of PLC " + plcName + " changed to: " + val);
  10. }
  11. }
  12. }
  13. }

6. Write shorts/integers/booleans to DB

  1. /*
  2. args:
  3. ** database to write to: from plc = true, from pc = false
  4. ** address to write to
  5. ** short/integer to write to db
  6. */
  7. plc1.putInt(false, 12, (short)3);
  8. plc1.putDInt(false, 12, 3);
  9. /*
  10. args:
  11. ** database to write to: from plc = true, from pc = false
  12. ** address to write to
  13. ** bit offset at address to write to
  14. ** value to write
  15. */
  16. plc1.putBool(false, 0, 1, true);
  17. plc1.signalBoolean(false, 0, 1, true); // resets to false after 300ms

7. Read shorts/integers/booleans from DB

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

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.

  1. plc1.liveBitEnabled = true;
  2. plc1.liveBitAddress = 0;
  3. plc1.liveBitPosition = 0;
  4. plc1.liveBitPCDuration = 250;
  5. 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.