自动化立体仓库 - WCS系统
#
luxiaotao1123
2020-08-04 6abc20e29568c129f4ca71eccec9310534a8c779
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.zy.common.HslCommunication.ModBus;
 
import com.zy.common.HslCommunication.BasicFramework.SoftBasic;
import com.zy.common.HslCommunication.Core.Address.DeviceAddressBase;
import com.zy.common.HslCommunication.Utilities;
 
 
/**
 * Modbus协议地址格式,可以携带站号,功能码,地址信息
 */
public class ModbusAddress extends DeviceAddressBase {
 
 
 
    /**
     * 实例化一个默认的对象
     */
    public ModbusAddress() {
        Station = -1;
        Function = ModbusInfo.ReadRegister;
        setAddress(0);
    }
 
 
    /**
     * 实例化一个默认的对象,使用默认的地址初始化
     * @param address
     */
    public ModbusAddress(String address) {
        Station = -1;
        Function = ModbusInfo.ReadRegister;
        setAddress(0);
        AnalysisAddress(address);
    }
 
 
    /**
     * 获取站号
     * @return
     */
    public int getStation() {
        return Station;
    }
 
    /**
     * 设置站号
     * @param station
     */
    public void setStation(int station) {
        Station = station;
    }
 
 
    /**
     * 获取功能码
     * @return
     */
    public byte getFunction() {
        return Function;
    }
 
    /**
     * 设置功能码
     * @param function
     */
    public void setFunction(byte function) {
        Function = function;
    }
 
    private int Station = -1;
 
    private byte Function = -1;
 
 
 
    /**
     * 解析Modbus的地址码
     * @param address 地址
     */
    @Override
    public void AnalysisAddress(String address) {
        if (address.indexOf(';') < 0) {
            // 正常地址,功能码03
            setAddress(Integer.parseInt(address));
        } else {
            // 带功能码的地址
            String[] list = address.split(";");
            for (int i = 0; i < list.length; i++) {
                if (list[i].charAt(0) == 's' || list[i].charAt(0) == 'S') {
                    // 站号信息
                    this.Station = Integer.parseInt(list[i].substring(2));
                } else if (list[i].charAt(0) == 'x' || list[i].charAt(0) == 'X') {
                    this.Function = (byte) Integer.parseInt(list[i].substring(2));
                } else {
                    setAddress(Integer.parseInt(list[i]));
                }
            }
        }
    }
 
 
 
    /**
     * 创建一个读取线圈的字节对象
     * @param station 读取的站号
     * @param length 读取数据的长度
     * @return 原始的modbus指令
     */
    public byte[] CreateReadCoils(byte station, int length) {
        byte[] buffer = new byte[6];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = ModbusInfo.ReadCoil;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = Utilities.getBytes(length)[1];
        buffer[5] = Utilities.getBytes(length)[0];
        return buffer;
    }
 
    /**
     * 创建一个读取离散输入的字节对象
     * @param station 读取的站号
     * @param length 读取数据的长度
     * @return 原始的modbus指令
     */
    public byte[] CreateReadDiscrete(byte station, int length) {
        byte[] buffer = new byte[6];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = ModbusInfo.ReadDiscrete;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = Utilities.getBytes(length)[1];
        buffer[5] = Utilities.getBytes(length)[0];
        return buffer;
    }
 
 
 
    /**
     * 创建一个读取寄存器的字节对象
     * @param station 读取的站号
     * @param length 读取数据的长度
     * @return 原始的modbus指令
     */
    public byte[] CreateReadRegister(byte station, int length) {
        byte[] buffer = new byte[6];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = Function;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = Utilities.getBytes(length)[1];
        buffer[5] = Utilities.getBytes(length)[0];
        return buffer;
    }
 
 
    /**
     * 创建一个写入单个线圈的指令
     * @param station 站号
     * @param value 值
     * @return 原始的modbus指令
     */
    public byte[] CreateWriteOneCoil(byte station, boolean value) {
        byte[] buffer = new byte[6];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = ModbusInfo.WriteOneCoil;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = (byte) (value ? 0xFF : 0x00);
        buffer[5] = 0x00;
        return buffer;
    }
 
 
 
    /**
     * 创建一个写入单个寄存器的指令
     * @param station 站号
     * @param data 值
     * @return 原始的modbus指令
     */
    public byte[] CreateWriteOneRegister(byte station, byte[] data) {
        byte[] buffer = new byte[6];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = ModbusInfo.WriteOneRegister;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = data[1];
        buffer[5] = data[0];
        return buffer;
    }
 
 
 
    /**
     * 创建一个写入批量线圈的指令
     * @param station 站号
     * @param values 值
     * @return 原始的modbus指令
     */
    public byte[] CreateWriteCoil(byte station, boolean[] values) {
        byte[] data = SoftBasic.BoolArrayToByte(values);
        byte[] buffer = new byte[7 + data.length];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = ModbusInfo.WriteCoil;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = (byte) (values.length / 256);
        buffer[5] = (byte) (values.length % 256);
        buffer[6] = (byte) (data.length);
        System.arraycopy(data, 0, buffer, 7, data.length);
        return buffer;
    }
 
 
 
    /**
     * 创建一个写入批量寄存器的指令
     * @param station 站号
     * @param values 值
     * @return 原始的modbus指令
     */
    public byte[] CreateWriteRegister(byte station, byte[] values) {
        byte[] buffer = new byte[7 + values.length];
        buffer[0] = this.Station < 0 ? station : (byte) this.Station;
        buffer[1] = ModbusInfo.WriteRegister;
        buffer[2] = Utilities.getBytes(this.getAddress())[1];
        buffer[3] = Utilities.getBytes(this.getAddress())[0];
        buffer[4] = (byte) (values.length / 2 / 256);
        buffer[5] = (byte) (values.length / 2 % 256);
        buffer[6] = (byte) (values.length);
        System.arraycopy(values, 0, buffer, 7, values.length);
        return buffer;
    }
 
 
 
    /**
     * 地址新增指定的数
     * @param value 地址值
     * @return 新增后的地址信息
     */
    public ModbusAddress AddressAdd(int value) {
        ModbusAddress address = new ModbusAddress();
        address.setAddress(getAddress() + value);
        address.setFunction(getFunction());
        address.setStation(getStation());
        return address;
    }
 
 
 
    /**
     * 地址新增1
     * @return 新增后的地址信息
     */
    public ModbusAddress AddressAdd() {
        return AddressAdd(1);
    }
 
 
 
    /**
     * 获取本对象的字符串表示形式
     * @return 字符串数据
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (Station >= 0) sb.append("s=" + Station + ";");
        if (Function >= 1) sb.append("x=" + Function + ";");
        sb.append(String.valueOf(getAddress()));
 
        return sb.toString();
    }
 
}