严智鑫
2025-06-13 d14cdaf28222bfef468185e34de7c823f1436b19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.mes;
 
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.mes.common.JsonConversion;
import com.mes.common.ReadFile;
import com.mes.connect.IndustrialInterface.Api;
import com.mes.connect.IndustrialInterface.IndustrialClient;
import com.mes.connect.Thread.MachineThread;
import com.mes.connect.entity.LogicConfig;
import com.mes.connect.entity.PlcParameters;
import com.mes.connect.s7.S7Client;
import com.mes.connect.s7.S7ClientOld;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Types;
import java.util.*;
 
/**
 * @Author : yanzhixin
 * @Date: 2024/3/27 16:37
 * @Description:
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = plcConnectModuleApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class plcConnectModuleApplicationTest {
 
    @Autowired
    Api api;
 
    @Test
    public void testb() {
        //byte[] resultValues=new byte[]{99, 23, 67, 76, 45, 49, 48, 45, 56, 48, 47, 55, 48, 67, 76, 45, 51, 45, 55, 51};
        String s="白玻";
        log.info(s);
    }
    @Test
    public void testReadJson() {
        try {
            LogicConfig logicConfig=JsonConversion.jsonToObjectByJackson(ReadFile.readJson("shelfLogic.json").toString(), LogicConfig.class);
            PlcParameters plcParameters=JsonConversion.jsonToObjectByJackson(ReadFile.readJson("shelf.json").toString(), PlcParameters.class);
            log.info("{},{}",logicConfig,plcParameters);
        }catch (Exception e) {
 
        }
    }
    @Test
    public void testReadS7Old() throws IOException {
        S7PLC s7PLC = new S7PLC(EPlcType.S1500, "10.153.19.191", 102, 0, 0);
        s7PLC.connect();
        int value= s7PLC.readUInt16("DB8.6");
        log.info("{},{}",value,s7PLC.checkConnected());
        s7PLC.close();
    }
    @Test
    public void testReadS7OldClient() throws IOException {
        IndustrialClient client = new S7ClientOld("S1500","10.153.19.191", 102, 0, 1);
        client.connect();
        int value=client.readRegister("S7.DB8.DBW6");
        log.info("{}",value);
        client.disconnect();
    }
    @Test
    public void testReadS7() throws IOException {
        IndustrialClient client=new S7Client("10.153.19.42", 102, 0, 1);;
        client.connect();
        // 连接到 PLC
        System.out.println("成功连接到 PLC");
 
//        // 读取单个位
//        boolean bitValue = client.readBit("S7.DB1.0");
//        System.out.println("DB1.DBX0.0 的值: " + bitValue);
//
//        // 写入单个位
//        client.writeBit("DB1.DBX0.1", true);
//        System.out.println("已将 DB1.DBX0.1 设置为 true");
 
        // 读取单个寄存器 (Word)
        int registerValue = client.readRegister("DB1.DBW2");
        System.out.println("DB1.DBW2 的值: " + registerValue);
//
//        // 写入单个寄存器
//        client.writeRegister("DB1.DBW4", 12345);
//        System.out.println("已将 DB1.DBW4 设置为 12345");
//
//        // 读取多个寄存器
//        int[] registers = client.readRegisters("DB1.DBW6", 3);
//        System.out.println("DB1.DBW6-DB1.DBW11 的值: " +
//                registers[0] + ", " +
//                registers[1] + ", " +
//                registers[2]);
//
//        // 写入多个寄存器
//        int[] values = {6789, 101112, 131415};
//        client.writeRegisters("DB1.DBW12", values);
//        System.out.println("已写入多个寄存器值");
//
//        // 读取浮点数
//        float floatValue = client.readFloat("DB1.DBD18");
//        System.out.println("DB1.DBD18 的浮点值: " + floatValue);
//
//        // 写入浮点数
//        client.writeFloat("DB1.DBD22", 3.14159f);
//        System.out.println("已将 DB1.DBD22 设置为 3.14159");
//
//        // 读取字符串
//        String stringValue = client.readString("DB1.DBW26", 10);
//        System.out.println("DB1.DBW26 开始的字符串: " + stringValue);
//
//        // 写入字符串
//        client.writeString("DB1.DBW36", "Hello PLC");
//        System.out.println("已写入字符串 'Hello PLC'");
    }
    @Test
    public void testApi() {
        //Http接口测试
//        Map<String, Object> data=new HashMap<>();
//        data.put("method","POST");
//        String resultHttp[]= api.httpApi("http://localhost:8082/account/testApi",data);
//        log.info("{}",resultHttp);
 
        // 查询带单个参数的视图
//        Map<String, Object> params = new HashMap<>();
//        params.put("width", 699); // 参数名与SQL中的列名一致
//        String resultView[]= api.viewApi("procedurename",null);
//        log.info("{}",resultView);
 
        // 示例 调用testProcedure存储过程
//        Map<String, Object> params = new HashMap<>();
//        params.put("dataParmars", "测试数据"); // 输入参数,对应存储过程中的IN参数
//        Map<String, Object> outParams = new HashMap<>();
//        outParams.put("messagedate", Types.VARCHAR); // 输出参数,对应存储过程中的OUT参数
//        String[] resultProcedure = api.procedureAPI("testProcedure", params, outParams);
//        // 打印执行结果
//        log.info("{}存储过程执行结果:{}",params,resultProcedure);
    }
    @Test
    public void testMainThread() {
        Thread thread = new Thread(() -> {
            while (true) {
                log.info(new Date().toString());
            }
 
        });
        thread.start();
    }
    @Test
    public void testMain() {
 
    }
 
}