Skip to content

Commit

Permalink
docs: 1# modify tutorial (完善相关教程); 2# upgrade to v1.3.0 (版本升级到v1.3.0);
Browse files Browse the repository at this point in the history
  • Loading branch information
xingshuangs committed May 17, 2023
1 parent a0ded65 commit 27ee4d8
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 61 deletions.
6 changes: 3 additions & 3 deletions README-CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# IOT-COMMUNICATION

![Maven-v1.2.7](https://img.shields.io/badge/Maven-v1.2.7-brightgreen)
[![Maven-v1.3.0](https://img.shields.io/badge/Maven-v1.3.0-brightgreen)](https://mvnrepository.com/artifact/com.github.xingshuangs/iot-communication)
![Language-java8](https://img.shields.io/badge/Language-java8-blue)
![Idea-2022.02.03](https://img.shields.io/badge/Idea-2022.02.03-lightgrey)
![CopyRight-Oscura](https://img.shields.io/badge/CopyRight-Oscura-yellow)
Expand All @@ -14,7 +14,7 @@

目前它只是一个物联网通信的工具,包含

- 西门子S7通信协议(可以访问西门子S1500,S1200S200smart);
- 西门子S7通信协议(可以访问西门子**S1500****S1200****S200smart****西门子机床828D**);
- ModbusTCP通信协议;
- 基础字节数组解析转换工具;

Expand All @@ -28,7 +28,7 @@
<dependency>
<groupId>com.github.xingshuangs</groupId>
<artifactId>iot-communication</artifactId>
<version>1.2.7</version>
<version>1.3.0</version>
</dependency>
```

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# IOT-COMMUNICATION

![Maven-v1.2.7](https://img.shields.io/badge/Maven-v1.2.7-brightgreen)
[![Maven-v1.3.0](https://img.shields.io/badge/Maven-v1.3.0-brightgreen)](https://mvnrepository.com/artifact/com.github.xingshuangs/iot-communication)
![Language-java8](https://img.shields.io/badge/Language-java8-blue)
![Idea-2022.02.03](https://img.shields.io/badge/Idea-2022.02.03-lightgrey)
![CopyRight-Oscura](https://img.shields.io/badge/CopyRight-Oscura-yellow)
Expand All @@ -14,7 +14,7 @@

Now, it is a tool for iot communication, it includes

- Siemens S7 protocol, it can access to S1500, S1200, S200smart.
- Siemens S7 protocol, it can access to **S1500**, **S1200**, **S200smart**, **Siemens Machine Tool 828D**.
- ModbusTCP protocol.
- Parse byte array data.

Expand All @@ -28,7 +28,7 @@ Add the dependency to pom.xml in the JAVA project
<dependency>
<groupId>com.github.xingshuangs</groupId>
<artifactId>iot-communication</artifactId>
<version>1.2.7</version>
<version>1.3.0</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

<groupId>com.github.xingshuangs</groupId>
<artifactId>iot-communication</artifactId>
<version>1.2.7</version>
<version>1.3.0</version>
<packaging>jar</packaging>

<name>iot-communication</name>
<description>iot communication tool 目前支持S7、ModbusTCP通信协议、字节数据转换</description>
<description>iot-communication is a tool 目前支持S7、ModbusTCP通信协议、字节数据转换</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

/**
* plc的网络通信
* 最小字节数组大小是240-18=222,480-18=462,960-18=942
* 最大读取字节数组大小是240-18=222,480-18=462,960-18=942
* 根据测试S1200[CPU 1214C],单次读多字节
* 发送:最大字节读取长度是 216 = 240 - 24, 24(响应报文的PDU)=10(header)+14(parameter)
* 发送:最大字节读取长度是 216 = 240 - 24, 24(请求报文的PDU)=10(header)+14(parameter)
* 接收:最大字节读取长度是 222 = 240 - 18, 18(响应报文的PDU)=12(header)+2(parameter)+4(dataItem)
* 根据测试S1200[CPU 1214C],单次写多字节
* 发送:最大字节写入长度是 212 = 240 - 28, 28(请求报文的PDU)=10(header)+14(parameter)+4(dataItem)
* 接收:最大字节写入长度是 225 = 240 - 15, 15(请求报文的PDU)=12(header)+2(parameter)+1(dataItem)
* 接收:最大字节写入长度是 225 = 240 - 15, 15(响应报文的PDU)=12(header)+2(parameter)+1(dataItem)
*
* @author xingshuang
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.xingshuangs.iot.protocol.modbus.service;


import com.github.xingshuangs.iot.utils.HexUtil;

import java.util.List;

/**
Expand All @@ -9,6 +11,7 @@
public class DemoReadTest {
public static void main(String[] args) {
ModbusTcp plc = new ModbusTcp(1, "127.0.0.1");
plc.setComCallback(x -> System.out.printf("长度[%d]:%s%n", x.length, HexUtil.toHexString(x)));

// read coil
List<Boolean> readCoil = plc.readCoil(0, 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.xingshuangs.iot.protocol.modbus.service;


import com.github.xingshuangs.iot.utils.HexUtil;

import java.util.Arrays;
import java.util.List;

Expand All @@ -11,6 +13,7 @@ public class DemoWriteTest {

public static void main(String[] args) {
ModbusTcp plc = new ModbusTcp(1, "127.0.0.1");
plc.setComCallback(x -> System.out.printf("长度[%d]:%s%n", x.length, HexUtil.toHexString(x)));

// single write coil
plc.writeCoil(0, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class S7SerializerTest {

@Test
public void read() {
s7PLC.setComCallback(x -> log.debug("长度[{}],内容:{}", x.length, HexUtil.toHexString(x)));
s7PLC.setComCallback(x -> System.out.printf("长度[%d]:%s%n", x.length, HexUtil.toHexString(x)));
// s7PLC.setComCallback(x -> log.debug("长度[{}],内容:{}", x.length, HexUtil.toHexString(x)));
S7Serializer s7Serializer = S7Serializer.newInstance(s7PLC);
DemoBean bean = s7Serializer.read(DemoBean.class);
log.info(bean.toString());
Expand Down Expand Up @@ -65,6 +66,7 @@ public void writeLargeData() {
S7Serializer s7Serializer = S7Serializer.newInstance(s7PLC);
DemoLargeBean bean = s7Serializer.read(DemoLargeBean.class);
System.out.println("-------------------------------");
bean.setBitData(true);
bean.getByteData2()[0] = (byte) 0x05;
bean.getByteData3()[0] = (byte) 0x05;
bean.getByteData4()[0] = (byte) 0x05;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.xingshuangs.iot.protocol.s7.service;


import com.github.xingshuangs.iot.protocol.s7.enums.*;
import com.github.xingshuangs.iot.protocol.s7.model.*;

/**
* @author xingshuang
*/
public class DemoS7Custom {

public static void main(String[] args) {
S7PLC s7PLC = new S7PLC(EPlcType.S1200, "127.0.0.1");

// bit数据读写
byte[] expect = new byte[]{(byte) 0x00};
s7PLC.writeRaw(EParamVariableType.BIT, 1, EArea.DATA_BLOCKS, 1, 0, 3,
EDataVariableType.BIT, expect);
byte[] actual = s7PLC.readRaw(EParamVariableType.BIT, 1, EArea.DATA_BLOCKS, 1, 0, 3);

// byte数据读写
expect = new byte[]{(byte) 0x02, (byte) 0x03};
s7PLC.writeRaw(EParamVariableType.BYTE, 2, EArea.DATA_BLOCKS, 1, 1, 0,
EDataVariableType.BYTE_WORD_DWORD, expect);
byte[] actual1 = s7PLC.readRaw(EParamVariableType.BYTE, 2, EArea.DATA_BLOCKS, 1, 1, 0);

// 对象形式发送
RequestNckItem item = new RequestNckItem(ENckArea.C_CHANNEL, 1, 23, 1, ENckModule.S, 1);
S7Data s7Data = NckRequestBuilder.creatNckRequest(item);
S7Data ackData = s7PLC.readFromServerByPersistence(s7Data);

// 裸报文发送
byte[] sendByteArray = new byte[]{
// tpkt
(byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x1D,
// cotp DT Data
(byte) 0x02, (byte) 0xF0, (byte) 0x80,
// header
(byte) 0x32, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x00, (byte) 0x0C, (byte) 0x00, (byte) 0x00,
// parameter
(byte) 0x04, (byte) 0x01,
// request item
(byte) 0x12, (byte) 0x08, (byte) 0x82, (byte) 0x41, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x01, (byte) 0x7f, (byte) 0x01
};
byte[] recByteArray = s7PLC.readFromServerByPersistence(sendByteArray);

s7PLC.close();
}
}
9 changes: 9 additions & 0 deletions tips/changeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v1.3.0
- 更新时间:2023.05.17
- 添加西门子协议中NCK的寻址方式,主要针对西门子机床828D的数据访问
- S7协议添加对time、date、timeOfDay数据类型的读写
- S7协议序列化方式中添加对string、time、date、timeOfDay的解析
- 修复S7协议写string时更改第一个字节的bug(第一个字节为允许最大字节数),现在直接跳过该字节的处理
- modbus添加回调事件,用于输出交互的报文
- modbus添加短连接方式,添加readBoolean方法

## v1.2.7
- 更新时间:2023.03.16
- 修复下S7序列化读取时,由于实体类字段超过18个且读取数据量较小导致报文大小超过PDU的BUG
Expand Down
69 changes: 68 additions & 1 deletion tutorial/README-Modbus-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,73 @@

[返回主页](../README-CN.md)

## 前言

> 注意点
- **1** 个线圈 = **1**
- **1** 个寄存器地址 = **2** 个字节;
- [ 寄存器起始地址 = **0001** ] 相当于 [ 访问地址 = **0** ],存在 **1** 位地址偏移;
- **4 字节**数据的编码格式 = **BA_DC**; (大端模式 = **DC_BA**,小端模式 = **AB_CD**)

> 存储区
| 区号 | 名称 | 读写 | 地址范围 | 对应方法 |
|:---:|:------|:--------|:-----------:|:-------------------------------------|
| 0区 | 输出线圈 | 可读可写布尔量 | 00001-09999 | readCoil / writeCoil |
| 1区 | 输入线圈 | 只读布尔量 | 10001-19999 | readDiscreteInput |
| 3区 | 输入寄存器 | 只读寄存器 | 30001-39999 | readInputRegister |
| 4区 | 保持寄存器 | 可读可写寄存器 | 40001-49999 | readHoldRegister / writeHoldRegister |

> 功能码
| 功能码 | 功能说明 | 对应方法 |
|:---:|:--------|:------------------|
| 01H | 读取输出线圈 | readCoil |
| 02H | 读取输入线圈 | readDiscreteInput |
| 03H | 读取保持寄存器 | readHoldRegister |
| 04H | 读取输入寄存器 | readInputRegister |
| 05H | 写入单线圈 | writeCoil |
| 06H | 写入单寄存器 | writeHoldRegister |
| 0FH | 写入多线圈 | writeCoil |
| 10H | 写入多寄存器 | writeHoldRegister |

> 保持寄存器快捷访问
| 方法 | 寄存器数量 | 字节大小 | 位大小 | 含义 | 寄存器 |
|:-------------|:-----:|:----:|:---:|:----------|:-----:|
| readBoolean | 1 | 1/8 | 1 | 读取boolean | 保持寄存器 |
| readInt16 | 1 | 2 | 16 | 读取Int16 | 保持寄存器 |
| readUInt16 | 1 | 2 | 16 | 读取UInt16 | 保持寄存器 |
| readInt32 | 2 | 4 | 32 | 读取Int32 | 保持寄存器 |
| readUInt32 | 2 | 4 | 32 | 读取UInt32 | 保持寄存器 |
| readFloat32 | 2 | 4 | 32 | 读取Float32 | 保持寄存器 |
| readFloat64 | 4 | 8 | 64 | 读取Float64 | 保持寄存器 |
| readString | n | 2n | 16n | 读取字符串 | 保持寄存器 |
| writeInt16 | 1 | 2 | 16 | 写入Int16 | 保持寄存器 |
| writeUInt16 | 1 | 2 | 16 | 写入UInt16 | 保持寄存器 |
| writeInt32 | 2 | 4 | 32 | 写入Int32 | 保持寄存器 |
| writeUInt32 | 2 | 4 | 32 | 写入UInt32 | 保持寄存器 |
| writeFloat32 | 2 | 4 | 32 | 写入Float32 | 保持寄存器 |
| writeFloat64 | 4 | 8 | 64 | 写入Float32 | 保持寄存器 |
| writeString | n | 2n | 16n | 写入字符串 | 保持寄存器 |

## 打印报文

如果想知道通信过程中的实际输入输出报文内容,可以添加报文信息打印

```java
class Demo {
public static void main(String[] args) {
ModbusTcp plc = new ModbusTcp(1, "127.0.0.1");
// 报文输出设置
plc.setComCallback(x -> System.out.printf("长度[%d]:%s%n", x.length, HexUtil.toHexString(x)));
plc.writeInt16(2, (short) 10);
plc.close();
}
}
```

## 通信连接

- 默认采用长连接的方式,不用的时候需要手动关闭;
Expand Down Expand Up @@ -122,7 +189,7 @@ class Demo {
// hold register write float64
plc.writeFloat64(2, 33.21);

// hold register write String
// hold register write String,偶数长度
plc.writeString(2, "1234");

plc.close();
Expand Down
69 changes: 68 additions & 1 deletion tutorial/README-Modbus-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,73 @@

[HOME BACK](../README.md)

## Foreword

> Tips
- **1** coil = **1** bit
- **1** register address = **2** bytes;
- [ register start address = **0001** ] equal to [ access address = **0** ], there is a **1** address offset;
- The encoding format of **4 bytes** data = **BA_DC**; (big-endian mode = **DC_BA**,little-endian mode = **AB_CD**)

> Area
| Area Number | Name | Read/Write | Address Range | Method |
|:-----------:|:---------------|:-----------|:-------------:|:-------------------------------------|
| 0 area | output coil | read/write | 00001-09999 | readCoil / writeCoil |
| 1 area | input coil | read | 10001-19999 | readDiscreteInput |
| 3 area | input register | read | 30001-39999 | readInputRegister |
| 4 area | hold register | read/write | 40001-49999 | readHoldRegister / writeHoldRegister |

> Function Code
| Function Code | Description | Method |
|:-------------:|:------------------------|:------------------|
| 01H | read output coil | readCoil |
| 02H | read input coil | readDiscreteInput |
| 03H | read hold register | readHoldRegister |
| 04H | read input register | readInputRegister |
| 05H | write single coil | writeCoil |
| 06H | write single register | writeHoldRegister |
| 0FH | write multiple coil | writeCoil |
| 10H | write multiple register | writeHoldRegister |

> Hold Register Quick Access
| Method | Register Count | Size in Byte | Size in Bit | Register |
|:-------------|:--------------:|:------------:|:-----------:|:-------------:|
| readBoolean | 1 | 1/8 | 1 | hold register |
| readInt16 | 1 | 2 | 16 | hold register |
| readUInt16 | 1 | 2 | 16 | hold register |
| readInt32 | 2 | 4 | 32 | hold register |
| readUInt32 | 2 | 4 | 32 | hold register |
| readFloat32 | 2 | 4 | 32 | hold register |
| readFloat64 | 4 | 8 | 64 | hold register |
| readString | n | 2n | 16n | hold register |
| writeInt16 | 1 | 2 | 16 | hold register |
| writeUInt16 | 1 | 2 | 16 | hold register |
| writeInt32 | 2 | 4 | 32 | hold register |
| writeUInt32 | 2 | 4 | 32 | hold register |
| writeFloat32 | 2 | 4 | 32 | hold register |
| writeFloat64 | 4 | 8 | 64 | hold register |
| writeString | n | 2n | 16n | hold register |

## Print Message

If you want to know the actual input and output of packets during communication, you can print packet information.

```java
class Demo {
public static void main(String[] args) {
ModbusTcp plc = new ModbusTcp(1, "127.0.0.1");
// print message
plc.setComCallback(x -> System.out.printf("Length[%d]:%s%n", x.length, HexUtil.toHexString(x)));
plc.writeInt16(2, (short) 10);
plc.close();
}
}
```

## Communication Connection

- By default, the long connection mode is adopted. You need to close connection manually when it is not in use.
Expand Down Expand Up @@ -122,7 +189,7 @@ class Demo {
// hold register write float64
plc.writeFloat64(2, 33.21);

// hold register write String
// hold register write String, even length
plc.writeString(2, "1234");

plc.close();
Expand Down
Loading

0 comments on commit 27ee4d8

Please sign in to comment.