严智鑫
2024-09-05 7da8422f45751fffd5666a0260196a3d9605b7dc
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
package com.mes.service;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.mes.device.PlcParameterInfo;
import com.mes.tools.HexConversion;
import com.mes.utils.HexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.*;
import java.net.Socket;
import java.util.*;
 
/**
 * Plc协议:协议参数,协议配置,协议请求头,协议类型
 */
@Component
@Slf4j
public class PlcAgreement {
 
    public Socket socket =null;//通讯
    /**
     * 协议参数
     */
    private List<String> parameterKeys=null;
    private Map<String,PlcParameter> parameters=null;
    /**
     * 协议路径
     */
    private String jsonFilePath=null;
    /**
     * 读取起始地址
     */
    public String plcAddressBegin=null;
    /**
     * 读取长度
     */
    public int plcAddressLength=0;
    //类似序列号(4)+协议标志(4)+长度(4)+从站地址(2)+功能代码(2)+起始地址(4)+读取数量(4) "000100000006010300000032"
    public String requestHead=null;
 
    PlcAgreement(){
        jsonFilePath = System.getProperty("user.dir") + "../../JsonFile/PlcCacheGlass.json";
        boolean initSuccess=initword();
        log.info("初始化PlcCacheGlass:"+initSuccess);
    }
    //初始化word
    public boolean initword() {
        try {
            parameters=new LinkedHashMap<String,PlcParameter>();
            FileReader fileReader = new FileReader(jsonFilePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
 
            StringBuilder content = new StringBuilder();
            String line;
 
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line);
            }
            bufferedReader.close();
            fileReader.close();
 
            JSONObject jsonFile = new JSONObject(content.toString());
 
            JSONArray jsonArray = jsonFile.getJSONArray("parameterInfo");
 
            this.plcAddressBegin=jsonFile.getStr("plcAddressBegin");//设置起始位地址
            this.plcAddressLength=Integer.valueOf(jsonFile.getStr("plcAddressLength"));//设置地址长度
            this.requestHead=jsonFile.getStr("requestHead");//设置请求头部
 
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject parameterObj = jsonArray.getJSONObject(i);
                String code = parameterObj.getStr("code");
                PlcParameter plcParameter = new PlcParameter(
                        code,
                        Integer.valueOf(parameterObj.getStr("addressIndex")),
                        Integer.valueOf(parameterObj.getStr("addressLength")),""); //参数实例
                parameterKeys.add(code);
                parameters.put(code,plcParameter);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
 
    //读取数据
    public void read()throws Exception{
        int bufSizes = 0;
        byte[] msgs = new byte[2048];
        //写入读取地址
        DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
        outToServer.write(HexConversion.stringToInt(this.requestHead));
        outToServer.flush();
        //读取内容
        DataInputStream in = new DataInputStream(socket.getInputStream());
        bufSizes = in.read(msgs);
        String message = HexConversion.byteToHexString(bufSizes, msgs);//十进制字节数组转十六进制字符串
        //获取参数值
        for (String key:parameters.keySet()){
            parameters.get(key).setReadValue(message);
        }
    }
    //写入数据
    public void write(String key,String writeValue)throws Exception{
        parameters.get(key);
        if (writeValue != null && !"".equals(writeValue)) {
            //写入发送数据
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.write(HexConversion.stringToInt(writeValue));
            out.flush();
        }
    }
    //写
    public String message(String senddate, String address) {
        String Herd = "0110" + address;
        int length = senddate.length() / 4;
        String dates = Herd + HexUtil.intTo2ByteHex(length) + HexUtil.intTo1ByteHex(length * 2) + senddate;
        int lengths = dates.length() / 2;
        String date = "00000000" + HexUtil.intTo2ByteHex(lengths) + dates;
        return date;
    }
 
    public String getValueString(String key){
        return parameters.get(key).toString();
    }
    public int getValueInt(){
        return 0;
    }
    public double getValueDouble(){
        return 0;
    }
}