JavaFX – exchange data with Siemens PLC

How can exchange data between JavaFX application and Siemens PLC.

LibNoDave library downloaded resources contains java NoDave classes and java test classes. With java NoDave classes you can create required interface and connection type (MPI, PPI and ISO over TCP). Test classes are examples.

Add NoDave java classes in your NetBeans project  as shown on the image below in apparte directory “driver” and create own test class for example  “DataisoTCP”. NoDave.java is a library itself.

You still have to choose NoDave classes for required interface and connection type . On example below for  ISO over TCP connection need accessing following classes of the library (green cursors):

The “DataIsoTCP” class has functions for connect, disconnect and exchange data.

Usage of the functions connect, disconnect and exchange data:


package javafx2nodavetest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javafx2nodavetest.driver.Nodave;
import javafx2nodavetest.driver.PLCinterface;
import javafx2nodavetest.driver.TCPConnection;

public class DataIsoTCP {

  public static boolean Connection = false;
  public static char buf[];
  public static byte buf1[];
  public static PLCinterface di;
  public static TCPConnection dc;
  public static Socket sock;
  public static int slot;
  public static byte[] by;
  public static String IP;

  //IP 192.168.1.101
  DataIsoTCP(String host) {
    IP = host;
    //Nodave.Debug=Nodave.DEBUG_ALL;
    buf = new char[Nodave.OrderCodeSize];
    buf1 = new byte[Nodave.PartnerListSize];
    try {
     sock = new Socket(host, 102);
    } catch (IOException e1)
      System.out.println(e1);
    }
  }

  //Connect
  public static void StartConnection()
  Connection = false;
  OutputStream oStream = null;
  InputStream iStream = null;
  slot = 2;
  if (sock != null) {
    try {
      oStream = sock.getOutputStream();
    } catch (IOException e1) {
    }
    try {
      iStream = sock.getInputStream();
    } catch (IOException e1) {
    }
    di = new PLCinterface(oStream, iStream, "IF1", 0, Nodave.PROTOCOL_ISOTCP);
    dc = new TCPConnection(di, 0, slot);
    int res = dc.connectPLC();
      if (0 == res) {
        Connection = true;
        System.out.println(Connection OK);
      } else {
        System.out.println(No connection);
      }
    }
  }

  //Disconnect
  public static void Disconnection() {
    if (Connection == true) {
      Connection = false;
      dc.disconnectPLC();
      di.disconnectAdapter();
    }
  }

  //Aria: Nodave.OUTPUTS, Nodave.FLAGS, Nodave.INPUTS

  //write MB, IB, QB, DB.DBB
  public static void WriteData8(int area, int DBNum, int number, int a) {
    by = Nodave.bswap_8(a);
    dc.writeBytes(area, DBNum, number, 1, by);
  }

  //write MW, IW, QW, DB.DBW
  public static void WriteData16(int area, int DBNum, int number, int a) {
    by = Nodave.bswap_16(a);
    dc.writeBytes(area, DBNum, number, 2, by);
  }

  //write MD, ID, QD, DB.DBD
  public static void WriteData32(int area, int DBNum, int number, int a) {
    by = Nodave.bswap_32(a);
    dc.writeBytes(area, DBNum, number, 4, by);
  }

  //Write float MD, DB.DBD
  public static void WriteData32(int area, int DBNum, double a) {
    by = Nodave.toPLCfloat(a);
    dc.writeBytes(area, DBNum, number, 4, by)
  }

  //write M, I, Q, DB.DBX
  public static void WriteBinareData(int area, int DBNum, int number, int pos, boolean a) {
    byte b;
    if (Connection) {
      dc.readBytes(area, DBNum, number, 1, null);
      b = (byte) dc.getBYTE();
      if (a) {
        b = (byte) (b | (1 << pos));
      } else {
        b = (byte) (b &amp; ~(1 << pos));
      }
      by = Nodave.bswap_8((byte) b);
      dc.writeBytes(area, DBNum, number, 1, by);
    }
  }

  //read bytes
  public static String ReadData(int area, int DBNum, int number, int bytes {
    String tmp;
    if (Connection) {
      dc.readBytes(area, DBNum, number, bytes, null);
      switch (bytes) {
        case 1: {
          return Integer.toString(dc.getBYTE()); //IB, QB, MB, DB.DBB
        }
        case 2: {
          return Integer.toString(dc.getINT()); //IW, QW, MW, DB.DBW
        }
        case 4: {
          return Long.toString(dc.getU32()); //ID, QD, MD, DB.DBD
        }
        default: {
          return "unknown";
        }
      }
    } else {
      return "off-line";
    }
  }

  //Read timer T
  public static String ReadTimer(int number) {
    String tmp1, tmp2;
    if (Connection) {
      dc.readBytes(Nodave.TIMER, 0, number, 2, null);
      return Integer.toString(dc.getWORD());
    } else {
      return  "off-line";
    }
  }

  //Read counter C
  public static String ReadCounter(int number) {
    String tmp;
    if (Connection) {
      dc.readBytes(Nodave.COUNTER, 0, number, 2, null);
      return Integer.toString(dc.getWORD());
    } else {
      return "off-line";
    }
  }

  //Read M, I, Q, DB.DBX
  public static String ReadBinareData(int area, int DBNum, int number, int pos) {
    int tmp;
    if (Connection) {
      dc.readBytes(area, DBNum, number, 1, null);
      tmp = (dc.getBYTE()) &amp; (0x01 << pos);
      if (tmp &gt; 0) {
        return "true";
      } else {
        return "false";
      }
    } else {
      return "off-line";
    }
  }

  //Connect
  public static void Start(String adres) {
    //Nodave.Debug = Nodave.DEBUG_ALL ^ (Nodave.DEBUG_IFACE | Nodave.DEBUG_SPECIALCHARS);
    DataIsoTCP tp = new DataIsoTCP(adres);
    tp.StartConnection();
  }
}

Listing: debug connection.

JavaFxNoDaveTest- debug listing

Example: NetBeans 1.3 project with LibNoDave library:

JavaFX application makes connection with PLC via ISO on TCP protocol. If connection is successful, then timer periodically writes data to PLC, reads data from PLC and updates user interface of the application.

Java_source

JavaFX 2.0 – exchange data with Siemens PLC.

First impression is that JavaFX 2.0 is not compatible with JavaFX 1.3.

  • No more CustomNode class and extend group or region to create “custom nodes”. Instead of the nodes must use classes.
  • Use properties of classes to bind values to each other.  Not all variables from the API are “bindable”. It is more difficult to link with PLC.
  • No more function types…

The goal is try to exchange data between JavaFX 2.0 and Siemens PLC with LibNoDava library.
The first step is creating JavaFX 2.0 application. Then add the LibNoDave library files, in the example below in the separate directory. Add DataIsoTCP file from previous example. This class is designed for make connection with ISO over TCP protocol and exchange of data between the PLC and JavaFX application.

NetBeans73

The JavaFX 2.0 example is simple.

This application uses two buttons for connecting and disconnecting connection with PLC.

Switches, buttons en sliders to write data to inputs area and leds, alarms and other elements to visualize data from output area of PLC memory. The data are of different format boolean (M, I, Q),  analog input-output are integer 16bit (IW, QW) and integer 32bit and float (MD).

Java_launch Java_source

The variable table example:

TestJava2 Java_source

Advertisements

5 Responses to “JavaFX – exchange data with Siemens PLC”

  1. Charles Says:

    Hi Alex,

    I am trying to use your Java drivers to connect to a Siemens PLC. So far it is going well and i do not have any problems with the connection. However i am trying to read data from a huge data block 16 bytes at a time and displaying that on a table and it is very slow. Any suggestions on how i could speed this up?

  2. Robert Cer Says:

    Hi! I have a problem with debugging. In next line I have an error:

    dc = new TCPConnection(di, 0, slot);

    The error messages are:

    Error 1: The project was not built since its build path is incomplete. Cannot find the class file for javafxnodavetest.driver.PLCinterface. Fix the build path then try building this project
    Error 2: The type javafxnodavetest.driver.PLCinterface cannot be resolved. It is indirectly referenced from required .class files

    Does anyone has an idea what is the problem?

    Thanks,
    Robert

  3. lloyd Says:

    hi Alex,

    am writing an application in java which i want to send inputs and receive outputs from the PLC, how do i link the two?

  4. Sarabjot Singh Manku Says:

    I want to read data from S7 300 , cpu 313c 2dp , from MPI Port . Which Code I should refer to ?
    Please help !
    OR What changes I should make in the above code which is meant for TCP data exchange ?

    Thanks
    Sarabjot Singh Manku

  5. Sarabjot Singh Manku Says:

    Dear Mr. Alex Sentcha ,

    I got the file mentioned above by you against my query .
    Now the issue is :- I got one cable which is USB Siemens MPI Adapter instead of RS232 Siemens MPI Adapter ( as mentioned by you )
    Whether the cable I am having can serve the Pupose or I strictly need the cable referred by you ?
    Please Confirm!
    Thanks

Comments are closed.


%d bloggers like this: