严智鑫
2024-12-03 def1eb8623e1444164ae4bce9179d011a89b8c5e
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
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 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.HashMap;
import java.util.Map;
 
import static com.mes.tools.HexConversion.*;
 
/**
 * Plc参数
 */
@Component
@Slf4j
public class PlcParameter {
 
 
    /**
     * 编号
     */
    private String CodeId;
    /**
     * 起始地址
     */
    private int addressStart=0;
    /**
     * 长度
     */
    private int addressLength=0;
    /**
     * 类型
     */
    private String type="int";
 
    /**
     * 实时读取的byte值
     */
    private byte[] readByte=null;
    /**
     * 实时读取的byte值转换成 对应类型
     */
    private Object readValue=null;
 
    /**
     * 需要写入的值
     */
    private Object writeValue=null;
 
    /**
     * 需要写入的值
     */
    private byte[] writeByte=null;
 
    PlcParameter(){
 
    }
    PlcParameter(String codeId, int addressStart, int addressLength, String type){
        this.CodeId=codeId;
        this.addressStart=addressStart;
        this.addressLength=addressLength;
        this.type=type;
    }
 
    public String getCodeId() {
        return CodeId;
    }
 
    public void setCodeId(String codeId) {
        CodeId = codeId;
    }
 
    public int getAddressStart() {
        return addressStart;
    }
 
    public void setAddressStart(int addressIndex) {
        this.addressStart = addressStart;
    }
 
    public int getAddressLength() {
        return addressLength;
    }
 
    public void setAddressLength(int addressLength) {
        this.addressLength = addressLength;
    }
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    public Object getReadValue() {
 
        return readValue;
    }
    public String getValueString() {
        return getValueInt()+"";
    }
    public int getValueInt() {
        if(this.readByte==null||this.readByte.length<1){
            //log.info("读取内容为null: {}  :{}",this.getCodeId(),this.readByte);
            return 0;
        }
        return bytesToIntDesc(this.readByte,0);
    }
 
    public void setReadValue(Object readValue) {
        this.readValue = readValue;
    }
 
    public byte[] getReadByte() {
        return this.readByte;
    }
 
    public void setReadByte(byte[] readByte) {
        this.readByte = readByte;
    }
 
    public Object getWriteValue() {
        return writeValue;
    }
 
    public byte [] setWriteValue(Object writeValue) {
        //传入值根据参数类型进行转换成字符串保存进写入 字节内并且返回
        byte []sendByte=new byte[13+this.addressLength];
        if ("int".equals(this.type)){
            this.writeByte=intToBytesDesc(Integer.parseInt(writeValue.toString()),this.addressLength);
            return this.writeByte;
        }else if ("word".equals(this.type)){
            this.writeByte=intToBytesDesc(Integer.parseInt(writeValue.toString()),this.addressLength);
            return this.writeByte;
        }else if("string".equals(this.type)){
 
        }else{
 
        }
        return null;
    }
    public byte [] getWriteByte() {
        return this.writeByte;
    }
}