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)); } }