全网最新 java 开发 OPC 实现 DA 通讯控制点位 西门子

OPC SERVER软件使用
MatrikonOPC: 使用Matrikon OPC Server Simulation
KEPServer V6: 使用KEPServerEX 6
安装教程参考: https://www.cnblogs.com/ioufev/p/9366426.html
需要的依赖

   <!-- OPC-utgard -->
        <!-- 来自jinterop;该库用于为Java提供dcom访问权限 -->
        <dependency>
            <groupId>org.openscada.utgard</groupId>
            <artifactId>org.openscada.opc.dcom</artifactId>
            <version>1.5.0</version>
        </dependency>
        <!-- 来自utgard;实际的uttard api -->
        <dependency>
            <groupId>org.openscada.utgard</groupId>
            <artifactId>org.openscada.opc.lib</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-ext-jdk16</artifactId>
            <version>
                1.46
            </version>
        </dependency>
        <!-- OPC-jinterop -->
        <!-- 来自jinterop;该库用于为Java提供dcom访问权限 -->
        <dependency>
            <groupId>org.openscada.jinterop</groupId>
            <artifactId>org.openscada.jinterop.core</artifactId>
            <version>2.1.8</version>
        </dependency>
        <!-- 来自jinterop;一些jinterop依赖项 -->
        <dependency>
            <groupId>org.openscada.jinterop</groupId>
            <artifactId>org.openscada.jinterop.deps</artifactId>
            <version>1.5.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bcprov-jdk15on</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- OPC-external -->
        <!-- 来自外部; jinterop的依赖项 -->
        <dependency>
            <groupId>org.openscada.external</groupId>
            <artifactId>org.openscada.external.jcifs</artifactId>
            <version>1.2.25</version>
            <exclusions>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bcprov-jdk15on</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

设置线程池 开机启动初始化连接服务端

  public static Server server = null;
 @Override
    public void run(String... args) throws Exception {
        try {
            ServerList serverList = new ServerList(this.host, this.userName, this.password, this.domain);
            ConnectionInformation ci = new ConnectionInformation();
            ci.setHost(this.host);
            ci.setDomain(this.domain);
            ci.setUser(this.userName);
            ci.setPassword(this.password);
            ci.setClsid(serverList.getClsIdFromProgId(progId));
            server = new Server(ci,  new ScheduledThreadPoolExecutor(1,
                    new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build()));
            server.connect();
            Properties prop = System.getProperties();
            RunWarning runWarning=new RunWarning();
            runWarning.setTipsMessage("系统启动时登录的用户为"+prop.getProperty( "user.name" ));
            runWarning.setCreateTime(LocalDateTime.now());
            runWarningService.save(runWarning);
            log.info("OPC客户端初始化成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            RunWarning runWarning=new RunWarning();
            runWarning.setTipsMessage(e.getMessage());
            runWarning.setCreateTime(LocalDateTime.now());
            runWarningService.save(runWarning);
            log.error("OPC客户端初始化失败");
        }
    }
  host: 127.0.0.1 //本机地址
  domain: "" //可以为空
  userName: OPCUser //windows配置的具有DOM访问权限的用户名
  password: 123456 //密码
  #clsid: F8582CF3-88FB-11D0-B850-00C0F0104305 //组件服务中的serverID 
  progId: Kepware.KEPServerEX.V6 //服务的名称 参考下图
  groupName: //分组名称自定义

在这里插入图片描述
获取OPC所有点位方法

 private volatile Server mServer = OPCApplication.server;
    public  List findAllItem (){
        FlatBrowser flatBrowser = mServer.getFlatBrowser();
        List<String> list=new ArrayList<>();
        if (flatBrowser == null) {
            log.warn("读取 空");
        } else {
            try {
                Collection<String> collection = flatBrowser.browse();
                for (String item:collection
                     ) {
                    list.add(item);
                }
            } catch (JIException e) {
                log.error(e.getMessage());
            } catch (UnknownHostException e) {
                log.error("host主机IP错误 = " + e.getMessage());
            }
        }
        return list;
    }

OPCUtil 提供读写操作 写入值需要根据数据类型写入数据类型不符会写入失败

package com.example.Util;

import com.example.OPCApplication;
import com.example.entity.OpcData;
import com.example.entity.OpcItem;
import com.example.entity.StrategyPreinstall;
import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.core.JIVariant;
import org.openscada.opc.lib.common.NotConnectedException;
import org.openscada.opc.lib.da.*;
import org.openscada.opc.lib.da.browser.FlatBrowser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


public class OPCUtil {
    @Value("${OPC.groupName}")
    private String groupName; //自定义随便起名即可
    private volatile Server mServer = OPCApplication.server;
    private static final Logger log = LoggerFactory.getLogger(OPCUtil.class);
    //查询所有item
    public  List findAllItem (){
        FlatBrowser flatBrowser = mServer.getFlatBrowser();
        List<String> list=new ArrayList<>();
        if (flatBrowser == null) {
            log.warn("读取 空");
        } else {
            try {
                Collection<String> collection = flatBrowser.browse();
                for (String item:collection
                     ) {
                    list.add(item);
                }
            } catch (JIException e) {
                log.error(e.getMessage());
            } catch (UnknownHostException e) {
                log.error("host主机IP错误 = " + e.getMessage());
            }
        }
        return list;
    }
    public List<OpcItem> read(List<String> itemList) throws DuplicateGroupException, NotConnectedException, JIException, UnknownHostException, AddFailedException, InterruptedException {
       Group group= OPCApplication.server.addGroup(groupName);
        List<Item> cache = new LinkedList<>();
        for (String i:itemList
             ) {
            Item item= group.addItem(i);

            cache.add(item);
        }
        Item[] items = new Item[cache.size()];
        cache.toArray(items);
        List<OpcItem> opclist = group.read(false, items).entrySet().parallelStream().map(
                e -> {
                    try {
                        return new OpcItem(
                                e.getKey().getId(),
                                e.getValue().getValue().getType(),
                                e.getValue().getValue().getObject().toString(),
                                new Timestamp(e.getValue().getTimestamp().getTimeInMillis()),
                                e.getValue().getQuality(),
                                e.getValue().getErrorCode()
                        );
                    } catch (JIException ex) {
                        log.error("item" + e.getKey().getId() + "数值转换异常");
                        return null;
                    }
                }
        ).collect(Collectors.toList());
        group.clear();
        return opclist;
    }

    public String writeBatch(List<OpcData> itemList) throws DuplicateGroupException, NotConnectedException, JIException, UnknownHostException, AddFailedException {
        Group group= OPCApplication.server.addGroup(groupName);
        for (OpcData i:itemList
        ) {
            Item item= group.addItem(i.getItemId());
            if ("true".equals(i.getItemValue())||"flase".equals(i.getItemValue())){
                item.write(JIVariant.makeVariant(Boolean.parseBoolean(i.getItemValue())));
            }
            item.write(JIVariant.makeVariant(Double.parseDouble(i.getItemValue())));
        }
        group.clear();
        return null;
    }
    public String writeStrategy(String ItemId,Double ItemValue) throws DuplicateGroupException, NotConnectedException, JIException, UnknownHostException, AddFailedException {
        Group group= OPCApplication.server.addGroup(groupName);

            Item item= group.addItem(ItemId);
            item.write(JIVariant.makeVariant(ItemValue));

        group.clear();
        return null;
    }
    public String writeSop(String ItemId,Boolean ItemValue) throws DuplicateGroupException, NotConnectedException, JIException, UnknownHostException, AddFailedException {
        Group group= OPCApplication.server.addGroup(groupName);

        Item item= group.addItem(ItemId);
        item.write(JIVariant.makeVariant(ItemValue));

        group.clear();
        return null;
    }
}

JIException: Message not found for errorCode: 0x80010111
不要装杀毒软件,所有杀毒软件全部卸载 把windows10降到1909以下版本 windows7 不受影响.如果还报错建议检查一下OPC配置
0x8000401A
The server process could not be started because the configured identity is incorrect. Check the username and password.

0x80004005
Unspecified Error

0x800706BA
RPC_S_SERVER_UNAVAILABLE
The RPC server is unavailable

0x800706BF
远程过程调用失败且未执行

0x8007000E
E_OUTOFMEMORY
Not enough memory to complete the requested operation. This can happen any time the server needs to allocate memory to complete the requested operation.

0x80070776
Couldn’t create connection with advise sink,
Reason for error: There is a problem resolving the computer name.
Solution: This problem can be solved by specifying the IP address of the server instead of specifying the computer name

0x80070005
0x80070005 appears in the OPC Client application when it succeeds in launching an OPC Server or OpcEnum, but fails to receive a reply from either of the applications. This error could be caused under several conditions: On the OPC Server PC, the OPC Client User Account does not have the right Access Control List (ACL) permissions in the System-Wide DCOM settings, Access Permissions, Edit Default.
On the OPC Client PC, the OPC Server User Account does not have the right Access Control List (ACL) permissions in the System-Wide DCOM settings, Access Permissions, Edit Limits.

On the OPC Client PC, the DCOM Default Impersonation Level is set to “Anonymous” instead of “Identify”, and the “ANONYMOUS LOGON” Access Control Entry (ACE) does not exist in the OPC Client PC, Access Control List (ACL) permissions in the System-Wide DCOM settings, Access Permissions, Edit Limits.

Failure to obtain a CLSID
OPC Client application failed to find the OPC Server. The two most common causes are:

  1. Failure to find the OPC server in the Windows Registry
  2. Failure to connect to OPCENUM.EXE

Failed to connect to 2.0 data access interface for group … Falling back to 1.0 interface
可能原因:计算机重名或局域网DNS服务故障,或者在不同子网中
0x8001FFFF
设备连接数量超过OPCserver支持的连接量,需要重启电脑即可
0x80010108 The object invoked has
disconnected from its clients.
Re-initialize your OPC Server Connection.
0x80040004 There is no connection for
this connection ID
0x80040005 Need to run the object to
perform this operation
0x80040007 Uninitialized object
0x80040154 Class not registered The OPC Server, or a component needed to
make the OPC connection is not registered
with Windows. This may mean that you simply
need to register a DLL or OCX file.
0x80040155 Interface not registered The OPC Server does not support the
interface that you are trying to connect to.
Examples may include Item Browsing,
Asynchronous I/O or OPC DA v2.x or 3.x
interfaces etc.
0x800401f3 Invalid class string The GUID/CLSID of the specified OPC Server
is not valid.
0x80040200 • Unable to
impersonate DCOM
Client
• Unknown OLE status
code
DCOM security problem, typically on the
Client side. This error typically occurs when
trying to specify a callback address for
Asynchronous I/O.
0x80040202 Cannot Connect Error typically occurs when a call is made to
Advise on the connection point. This often
means that OPCPROXY.DLL is not the same
version on your different computers.
0x80070002 The system cannot find the
file specified.
Re-install your software.
0x80070005 Access is denied. You need to configure your DCOM Security
settings. See our DCOM Tutorial:
http://www.softwaretoolbox.com/dcom Page 6 of 7
Hex Code Description Resolution
0x80070057 The parameter is incorrect. The OPC Server has rejected your request,
indicating that the parameter(s) you specified
are not valid for the type of request being
made. You will need more details on the
actual OPC calls being made between the
Client and Server.
0x8007041d The service did not respond
to the start or control request
in a timely fashion.
Specific to Windows Services. The service did
not start within the allowed time-frame. This
indicates an initialization problem with the
Windows service.
0x800705b4 This operation returned
because the timeout period
expired.
This is a timeout. You may need to increase
your timeout settings.
0x800706ea A floating-point underflow
occurred at the RPC server.
0x80070725 Incompatible version of the
RPC stub.
0x80080005 Server execution failed There is a problem with the OPC Server
preventing it from being started by Windows.
This may be the result of file-permissions,
DCOM Security permissions, or a lack of
resources.
0x80004002 No such interface supported The OPC Server does not support the
interface that you are trying to connect to.
Examples may include Item Browsing,
Asynchronous I/O or OPC DA v2.x or 3.x
interfaces etc.
0x80004005 Unspecified error The most common message seen, that yields
the least information. In these cases you often
need to check the event-logs at your OPC
Server for more information.
0x8000401a The server process could not
be started because the
configured identity is
incorrect. Check the
username and password.
DCOM Configuration permissions. Modify the
identity that the application should run under,
perhaps specify a named account or choose
“Interactive User’.
0x800706ba The RPC server is
unavailable.
The OPC Server could not be contacted. This
is usually the result of a firewall blocking the
application

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值