代码编织梦想

1 依赖

<!--utgard -->
        <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>
        <dependency>
            <groupId>org.openscada.jinterop</groupId>
            <artifactId>org.openscada.jinterop.core</artifactId>
            <version>2.1.8</version>
        </dependency>
        <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>
        <dependency>
            <groupId>org.openscada.utgard</groupId>
            <artifactId>org.openscada.opc.dcom</artifactId>
            <version>1.5.0</version>
        </dependency>
        <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-jdk15on</artifactId>
            <version>1.61</version>
        </dependency>

2 config

#OPC 配置
opcconfig:
  host: 192.168.4.132
  username: opcuser
  password: 123456
  clsid: F8582CF2-88FB-11D0-B850-00C0F0
  # 5 秒读一次数据
  frequency: 5000

@Component
@ConfigurationProperties(prefix = "opcconfig")
public class OpcConfig {
    private static String host;
    private static String username;
    private static String password;
    private static String clsid;
    private static Integer frequency;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        OpcConfig.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        OpcConfig.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        OpcConfig.password = password;
    }

    public String getClsid() {
        return clsid;
    }

    public void setClsid(String clsid) {
        OpcConfig.clsid = clsid;
    }

    public Integer getFrequency() {
        return frequency;
    }

    public void setFrequency(Integer frequency) {
        OpcConfig.frequency = frequency;
    }

    public static Server instance;

    /**
     * 获取连接
     * @return 返回连接
     * @throws Exception 连接异常
     */
    public static Server getServerInstance() throws Exception {
        if (instance == null) {
            instance = OpcConnectUtils.readDataFromOpcServer(host, username
                    , password, clsid);
        }
        return instance;
    }
}
public class OpcConnectUtils {
    public static Server readDataFromOpcServer(String host,String username,String password,String clsid) throws Exception {
        // 连接信息
        final ConnectionInformation ci = new ConnectionInformation();
        ci.setHost(host);         // 本机IP
        ci.setDomain("");                  // 域,为空就行
        ci.setUser(username);             // 本机上自己建好的用户名
        ci.setPassword(password);          // 密码

        //使用MatrikonOPC Server的配置
        ci.setClsid(clsid); // MatrikonOPC的注册表ID,可以在“组件服务”里看到

        final String itemId = "group1.tag1";    // MatrikonOPC Server上配置的项的名字按实际

        // 启动服务
        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());

        try {
            // 连接到服务
            server.connect();
            System.out.println("连接到服务器。。。。");
            return server;

        } catch (final JIException e) {
            System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode())));
            return null;
        }
    }
}

3 读取值

		Server server = OpcConfig.getServerInstance();
        final AccessBase access = new SyncAccess(server, opcConfig.getFrequency());
      
        for (String deviceName : deviceNameList) {
            // 这是个回调函数,就是读到值后执行这个打印,是用匿名类写的,当然也可以写到外面去
            access.addItem(deviceName, (item, itemState) -> {
                JIString value;
                try {
                    value = itemState.getValue().getObjectAsString();
                } catch (JIException e) {
                    throw new RuntimeException(e);
                }
                // 数值与 设备名称用 ## 分隔
                stringRedisTemplate.convertAndSend("saveEnvironmentMonitorData", value.getString() + "##" + item.getId());
            });
        }
        // start reading,开始读值
        access.bind();

4 写值

 Server server;
        try {
            server = OpcConfig.getServerInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    public void writeDataToOpcServer(Server server, String name, Boolean isTurnOn) throws Exception {
        //Server server = OpcConfig.getServerInstance();
        try {
            final String itemId = "lighting." + name;
            assert server != null;
            Group group = server.addGroup("test");
            final Item item = group.addItem(itemId);
            item.write(new JIVariant(isTurnOn));
            //移除分组
            server.removeGroup(group, true);
        } catch (final JIException e) {
            throw new RuntimeException(e);
        }
    }
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_45914119/article/details/126762544