张世豪
8 天以前 c57cb0cd9feb4495c89246b2faec6d5e45c23c30
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
package yaokong;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
import chuankou.SerialPortService;
import chuankou.sendmessage;
 
/**
 * Control05.java - 遥控机器启动关闭指令类
 * 
 * 指令类型: 0x05
 * 功能: 控制割草机整机电源的启动与关闭
 * 
 * 数据格式:
 *   控制值(1字节): 0=关闭,1=启动
 *   保留字段(3字节): 0x00填充
 * 
 * 示例用法:
 *   1. 启动机器: Control05.buildPowerCommandHex("1") 或 Control05.powerOn()
 *   2. 关闭机器: Control05.buildPowerCommandHex("0") 或 Control05.powerOff()
 *   3. 发送指令: Control05.sendPowerOnIfDebugSerialOpen()
 */
public class Control05 {
    
    /**
     * 构建遥控机器启动关闭指令(指令类型0x05)的HEX格式字符串
     * 
     * @param powerValue 电源控制值字符串:"0"表示关闭,"1"表示启动
     * @return 电源控制指令的HEX格式字符串
     */
    public static String buildPowerCommandHex(String powerValue) {
        // 解析控制值
        int power = parsePowerValue(powerValue);
        
        byte[] commandBytes = buildPowerCommandBytes((byte) power);
        return bytesToHex(commandBytes);
    }
    
    /**
     * 构建遥控机器启动关闭指令的字节数组
     * 
     * @param powerValue 电源控制值:0=关闭,1=启动
     * @return 电源控制指令的字节数组
     */
    public static byte[] buildPowerCommandBytes(byte powerValue) {
        // 验证控制值范围
        if (powerValue != 0 && powerValue != 1) {
            throw new IllegalArgumentException("电源控制值必须是0或1");
        }
        
        int dataLength = 4; // 控制值1字节 + 保留字段3字节
        
        ByteBuffer buffer = ByteBuffer.allocate(14); // 总长度14字节
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        
        // 帧头
        buffer.put(BluetoothProtocol.FRAME_HEADER);
        
        // 指令类型 (0x05)
        buffer.put((byte) 0x05);
        
        // 数据长度
        buffer.putShort((short) dataLength);
        
        // 序列号
        short sequence = (short) BluetoothProtocol.getNextSequence();
        buffer.putShort(sequence);
        
        // 控制值
        buffer.put(powerValue);
        
        // 保留字段 (3字节,全部填0)
        buffer.put((byte) 0x00);
        buffer.put((byte) 0x00);
        buffer.put((byte) 0x00);
        
        // 计算CRC16(从指令类型开始到数据内容结束)
        byte[] dataForCRC = new byte[9];
        ByteBuffer tempBuffer = ByteBuffer.allocate(9);
        tempBuffer.order(ByteOrder.LITTLE_ENDIAN);
        tempBuffer.put((byte) 0x05); // 指令类型
        tempBuffer.putShort((short) dataLength); // 数据长度
        tempBuffer.putShort(sequence); // 序列号
        tempBuffer.put(powerValue); // 控制值
        tempBuffer.put((byte) 0x00); // 保留字段1
        tempBuffer.put((byte) 0x00); // 保留字段2
        tempBuffer.put((byte) 0x00); // 保留字段3
        
        dataForCRC = tempBuffer.array();
        int crc = CRC16.calculateCRC16(dataForCRC, 0, dataForCRC.length);
        buffer.putShort((short) crc);
        
        // 帧尾
        buffer.put((byte) 0x0D);
        
        return buffer.array();
    }
    
    /**
     * 当调试串口打开时发送启动机器指令
     * 
     * @return 发送成功返回true,否则返回false
     */
    public static boolean sendPowerOnIfDebugSerialOpen() {
        return sendPowerCommandIfDebugSerialOpen((byte) 1);
    }
    
    /**
     * 当调试串口打开时发送关闭机器指令
     * 
     * @return 发送成功返回true,否则返回false
     */
    public static boolean sendPowerOffIfDebugSerialOpen() {
        return sendPowerCommandIfDebugSerialOpen((byte) 0);
    }
    
    /**
     * 快捷方法:构建启动机器指令
     * 
     * @return 启动指令的HEX字符串
     */
    public static String powerOn() {
        return buildPowerCommandHex("1");
    }
    
    /**
     * 快捷方法:构建关闭机器指令
     * 
     * @return 关闭指令的HEX字符串
     */
    public static String powerOff() {
        return buildPowerCommandHex("0");
    }
    
    /**
     * 解析控制值字符串
     */
    private static int parsePowerValue(String powerStr) {
        try {
            int power = Integer.parseInt(powerStr.trim());
            if (power != 0 && power != 1) {
                throw new IllegalArgumentException("电源控制值必须是0或1");
            }
            return power;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("电源控制值必须是整数: " + powerStr);
        }
    }
    
    /**
     * 发送电源控制指令(内部方法)
     */
    private static boolean sendPowerCommandIfDebugSerialOpen(byte powerValue) {
        SerialPortService service = sendmessage.getActiveService();
        if (service == null || !service.isOpen()) {
            return false;
        }
        byte[] payload = buildPowerCommandBytes(powerValue);
        
        // 调试:打印发送的数据
        System.out.println("发送电源控制指令: " + bytesToHex(payload));
        
        return sendmessage.sendViaActive(payload);
    }
    
    /**
     * 字节数组转换为HEX字符串
     */
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
            if (i < bytes.length - 1) {
                hexString.append(' ');
            }
        }
        return hexString.toString().toUpperCase();
    }
    
    /**
     * 测试函数:验证电源控制指令构建
     */
    public static void testBuildPowerCommand() {
        System.out.println("=== 测试电源控制指令构建 ===");
        
        // 测试1:启动指令
        byte[] cmd1 = buildPowerCommandBytes((byte)1);
        System.out.println("启动机器: " + bytesToHex(cmd1));
        
        // 测试2:关闭指令
        byte[] cmd2 = buildPowerCommandBytes((byte)0);
        System.out.println("关闭机器: " + bytesToHex(cmd2));
    }
}