java写s7和plc通讯

pom.xml

  <dependency>
            <groupId>com.github.s7connector</groupId>
            <artifactId>s7connector</artifactId>
            <version>2.1</version>
 </dependency>

maven下载不了的,下载包,评论或者私自内免费给
DB212 类:
String类型的我的占用25个字节,要看实际情况
在这里插入图片描述

package com.alnan.entity;


import com.github.s7connector.api.annotation.S7Variable;
import com.github.s7connector.impl.utils.S7Type;
import lombok.Data;

import java.io.Serializable;

@Data
public class DB212 implements Serializable {

    @S7Variable(type = S7Type.STRING,byteOffset = 32,bitOffset = 0, size = 25)
    public int PW_plateId;

    @S7Variable(type = S7Type.WORD,byteOffset = 88,bitOffset = 0)
    public int PW_plateLength;

    @S7Variable(type = S7Type.WORD,byteOffset = 92,bitOffset = 0)
    public int PW_plateWidth;

    @S7Variable(type = S7Type.WORD,byteOffset = 208,bitOffset = 0)
    public int PW_send;

    @S7Variable(type = S7Type.WORD,byteOffset = 212,bitOffset = 0)
    public int ACT_PW_1;

    @S7Variable(type = S7Type.WORD,byteOffset = 216,bitOffset = 0)
    public int ACT_PW_2;

    @S7Variable(type = S7Type.WORD,byteOffset = 260,bitOffset = 0)
    public int ACT_HY_UP_1;

    @S7Variable(type = S7Type.WORD,byteOffset = 264,bitOffset = 0)
    public int ACT_HY_DOWN_1;

    @S7Variable(type = S7Type.WORD,byteOffset = 276,bitOffset = 0)
    public int ACT_HY_UP_2;

    @S7Variable(type = S7Type.WORD,byteOffset = 280,bitOffset = 0)
    public int ACT_HY_DOWN_2;

    @S7Variable(type = S7Type.WORD,byteOffset = 292,bitOffset = 0)
    public int ACT_HY_UP_3;

    @S7Variable(type = S7Type.WORD,byteOffset = 296,bitOffset = 0)
    public int ACT_HY_DOWN_3;

    @S7Variable(type = S7Type.WORD,byteOffset = 308,bitOffset = 0)
    public int ACT_HY_UP_4;

    @S7Variable(type = S7Type.WORD,byteOffset = 312,bitOffset = 0)
    public int ACT_HY_DOWN_4;

    @S7Variable(type = S7Type.WORD,byteOffset = 324,bitOffset = 0)
    public int ACT_HY_UP_5;

    @S7Variable(type = S7Type.WORD,byteOffset = 328,bitOffset = 0)
    public int ACT_HY_DOWN_5;

    @S7Variable(type = S7Type.WORD,byteOffset = 340,bitOffset = 0)
    public int ACT_LC_UP_1;

    @S7Variable(type = S7Type.WORD,byteOffset = 344,bitOffset = 0)
    public int ACT_LC_DOWN_1;

    @S7Variable(type = S7Type.WORD,byteOffset = 356,bitOffset = 0)
    public int ACT_LC_UP_2;

    @S7Variable(type = S7Type.WORD,byteOffset = 360,bitOffset = 0)
    public int ACT_LC_DOWN_2;

}

应用:

 //一级发数据
            S7Connector s7Connector = S7ConnectorFactory.buildTCPConnector()
                    .withHost("192.168.100.10")//ip
                    .withPort(102)
                    .withTimeout(1000) //连接超时时间
                    .withRack(0)
                    .withSlot(1)
                    .build();


            s7Connector.write(DaveArea.DB, 212, 32, pzString(plateNo));//212是PLC地址,32是从第多少字节开始写入,
            s7Connector.write(DaveArea.DB,212,88, ByteBuffer.allocate(4).putInt(Integer.valueOf(length)).array());
            s7Connector.write(DaveArea.DB,212,92, ByteBuffer.allocate(4).putInt(Integer.valueOf(width)).array());
            s7Connector.write(DaveArea.DB,212,208, ByteBuffer.allocate(4).putInt(1).array());
            System.out.println(s7Serializer.dispense(DB212.class,212,0));
            s7Connector.close();



 public byte[] pzString(String out_string){
        //        写入字符串
        //第一个参数:DaveArea.DB 表示读取PLC的地址区域为DB
        //第二个参数:DB块地址,若plc中是DB1000,则填1000
        //第三个参数:偏移量
        //第四个参数:写入的数据 二进制数组byte[]  ,总长度为10的话,有效数据只能是10-8,第一位代表总长,第二位,代表有效数据字节
        int writeStrLength = 25;//地址块大小
        byte[] writeBytes = new byte[writeStrLength];
        writeBytes[0] = (byte) writeStrLength;//写入本字符串块总宽度
        out_string = out_string.trim();//清除掉两边的空串
        int availableEffectCharLength = 0;//有效字符数控制
        if (out_string.length() > writeStrLength - 2) {//>writeStrLength-2 截断到最大有效数据位
            availableEffectCharLength = writeStrLength - 2;

        } else {//<=writeStrLength-2
            availableEffectCharLength = out_string.length();
        }
        writeBytes[1] = (byte) availableEffectCharLength;//写入有效字节数
        byte[] strBytes = out_string.getBytes(StandardCharsets.US_ASCII);
        for (int i = 0; i < availableEffectCharLength; i++) {
            writeBytes[i + 2] = strBytes[i];
        }
      return writeBytes;
    }

基本上能用起来了
具体细节:

https://blog.csdn.net/qq_51383028/article/details/140549849

要实现JavaPLC通讯,一般可以通过OPC(OLE for Process Control)或S7协议来实现。下面以S7协议为例,介绍如何使用Java实现PLC通讯。 1. 首先需要下载S7协议相关的Java库,比如Snap7,可以从官网下载:http://snap7.sourceforge.net/ 2. 将下载好的Snap7库导入到Java项目中。 3. 编Java代码实现PLC通讯。下面是一个简单的示例代码,用于读取PLC的一个DB块中的数据: ```java import snap7.*; public class PlcTest { public static void main(String[] args) { S7Client client = new S7Client();// 创建S7Client对象 int result = client.ConnectTo("192.168.1.100", 0, 1);// 连接PLC if (result == 0) {// 连接成功 byte[] data = new byte[100];// 存放读取的数据 result = client.DBRead(1, 0, 100, data);// 读取DB1块中的数据 if (result == 0) {// 读取成功 System.out.println("读取的数据:" + new String(data)); } else {// 读取失败 System.out.println("读取失败:" + client.ErrorText(result)); } client.Disconnect();// 断开连接 } else {// 连接失败 System.out.println("连接失败:" + client.ErrorText(result)); } } } ``` 在上面的示例中,我们使用了Snap7库的S7Client类来实现PLC通讯。首先通过ConnectTo()方法连接PLC,然后通过DBRead()方法读取DB块中的数据,最后通过Disconnect()方法断开连接。 需要注意的是,PLC的IP地址和DB块号需要根据实际情况进行设置。此外,如果要PLC的数据,可以使用S7Client类的DBWrite()方法。 以上仅为示例代码,实际应用中还需要根据具体情况进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值