keysuke
keysuke

Reputation: 97

How to connect to plc and get data

i need to embed a OPC server to a eclipse RCP application but i don't know where to begin. I'm using the opcua4j as example to create the server, but can't figure out how to connect to the remote PLC device.

My question is how to communicate with a remote client from server using OPC, given the client's ip address?

Ps: What is the best tutorial/book about opc ua java programming?

Upvotes: 1

Views: 12016

Answers (2)

vonGohren
vonGohren

Reputation: 949

You may try to use a evaluation verison of the ProSys OPC Java SDK. This is a pretty simple SDK and it provides you with a tutorial on how to create a server and a client from scratch. Following is a code on how simple it is to create a server with no addresse space in it:

private String APPNAME = "yourappname";
private String SERVER = "nameoftheserver";
private int PORT = 52000; #choose whatever

public void CreateOPCUAserver() {
    server = new UaServer();

    final PkiFileBasedCertificateValidator validator = new PkiFileBasedCertificateValidator();
    server.setCertificateValidator(validator);
    validator.setValidationListener(validationListener);

    ApplicationDescription appDescription = new ApplicationDescription();
    appDescription.setApplicationName(new LocalizedText(APPNAME, Locale.ENGLISH));
    appDescription.setApplicationUri("urn:localhost:UA:"+SERVER);
    appDescription.setProductUri("urn:snorre.com:UA:"+SERVER);
    appDescription.setApplicationType(ApplicationType.Server);
    try {
        server.setPort(PORT);
        server.setUseLocalhost(true);
        server.setUseAllIpAddresses(true);
        server.setServerName("OPCUA/"+APPNAME);

        final ApplicationIdentity identity = ApplicationIdentity.loadOrCreateCertificate(appDescription, "NTNU",
                /* Private Key Password */"opcua",
                /* Key File Path */new File(validator.getBaseDir(), "private"),
                /* Enable renewing the certificate */true,
                /* Additional host names for the certificate */server.getHostNames());

        server.setApplicationIdentity(identity);
        server.setSecurityModes(SecurityMode.ALL);
        server.addUserTokenPolicy(UserTokenPolicy.ANONYMOUS);
        server.addUserTokenPolicy(UserTokenPolicy.SECURE_USERNAME_PASSWORD);
        server.addUserTokenPolicy(UserTokenPolicy.SECURE_CERTIFICATE);


        server.init();
            String endpoint = String.format("opc.tcp://%s:%s/OPCUA/%s","localhost", PORT, APPNAME);
            server.addEndpoint(endpoint, SecurityMode.NONE, UserTokenPolicy.ANONYMOUS);  
        server.start();

        logger.info("*********important parameters for event transmission**********");
        logger.info("Servername "+endpoint);
        logger.info("Publishing interval: " + server.getSubscriptionManager().getMaxPublishingInterval());
        logger.info("Retransmition queue size: " + server.getSubscriptionManager().getMaxRetransmissionQueueSize());
        logger.info("Session timeout: " + server.getSessionManager().getMaxSessionTimeout());
        logger.info(logger.getClass().getCanonicalName());
        logger.info("****************************end ********************");

    } catch (UaServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecureIdentityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

You also have to create a validation listener which is pretty basic. Just to get it to run

public class MyCertificateValidationListener implements CertificateValidationListener {
    @Override
    public ValidationResult onValidate(Cert certificate, ApplicationDescription applicationDescription, EnumSet<CertificateCheck> passedChecks) {
        // Do not mind about URI...
        if (passedChecks.containsAll(EnumSet.of(CertificateCheck.Trusted, CertificateCheck.Validity, CertificateCheck.Signature))) {
            if (!passedChecks.contains(CertificateCheck.Uri)) {
                try {
                    System.out.println("Client's ApplicationURI (" + applicationDescription.getApplicationUri()  + ") does not match the one in certificate: " + PkiFileBasedCertificateValidator.getApplicationUriOfCertificate(certificate));
                } catch (CertificateParsingException e) {
                    throw new RuntimeException(e);
                }
            }
            return ValidationResult.AcceptPermanently;
        }
        return ValidationResult.Reject;
    }
}

This does not have any authentication, but it helps you get on the way. Now you can use some free client program to connect to your server and browse. You will see that ther are some folders there, but that is because the server comes with those folders intially.

Now the server is up and running, you have to create an AddresseSpace, then a data model of the PLC to add to your current server. Then this data model has a connection to your PLC and its possible methods, giving OPC-UA clients to connect to your server basend on a simple uri like this "opc.tcp://localhost:52000/OPCUA/servername" or http. This is you run it locally. Else it has to be an IP on this.

When clients then browse this they can see the variables, methods events alarms and so on that you have created within your datamodel. And the can use the methods and alot of different cool stuff.

Upvotes: 1

James
James

Reputation: 962

This may give you a run down on OPC servers work, I'm not too sure how much you know, so sorry if I relay. A OPC Server polls rather than a request/reply style of communication. It polls for tags rather than devices.

Check this link for Java - OPC Deve - http://www.opcconnect.com/java.php#javacom

OPC 101

You will need to develop:

  • Channel
  • Device
  • Variables

Each channel specifies two things. The two aspects being specified are the : 1. Physical medium (Network adapter such as your standard Ethernet device or a Serial port) 2. Protocol being used (MODBUS, Allen Bradley, GE-Fanuc etc). This means each channel cannot host devices on either different mediums or protocols.

The Device This field specifies a few things. These being:

  1. Address. This can be an IP address (for Ethernet passed protocols – such as IDEC Serial Encapsulated Protocol), or an identification number for serial communications such as Profibus or Modbus ASCII/RTU.
  2. Driver Specific information - This may include specifying a CPU model, or I/O card being used. This varies among drivers.

The Variables are where the “Tags” are defined.

  1. Tag Name
  2. PLC Address. Such as M1 or RW1 or 49075 (Driver specific though)
  3. Data Type – Boolean, Word etc.

Each tag or process variable has both data and meta-data. This can includes variable, last updated, the health of the tag etc, engineering units.

Finally

I haven't actually programmed my own OPC Server, but the above might help. I am not sure how much that Java SDK above gives, but consider security in your application.

Is it possible to run an OPC server, polling the data, then your application interfaces with the OPC Server? This could help with not having to mess (too much, at least) with OPC. You can get Java based OPC servers than run on any platform and I'm confident most have an API, or at least a way to communicate the data.

Edit

Check out the software package Pi (http://www.osisoft.com/). That might help with this.

Upvotes: 3

Related Questions