Java读写操作西门子S7系列PLC

简介

Java实现操作西门子S7系列PLC,基于开源项目s7connect实现,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作。
项目实现&测试源码

特性

  • 使用 TCP 协议连接西门子S7系列PLC
  • 高性能读写西门子S7系列PLC
  • 支持 OSGi
  • 支持 PROFINET
  • 支持注解定义

使用

连接PLC
private static final String HOST = "192.168.0.20";

S7Connector connector =  S7ConnectorFactory.buildTCPConnector().withHost(HOST).build();
定义读写操作模块
@Data
public class PlcDb {
    @S7Variable(type = S7Type.BOOL, byteOffset = 0, bitOffset = 0)
    public boolean status;

    @S7Variable(type = S7Type.DWORD, byteOffset = 2)
    public long option;

    @S7Variable(type = S7Type.REAL, byteOffset = 34)
    public float position;

    @S7Variable(type = S7Type.TIME, byteOffset = 66)
    public long time;
}
读数据
private static final int DB_NUM = 7;
private static final int BYTE_OFFSET = 0;
    
PlcDb plcDb = serializer.dispense(PlcDb.class, DB_NUM, BYTE_OFFSET);
写数据
private static final int DB_NUM = 7;
private static final int BYTE_OFFSET = 0;
PlcDb plcDb =new PlcDb();
plcDb.set...
...

serializer.store(PlcDb.class, DB_NUM, BYTE_OFFSET);
关闭操作
 connector.close();

Test测试

项目实现&测试源码

Websocket 下的操作实例

package com.poster.controller;
import com.alibaba.fastjson.JSON;
import com.github.s7connector.api.S7Connector;
import com.github.s7connector.api.S7Serializer;
import com.github.s7connector.api.factory.S7ConnectorFactory;
import com.github.s7connector.api.factory.S7SerializerFactory;
import com.poster.bean.PlcDb;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * <p>Copyright(c) 2018. Pnoker All Rights Reserved.
 * <p>Author     : Pnoker
 * <p>Email      : pnokers@gmail.com
 * <p>Description: Websocket Controller控制类
 */
@Slf4j
@Component
@ServerEndpoint(value = "/ws/poster")
public class WsController {
    private static final String HOST = "192.168.0.20";
    private static final int DB_NUM = 7;
    private static final int BYTE_OFFSET = 0;

    private Session session;
    private volatile boolean isRun = false;
    private S7Connector connector = null;

    @OnOpen
    public void onOpen(Session session) {
        log.info("open websocket {}", session.getId());
        this.session = session;
        if (null == connector) {
            try {
                isRun = true;
                connector = S7ConnectorFactory.buildTCPConnector().withHost(HOST).build();
            } catch (Exception e) {
                connector = null;
                log.error("new s7connector fail {}", e.getMessage(), e);
            }
        }

        if (null != connector) {
            S7Serializer serializer = S7SerializerFactory.buildSerializer(connector);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    try {
                        if (isRun) {
                            PlcDb plcDb = serializer.dispense(PlcDb.class, DB_NUM, BYTE_OFFSET);
                            sendMessage(JSON.toJSONString(plcDb));
                        } else {
                            cancel();
                            log.info("stop send message to session {}", session.getId());
                        }
                    } catch (Exception e) {
                        log.error("send message timer fail {}", e.getMessage(), e);
                    }
                }
            }, 2000, 200);
        }
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("receive message '{}' from session id '{}' ", message, session.getId());
    }

    @OnClose
    public void onClose() {
        log.info("close websocket {}", session.getId());
        isRun = false;
    }

    public void onError(Session session, Throwable error) {
        log.error(error.getMessage());
    }

    public void sendMessage(String message) {
        try {
            log.info("send message {} ", message);
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            log.error("sendMessage fail {}", e.getMessage(), e);
        }
    }
}

项目推荐



DC3是基于Spring Cloud的开源可分布式物联网(IOT)平台,用于快速开发、部署物联设备接入项目,是一整套物联系统解决方案。

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值