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