张世豪
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
package yaokong;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
import chuankou.SerialPortService;
import chuankou.sendmessage;
 
/**
 * Control07.java - 遥控大灯开关指令类
 * 
 * 指令类型: 0x07
 * 功能: 控制割草机大灯的开关
 * 
 * 数据格式:
 *   开关状态(1字节): 0=关闭,1=开启
 *   保留字段(3字节): 0x00填充
 * 
 * 示例用法:
 *   1. 开启大灯: Control07.buildLightCommandHex("1") 或 Control07.lightOn()
 *   2. 关闭大灯: Control07.buildLightCommandHex("0") 或 Control07.lightOff()
 *   3. 发送开灯指令: Control07.sendLightOnIfDebugSerialOpen()
 *   4. 发送关灯指令: Control07.sendLightOffIfDebugSerialOpen()
 */
public class Control07 {
    
    /**
     * 构建遥控大灯开关指令(指令类型0x07)的HEX格式字符串
     * 
     * @param lightValueStr 大灯开关状态字符串:"0"表示关闭,"1"表示开启
     * @return 大灯控制指令的HEX格式字符串
     */
    public static String buildLightCommandHex(String lightValueStr) {
        int lightValue = parseLightValue(lightValueStr);
        byte[] commandBytes = buildLightCommandBytes((byte) lightValue);
        return bytesToHex(commandBytes);
    }
    
    /**
     * 构建遥控大灯开关指令的字节数组
     * 
     * @param lightValue 大灯开关状态:0=关闭,1=开启
     * @return 大灯控制指令的字节数组
     */
    public static byte[] buildLightCommandBytes(byte lightValue) {
        // 验证开关状态范围
        if (lightValue != 0 && lightValue != 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);
        
        // 指令类型 (0x07)
        buffer.put((byte) 0x07);
        
        // 数据长度
        buffer.putShort((short) dataLength);
        
        // 序列号
        short sequence = (short) BluetoothProtocol.getNextSequence();
        buffer.putShort(sequence);
        
        // 大灯开关状态
        buffer.put(lightValue);
        
        // 保留字段 (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) 0x07); // 指令类型
        tempBuffer.putShort((short) dataLength); // 数据长度
        tempBuffer.putShort(sequence); // 序列号
        tempBuffer.put(lightValue); // 大灯开关状态
        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 sendLightOnIfDebugSerialOpen() {
        return sendLightCommandIfDebugSerialOpen((byte) 1);
    }
    
    /**
     * 当调试串口打开时发送关闭大灯指令
     * 
     * @return 发送成功返回true,否则返回false
     */
    public static boolean sendLightOffIfDebugSerialOpen() {
        return sendLightCommandIfDebugSerialOpen((byte) 0);
    }
    
    /**
     * 快捷方法:构建开启大灯指令
     * 
     * @return 开灯指令的HEX字符串
     */
    public static String lightOn() {
        return buildLightCommandHex("1");
    }
    
    /**
     * 快捷方法:构建关闭大灯指令
     * 
     * @return 关灯指令的HEX字符串
     */
    public static String lightOff() {
        return buildLightCommandHex("0");
    }
    
    /**
     * 解析大灯开关状态字符串
     */
    private static int parseLightValue(String lightStr) {
        try {
            int light = Integer.parseInt(lightStr.trim());
            if (light != 0 && light != 1) {
                throw new IllegalArgumentException("大灯开关状态必须是0或1");
            }
            return light;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("大灯开关状态必须是整数: " + lightStr);
        }
    }
    
    /**
     * 发送大灯控制指令(内部方法)
     */
    private static boolean sendLightCommandIfDebugSerialOpen(byte lightValue) {
        SerialPortService service = sendmessage.getActiveService();
        if (service == null || !service.isOpen()) {
            return false;
        }
        byte[] payload = buildLightCommandBytes(lightValue);
        
        // 调试:打印发送的数据
        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 testBuildLightCommand() {
        System.out.println("=== 测试大灯控制指令构建 ===");
        
        // 测试1:开灯指令
        byte[] cmd1 = buildLightCommandBytes((byte)1);
        System.out.println("开启大灯: " + bytesToHex(cmd1));
        
        // 测试2:关灯指令
        byte[] cmd2 = buildLightCommandBytes((byte)0);
        System.out.println("关闭大灯: " + bytesToHex(cmd2));
    }
}