自动化立体仓库 - WCS系统
#
luxiaotao1123
2020-08-05 3e3959bf84c673b3cb366e6225a6b5c8dfbea903
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
package com.zy.common.HslCommunication.ModBus;
 
import com.zy.common.HslCommunication.Core.Types.OperateResultExOne;
import com.zy.common.HslCommunication.StringResources;
import com.zy.common.HslCommunication.Utilities;
 
/**
 * Modbus协议相关的一些信息
 */
public class ModbusInfo {
 
 
    /**
     * 读取线圈
     */
    public static final byte ReadCoil = 0x01;
 
    /**
     * 读取离散量
     */
    public static final byte ReadDiscrete = 0x02;
 
 
    /**
     * 读取寄存器
     */
    public static final byte ReadRegister = 0x03;
 
    /**
     * 读取输入寄存器
     */
    public static final byte ReadInputRegister = 0x04;
 
 
    /**
     * 写单个线圈
     */
    public static final byte WriteOneCoil = 0x05;
 
    /**
     * 写单个寄存器
     */
    public static final byte WriteOneRegister = 0x06;
 
 
    /**
     * 写多个线圈
     */
    public static final byte WriteCoil = 0x0F;
 
    /**
     * 写多个寄存器
     */
    public static final byte WriteRegister = 0x10;
 
 
    /*****************************************************************************************
     *
     *    本服务器和客户端支持的异常返回
     *
     *******************************************************************************************/
 
 
    /**
     * 不支持该功能码
     */
    public static final byte FunctionCodeNotSupport = 0x01;
 
    /**
     * 该地址越界
     */
    public static final byte FunctionCodeOverBound = 0x02;
 
    /**
     * 读取长度超过最大值
     */
    public static final byte FunctionCodeQuantityOver = 0x03;
 
    /**
     * 读写异常
     */
    public static final byte FunctionCodeReadWriteException = 0x04;
 
 
    /**
     * 将modbus指令打包成Modbus-Tcp指令
     *
     * @param value Modbus指令
     * @param id    消息的序号
     * @return Modbus-Tcp指令
     */
    public static byte[] PackCommandToTcp(byte[] value, int id) {
        byte[] buffer = new byte[value.length + 6];
        buffer[0] = Utilities.getBytes(id)[1];
        buffer[1] = Utilities.getBytes(id)[0];
 
        buffer[4] = Utilities.getBytes(value.length)[1];
        buffer[5] = Utilities.getBytes(value.length)[0];
 
        System.arraycopy(value, 0, buffer, 6, value.length);
        return buffer;
    }
 
 
    /**
     * 分析Modbus协议的地址信息,该地址适应于tcp及rtu模式
     *
     * @param address         带格式的地址,比如"100","x=4;100","s=1;100","s=1;x=4;100"
     * @param isStartWithZero 起始地址是否从0开始
     * @return 转换后的地址信息
     */
    public static OperateResultExOne<ModbusAddress> AnalysisReadAddress(String address, boolean isStartWithZero) {
        try {
            ModbusAddress mAddress = new ModbusAddress(address);
            if (!isStartWithZero) {
                if (mAddress.getAddress() < 1) throw new Exception("地址值在起始地址为1的情况下,必须大于1");
                mAddress.setAddress(mAddress.getAddress() - 1);
            }
            return OperateResultExOne.CreateSuccessResult(mAddress);
        } catch (Exception ex) {
            OperateResultExOne<ModbusAddress> resultExOne = new OperateResultExOne<>();
            resultExOne.Message = ex.getMessage();
            return resultExOne;
        }
    }
 
 
    /**
     * 通过错误码来获取到对应的文本消息
     *
     * @param code 错误码
     * @return 错误的文本描述
     */
    public static String GetDescriptionByErrorCode(byte code) {
        switch (code) {
            case ModbusInfo.FunctionCodeNotSupport:
                return StringResources.Language.ModbusTcpFunctionCodeNotSupport();
            case ModbusInfo.FunctionCodeOverBound:
                return StringResources.Language.ModbusTcpFunctionCodeOverBound();
            case ModbusInfo.FunctionCodeQuantityOver:
                return StringResources.Language.ModbusTcpFunctionCodeQuantityOver();
            case ModbusInfo.FunctionCodeReadWriteException:
                return StringResources.Language.ModbusTcpFunctionCodeReadWriteException();
            default:
                return StringResources.Language.UnknownError();
        }
    }
 
 
}