diff --git a/.vscode/settings.json b/.vscode/settings.json index 0422cc8..5f4f2bf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,5 +56,16 @@ "C_Cpp_Runner.useLeakSanitizer": false, "C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "optional": "cpp", + "istream": "cpp", + "ostream": "cpp", + "system_error": "cpp", + "array": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp" + } } \ No newline at end of file diff --git a/VSMD通信协议整理.pdf b/VSMD通信协议整理.pdf new file mode 100644 index 0000000..fe7a4db Binary files /dev/null and b/VSMD通信协议整理.pdf differ diff --git a/device_config.json b/device_config.json index aff581a..2bf40d0 100644 --- a/device_config.json +++ b/device_config.json @@ -1,65 +1,73 @@ -{ - "cmd": "cfg", - "params": { - "cfg zmd": 1, - "cfg snr": 0, - "cfg osv": 0, - "cfg zsd": 1200, - "cfg zsp": 2400, - "cfg dmd": 2, - "cfg dar": 5, - "cfg msr": 1, - "cfg msv": 1, - "cfg psr": 1, - "cfg psv": 1, - "cfg bdr": 115200, - "cfg cid": 1, - "cfg mcs": 5, - "cfg spd": 1200, - "cfg acc": 12000, - "cfg dec": 12000, - "cfg cra": 0.8, - "cfg crn": 0.4, - "cfg crh": 0.0 - }, - "actions": [ - { - "cmd": "ena" - }, - { - "cmd": "off" - }, - { - "cmd": "mov" - }, - { - "cmd": "pos", - "value": 10000 - }, - { - "cmd": "rmv", - "value": -6400 - }, - { - "cmd": "pps" - }, - { - "cmd": "pps", - "value": 12800 - }, - { - "cmd": "org" - }, - { - "cmd": "stp" - }, - { - "cmd": "zero", - "value": "start" - }, - { - "cmd": "zero", - "value": "stop" - } - ] -} \ No newline at end of file +{"command": "cfg"} +{"command": "ena"} +{"command": "off"} +{"command": "mov"} +{"command": "org"} +{"command": "dev"} +{"command": "sts"} +{"command": "zero_start"} +{"command": "zero_stop"} +{"command": "sav"} + +{"command": "pos", "PA": {"value": 1000}} +{"command": "rmv", "PA": {"value": 500}} +{"command": "pps", "PA": {"value": 1000}} +{"command": "stp", "PA": {"value": 1}} + +{"command": "cfg_bdr", "PA": {"value": 115200}} +{"command": "cfg_mcs", "PA": {"value": 5}} +{"command": "cfg_spd", "PA": {"value": 10000}} +{"command": "cfg_acc", "PA": {"value": 100000}} +{"command": "cfg_dec", "PA": {"value": 100000}} +{"command": "cfg_cra", "PA": {"value": 1.5}} +{"command": "cfg_crn", "PA": {"value": 1.0}} +{"command": "cfg_crh", "PA": {"value": 0.5}} + +{"command": "cfg_zmd", "PA": {"value": 2}} +{"command": "cfg_snr", "PA": {"value": 1}} +{"command": "cfg_osv", "PA": {"value": 0}} +{"command": "cfg_zsd", "PA": {"value": 5000}} +{"command": "cfg_zsp", "PA": {"value": 100}} + +{"command": "cfg_dmd", "PA": {"value": 1}} +{"command": "cfg_dar", "PA": {"value": 30}} + +{"command": "cfg_msr", "PA": {"value": 3}} +{"command": "cfg_msv", "PA": {"value": 0}} +{"command": "cfg_psr", "PA": {"value": 4}} +{"command": "cfg_psv", "PA": {"value": 1}} + +//编码器配置命令 +{"command": "cfg_pae", "PA": {"value": 1}} +{"command": "cfg_emod", "PA": {"value": 2}} +{"command": "cfg_elns", "PA": {"value": 1000}} +{"command": "cfg_estp", "PA": {"value": 200}} +{"command": "cfg_erty", "PA": {"value": 3}} +{"command": "cfg_edir", "PA": {"value": 0}} +{"command": "cfg_ez", "PA": {"value": 5}} +{"command": "cfg_ewr", "PA": {"value": 1}} + +//S3-S6传感器触发事件配置命令 +{"command": "cfg_s3f", "PA": {"value": 5}} +{"command": "cfg_s3r", "PA": {"value": 6}} +{"command": "cfg_s4f", "PA": {"value": 7}} +{"command": "cfg_s4r", "PA": {"value": 8}} +{"command": "cfg_s5f", "PA": {"value": 9}} +{"command": "cfg_s5r", "PA": {"value": 0}} +{"command": "cfg_s6f", "PA": {"value": 1}} +{"command": "cfg_s6r", "PA": {"value": 2}} + +{"command": "cfg_s3", "PA": {"value": 1}} +{"command": "cfg_s4", "PA": {"value": 0}} +{"command": "cfg_s5", "PA": {"value": 1}} +{"command": "cfg_s6", "PA": {"value": 0}} + +//S3-S6端口输出控制命令 +{"command": "s3_on"} +{"command": "s3_off"} +{"command": "s4_on"} +{"command": "s4_off"} +{"command": "s5_on"} +{"command": "s5_off"} +{"command": "s6_on"} +{"command": "s6_off"} diff --git a/src/VSMD_command_handler.cpp b/src/VSMD_command_handler.cpp new file mode 100644 index 0000000..f8486e6 --- /dev/null +++ b/src/VSMD_command_handler.cpp @@ -0,0 +1,2278 @@ +#include "VSMD_command_handler.h" +#include "IRIS_Method.h" +#include +#include "bluetooth_handler.h" + +#define JSON_BUFFER_SIZE 512 + +// 外部串口对象声明 +extern HardwareSerial DebugSerial; + +void sendJsonResponse(const char* command, const char* status, int value) { + StaticJsonDocument responseDoc; + + responseDoc["command"] = command; + responseDoc["ST"] = status; + + // 如果有参数值,添加到 PA 对象中 + if (value != -1) { + JsonObject params = responseDoc.createNestedObject("PA"); + params["value"] = value; + } + + // 可选:添加其他信息 + responseDoc["Other"] = ""; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + DebugSerial.print("JSON Response: "); + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + + // DebugSerial.println(); + // DebugSerial.print("Sent JSON Response: "); + // DebugSerial.println(jsonResponse); +} + +void sendStringResponse(const char* response) { + // 打包并发送字符串响应 + packedLength = IRIS_Protocol_Pack(0x01, strlen(response), (uint8_t*)response, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + + DebugSerial.print("Sent String Response: "); + DebugSerial.println(response); +} + +void processJsonCommand(const char* jsonString) { + StaticJsonDocument doc; + DeserializationError error = deserializeJson(doc, jsonString); + + if (error) { + DebugSerial.print("JSON parsing failed: "); + DebugSerial.println(error.c_str()); + sendJsonResponse("error", "JSON_PARSE_ERROR"); + return; + } + + // 使用 command 字段替代 cmd + const char* command = doc["command"]; + if (command == nullptr) { + sendJsonResponse("error", "NO_COMMAND_FIELD"); + return; + } + + // 获取 PA 参数对象(可选) + JsonObject params = doc["PA"]; + + // 根据command执行相应操作 + if (strcmp(command, "cfg") == 0) { + handle_cfg_cmd(); + } else if (strcmp(command, "ena") == 0) { + handle_ena_cmd(); + } else if (strcmp(command, "off") == 0) { + handle_off_cmd(); + } else if (strcmp(command, "mov") == 0) { + handle_mov_cmd(); + } else if (strcmp(command, "pos") == 0) { + int value = params.containsKey("value") ? params["value"].as() : 0; + handle_pos_cmd(value); + } else if (strcmp(command, "rmv") == 0) { + int value = params.containsKey("value") ? params["value"].as() : 0; + handle_rmv_cmd(value); + } else if (strcmp(command, "pps") == 0) { + if (params.containsKey("value")) { + int value = params["value"].as(); + handle_pps_cmd(value); + } else { + handle_pps_cmd(); + } + } else if (strcmp(command, "org") == 0) { + handle_org_cmd(); + } else if (strcmp(command, "stp") == 0) { + if (params.containsKey("value")) { + int value = params["value"].as(); + handle_stp_cmd(value); + } else { + handle_stp_cmd(); // 无参数,默认为0(减速停止) + } + } else if (strcmp(command, "dev") == 0) { + handle_dev_cmd(); + } else if (strcmp(command, "sts") == 0) { + handle_sts_cmd(); + } else if (strcmp(command, "zero_start") == 0) { + handle_zero_start_cmd(); + } else if (strcmp(command, "zero_stop") == 0) { + handle_zero_stop_cmd(); + } else if (strcmp(command, "cfg_bdr") == 0) { + int baudrate = params["value"].as(); + handle_cfg_bdr_cmd(baudrate); + } else if (strcmp(command, "cfg_mcs") == 0) { + //设置微步细分 + int mcs = params["value"].as(); + handle_cfg_mcs_cmd(mcs); + } else if (strcmp(command, "cfg_spd") == 0) { + //设置运行速度 + int spd = params["value"].as(); + handle_cfg_spd_cmd(spd); + } else if (strcmp(command, "cfg_acc") == 0) { + //设置加速度 + int acc = params["value"].as(); + handle_cfg_accSpeed_cmd(acc); + } else if (strcmp(command, "cfg_dec") == 0) { + //设置减速度 + int dec = params["value"].as(); + handle_cfg_decSpeed_cmd(dec); + } else if (strcmp(command, "cfg_cra") == 0) { + //设置加速时电流 + float cra = params["value"].as(); + handle_cfg_cra_cmd(cra); + } else if (strcmp(command, "cfg_crn") == 0) { + //设置匀速时电流 + float crn = params["value"].as(); + handle_cfg_crn_cmd(crn); + } else if (strcmp(command, "cfg_crh") == 0) { + //设置HOLD电流 + float crh = params["value"].as(); + handle_cfg_crh_cmd(crh); + // ***************************S3-S6传感器触发事件配置(参数范围0-9)**********************/ + } else if (strcmp(command, "cfg_s3f") == 0) { + int s3f = params["value"].as(); + handle_cfg_sensor_cmd("s3f", s3f); + } else if (strcmp(command, "cfg_s3r") == 0) { + int s3r = params["value"].as(); + handle_cfg_sensor_cmd("s3r", s3r); + } else if (strcmp(command, "cfg_s4f") == 0) { + int s4f = params["value"].as(); + handle_cfg_sensor_cmd("s4f", s4f); + } else if (strcmp(command, "cfg_s4r") == 0) { + int s4r = params["value"].as(); + handle_cfg_sensor_cmd("s4r", s4r); + } else if (strcmp(command, "cfg_s5f") == 0) { + int s5f = params["value"].as(); + handle_cfg_sensor_cmd("s5f", s5f); + } else if (strcmp(command, "cfg_s5r") == 0) { + int s5r = params["value"].as(); + handle_cfg_sensor_cmd("s5r", s5r); + } else if (strcmp(command, "cfg_s6f") == 0) { + int s6f = params["value"].as(); + handle_cfg_sensor_cmd("s6f", s6f); + } else if (strcmp(command, "cfg_s6r") == 0) { + int s6r = params["value"].as(); + handle_cfg_sensor_cmd("s6r", s6r); + // ***************************S3-S6传感器工作模式配置(参数范围0-1)**********************/ + } else if (strcmp(command, "cfg_s3") == 0) { + int s3 = params["value"].as(); + handle_cfg_sensor_cmd("s3", s3); + } else if (strcmp(command, "cfg_s4") == 0) { + int s4 = params["value"].as(); + handle_cfg_sensor_cmd("s4", s4); + } else if (strcmp(command, "cfg_s5") == 0) { + int s5 = params["value"].as(); + handle_cfg_sensor_cmd("s5", s5); + } else if (strcmp(command, "cfg_s6") == 0) { + int s6 = params["value"].as(); + handle_cfg_sensor_cmd("s6", s6); + //**************************************************************** */ + } else if (strcmp(command, "cfg_zmd") == 0) { + //设置归零功能 + int zmd = params["value"].as(); + handle_cfg_zmd_cmd(zmd); + } else if (strcmp(command, "cfg_snr") == 0) { + //设置归零用传感器 + int snr = params["value"].as(); + handle_cfg_snr_cmd(snr); + } else if (strcmp(command, "cfg_osv") == 0) { + //设置归零传感器OPEN时电平 + int osv = params["value"].as(); + handle_cfg_osv_cmd(osv); + } else if (strcmp(command, "cfg_zsd") == 0) { + //设置归零速度 + int zsd = params["value"].as(); + handle_cfg_zsd_cmd(zsd); + } else if (strcmp(command, "cfg_zsp") == 0) { + //设置归零后的安全位置 + int zsp = params["value"].as(); + handle_cfg_zsp_cmd(zsp); + } else if (strcmp(command, "cfg_dmd") == 0) { + //设置离线运行模式 + int dmd = params["value"].as(); + handle_cfg_dmd_cmd(dmd); + } else if (strcmp(command, "cfg_dar") == 0) { + //设置无握手启动时间 + int dar = params["value"].as(); + handle_cfg_dar_cmd(dar); + } else if (strcmp(command, "cfg_msr") == 0) { + //设置负极限传感器 + int msr = params["value"].as(); + handle_cfg_msr_cmd(msr); + } else if (strcmp(command, "cfg_msv") == 0) { + //设置负极限触发电平 + int msv = params["value"].as(); + handle_cfg_msv_cmd(msv); + } else if (strcmp(command, "cfg_psr") == 0) { + //设置正极限传感器 + int psr = params["value"].as(); + handle_cfg_psr_cmd(psr); + } else if (strcmp(command, "cfg_psv") == 0) { + //设置正极限触发电平 + int psv = params["value"].as(); + handle_cfg_psv_cmd(psv); + } else if (strcmp(command, "cfg_pae") == 0) { + //设置上电使能 + int pae = params["value"].as(); + handle_cfg_pae_cmd(pae); + } else if (strcmp(command, "cfg_emod") == 0) { + //设置编码器模式 + int emod = params["value"].as(); + handle_cfg_emod_cmd(emod); + } else if (strcmp(command, "cfg_elns") == 0) { + //设置编码器线数 + int elns = params["value"].as(); + handle_cfg_elns_cmd(elns); + } else if (strcmp(command, "cfg_estp") == 0) { + //设置电机每圈整步数 + int estp = params["value"].as(); + handle_cfg_estp_cmd(estp); + } else if (strcmp(command, "cfg_erty") == 0) { + //设置堵转后重试次数 + int erty = params["value"].as(); + handle_cfg_erty_cmd(erty); + } else if (strcmp(command, "cfg_edir") == 0) { + //设置编码器方向 + int edir = params["value"].as(); + handle_cfg_edir_cmd(edir); + } else if (strcmp(command, "cfg_ez") == 0) { + //设置编码器灵敏度 + int ez = params["value"].as(); + handle_cfg_ez_cmd(ez); + } else if (strcmp(command, "cfg_ewr") == 0) { + //设置堵转后动作 + int ewr = params["value"].as(); + handle_cfg_ewr_cmd(ewr); + } else if (strcmp(command, "sav") == 0) { + handle_sav_cmd(); + // ***************************S3-S6端口输出控制**********************/ + } else if (strcmp(command, "s3_on") == 0) { + handle_port_output_cmd("s3", "on"); + } else if (strcmp(command, "s3_off") == 0) { + handle_port_output_cmd("s3", "off"); + } else if (strcmp(command, "s4_on") == 0) { + handle_port_output_cmd("s4", "on"); + } else if (strcmp(command, "s4_off") == 0) { + handle_port_output_cmd("s4", "off"); + } else if (strcmp(command, "s5_on") == 0) { + handle_port_output_cmd("s5", "on"); + } else if (strcmp(command, "s5_off") == 0) { + handle_port_output_cmd("s5", "off"); + } else if (strcmp(command, "s6_on") == 0) { + handle_port_output_cmd("s6", "on"); + } else if (strcmp(command, "s6_off") == 0) { + handle_port_output_cmd("s6", "off"); + //************************************************************* */ + } + else { + sendJsonResponse(command, "UNKNOWN_COMMAND"); + } +} + +void processStringCommand(const char* stringData) { + // 处理字符串命令 + String response = "Echo: " + String(stringData); + sendStringResponse(response.c_str()); +} + +void send_vsmd_cmd(const char* cmd, int value) { + int len = strlen(cmd); + for (int i = 0; i < len; i++) { + Serial.print(cmd[i]); + } + // 如果有参数值,则添加参数 + if (value != -1) { + Serial.print(" "); + Serial.print(value); + } + Serial.println(); + + delay(100); +} + +void printSerialRawData(const uint8_t* buffer, int bytesRead) { + // 打印Serial串口收到的原始数据 + DebugSerial.print("Serial received data (bytes): "); + DebugSerial.println(bytesRead); + DebugSerial.print("Raw data: "); + for (int i = 0; i < bytesRead; i++) { + DebugSerial.printf("%02X ", buffer[i]); + } + DebugSerial.println(); +} + +String readAndParseSerialData() { + String info = ""; + + // 检查Serial串口是否有返回数据 + if (Serial.available() > 0) { + uint8_t serialBuffer[512]; + int bytesRead = Serial.readBytes(serialBuffer, sizeof(serialBuffer)); + + if (bytesRead > 0) { + // 使用 parseInfo 解析串口返回的实际数据 + info = parser.parseInfo(serialBuffer, bytesRead); + } else { + info = "No response data"; + } + } else { + info = "No serial data available"; + } + + return info; +} + +//*********************************命令处理*******************************// + +void handle_cfg_bdr_cmd(int baudrate) { + // 验证参数范围 + if (baudrate < 2400 || baudrate > 921600) { + DebugSerial.print("Invalid baudrate value: "); + DebugSerial.println(baudrate); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg bdr"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = baudrate; + responseDoc["Other"] = "Baudrate must be between 2400 and 921600"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_bdr_cmd: "); + DebugSerial.println(baudrate); + + // 构造命令字符串 "cfg bdr=115200\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg bdr=%d\n", baudrate); + send_vsmd_cmd(cmdBuffer); + + String bdrInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg bdr"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = baudrate; + responseDoc["Other"] = bdrInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_mcs_cmd(int value) { + // 双重验证参数范围 + if (value < 0 || value > 8) { + DebugSerial.print("Invalid microstep value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg mcs"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "MCS value must be between 0 and 8"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_mcs_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg mcs=5\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg mcs=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String mcsInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_mcs"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = mcsInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_cmd() { + DebugSerial.println("cfg_cmd"); + send_vsmd_cmd("cfg\n"); + + String configInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = configInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_dev_cmd() { + DebugSerial.println("dev_cmd"); + send_vsmd_cmd("dev\n"); + + String deviceInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "dev"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = deviceInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + + DebugSerial.println(); + DebugSerial.print("Sent JSON Response: "); + DebugSerial.println(jsonResponse); +} + +void handle_sts_cmd() { + DebugSerial.println("sts_cmd"); + send_vsmd_cmd("sts\n"); + + String statusInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "sts"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = statusInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_ena_cmd() { + deviceEnabled = true; + DebugSerial.println("ena_cmd"); + send_vsmd_cmd("ena\n"); + + String enableInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "ena"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = enableInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_off_cmd() { + deviceEnabled = false; + DebugSerial.println("off_cmd"); + send_vsmd_cmd("off\n"); + + String offInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "off"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = offInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_mov_cmd() { + if (!deviceEnabled) { + sendJsonResponse("mov", "DEVICE_DISABLED"); + return; + } + DebugSerial.println("mov_cmd"); + send_vsmd_cmd("mov\n"); + + String movInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "mov"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = movInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_pos_cmd(int value) { + if (!deviceEnabled) { + sendJsonResponse("pos", "DEVICE_DISABLED"); + return; + } + + // 验证位置参数范围:-2147483647~2147483647 + if (value < -2147483647 || value > 2147483647) { + DebugSerial.print("Invalid position value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "pos"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "POS value must be between -2147483647 and 2147483647"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("pos_cmd"); + DebugSerial.println(value); + send_vsmd_cmd("pos", value); + + String posInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "pos"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = posInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_rmv_cmd(int value) { + if (!deviceEnabled) { + sendJsonResponse("rmv", "DEVICE_DISABLED"); + return; + } + + // 验证相对移动参数范围:-2147483647~2147483647 + if (value < -2147483647 || value > 2147483647) { + DebugSerial.print("Invalid relative move value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "rmv"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "RMV value must be between -2147483647 and 2147483647"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.println("rmv_cmd"); + send_vsmd_cmd("rmv", value); + + String rmvInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "rmv"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = rmvInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_pps_cmd(int value) { + if (value == -1) { + // MOVE TO PPS + DebugSerial.println("pps_cmd"); + send_vsmd_cmd("pps"); + + String ppsInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "pps"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = pulsePerSecond; + responseDoc["Other"] = ppsInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + } else { + // 验证PPS参数范围:-2147483647~2147483647 + if (value < -2147483647 || value > 2147483647) { + DebugSerial.print("Invalid PPS value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "pps"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "PPS value must be between -2147483647 and 2147483647"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + // SET PPS + pulsePerSecond = value; + DebugSerial.print("pps set to: "); + DebugSerial.println(value); + send_vsmd_cmd("pps", value); + + String ppsInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "pps"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = ppsInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + } +} + +void handle_stp_cmd(int value) { + // 验证参数范围:0或1 + if (value != 0 && value != 1) { + DebugSerial.print("Invalid stop mode value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "stp"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "Stop mode value must be 0 (decelerate stop) or 1 (immediate stop)"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + if (value == 0) { + // 减速停止(默认行为) + DebugSerial.println("stp_cmd - decelerate stop"); + send_vsmd_cmd("stp\n"); + } else { + // 立刻停止 + DebugSerial.println("stp_cmd - immediate stop"); + send_vsmd_cmd("stp", 1); + } + + String stpInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "stp"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = stpInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_org_cmd() { + if (!deviceEnabled) { + sendJsonResponse("org", "DEVICE_DISABLED"); + return; + } + + DebugSerial.println("org_cmd"); + send_vsmd_cmd("org"); + + String orgInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "org"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = 0; + responseDoc["Other"] = orgInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_zero_start_cmd() { + + DebugSerial.println("Zero started"); + send_vsmd_cmd("zero start"); + + String zeroStartInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "zero_start"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = zeroStartInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_zero_stop_cmd() { + + DebugSerial.println("Zero stopped"); + send_vsmd_cmd("zero stop"); + + String zeroStopInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "zero_stop"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = zeroStopInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_spd_cmd(int value) { + // 验证运行速度范围:-192000~192000Hz + if (value < -192000 || value > 192000) { + DebugSerial.print("Invalid speed value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_spd"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "Speed value must be between -192000 and 192000 Hz"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_spd_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg spd=1200\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg spd=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String spdInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_spd"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = spdInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_accSpeed_cmd(int value) { + // 验证加速度范围:0~192000000 + if (value < 0 || value > 192000000) { + DebugSerial.print("Invalid acceleration value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_accSpeed"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "Acceleration value must be between 0 and 192000000"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_accSpeed_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg acc=1200\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg acc=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String accSpeedInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_accSpeed"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = accSpeedInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_decSpeed_cmd(int value) { + // 验证减速度范围:0~192000000 + if (value < 0 || value > 192000000) { + DebugSerial.print("Invalid deceleration value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_decSpeed"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "Deceleration value must be between 0 and 192000000"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_decSpeed_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg dec=1200\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg dec=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String decSpeedInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_decSpeed"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = decSpeedInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_cra_cmd(float value) { + // 双重验证参数范围 + if (value < 0.0 || value > 2.5) { + DebugSerial.print("Invalid CRA current value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_cra"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "CRA current must be between 0.0 and 2.5"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_cra_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg cra=0.8\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg cra=%.1f\n", value); + send_vsmd_cmd(cmdBuffer); + + String craInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_cra"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = craInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_crn_cmd(float value) { + // 双重验证参数范围 + if (value < 0.0 || value > 2.5) { + DebugSerial.print("Invalid CRN current value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_crn"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "CRN current must be between 0.0 and 2.5"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_crn_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg crn=0.4\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg crn=%.1f\n", value); + send_vsmd_cmd(cmdBuffer); + + String crnInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_crn"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = crnInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_crh_cmd(float value) { + // 双重验证参数范围 + if (value < 0.0 || value > 2.5) { + DebugSerial.print("Invalid CRH current value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_crh"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "CRH current must be between 0.0 and 2.5"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_crh_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg crh=0.0\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg crh=%.1f\n", value); + send_vsmd_cmd(cmdBuffer); + + String crhInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_crh"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = crhInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_sensor_cmd(const char* sensorType, int value) { + // 根据传感器类型确定参数范围 + int minValue = 0; + int maxValue = 9; + + // S3-S6工作模式参数范围为0-1 + if (strcmp(sensorType, "s3") == 0 || strcmp(sensorType, "s4") == 0 || + strcmp(sensorType, "s5") == 0 || strcmp(sensorType, "s6") == 0) { + maxValue = 1; + } + + // 验证参数范围 + if (value < minValue || value > maxValue) { + DebugSerial.print("Invalid sensor value for "); + DebugSerial.print(sensorType); + DebugSerial.print(": "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = String("cfg_") + sensorType; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + + String errorMsg = String(sensorType) + " value must be between " + + String(minValue) + " and " + String(maxValue); + responseDoc["Other"] = errorMsg; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_"); + DebugSerial.print(sensorType); + DebugSerial.print("_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg %s=%d\n", sensorType, value); + send_vsmd_cmd(cmdBuffer); + + String sensorInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = String("cfg_") + sensorType; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = sensorInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +/* +void handle_cfg_s1f_cmd(int value) { + // 验证参数范围 + if (value < 0 || value > 9) { + DebugSerial.print("Invalid S1F value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s1f"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "S1F value must be between 0 and 9"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_s1f_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg s1f=3\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg s1f=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String s1fInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s1f"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = s1fInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_s1r_cmd(int value) { + // 验证参数范围 + if (value < 0 || value > 9) { + DebugSerial.print("Invalid S1R value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s1r"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "S1R value must be between 0 and 9"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_s1r_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg s1r=2\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg s1r=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String s1rInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s1r"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = s1rInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_s2f_cmd(int value) { + // 验证参数范围 + if (value < 0 || value > 9) { + DebugSerial.print("Invalid S2F value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s2f"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "S2F value must be between 0 and 9"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_s2f_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg s2f=3\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg s2f=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String s2fInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s2f"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = s2fInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_s2r_cmd(int value) { + // 验证参数范围 + if (value < 0 || value > 9) { + DebugSerial.print("Invalid S2R value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s2r"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "S2R value must be between 0 and 9"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_s2r_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg s2r=2\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg s2r=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String s2rInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_s2r"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = s2rInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} +*/ + +void handle_cfg_zmd_cmd(int value) { + // 验证归零功能参数范围:0~4 + if (value < 0 || value > 4) { + DebugSerial.print("Invalid zero mode value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_zmd"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "ZMD value must be between 0 and 4"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_zmd_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg zmd=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg zmd=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String zmdInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_zmd"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = zmdInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_snr_cmd(int value) { + // 验证归零用传感器参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid sensor value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_snr"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "SNR value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_snr_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg snr=0\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg snr=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String snrInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_snr"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = snrInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_osv_cmd(int value) { + // 验证归零传感器OPEN时电平参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid open sensor value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_osv"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "OSV value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_osv_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg osv=0\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg osv=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String osvInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_osv"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = osvInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_zsd_cmd(int value) { + // 验证归零速度参数范围:-192000~192000 + if (value < -192000 || value > 192000) { + DebugSerial.print("Invalid zero speed value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_zsd"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "ZSD value must be between -192000 and 192000"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_zsd_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg zsd=-1200\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg zsd=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String zsdInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_zsd"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = zsdInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_zsp_cmd(int value) { + // 验证归零后安全位置参数范围:-2147483647~2147483647 + if (value < -2147483647 || value > 2147483647) { + DebugSerial.print("Invalid zero safe position value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_zsp"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "ZSP value must be between -2147483647 and 2147483647"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_zsp_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg zsp=2400\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg zsp=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String zspInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_zsp"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = zspInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_dmd_cmd(int value) { + // 验证离线运行模式参数范围:0~2 + if (value < 0 || value > 2) { + DebugSerial.print("Invalid demo mode value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_dmd"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "DMD value must be between 0 and 2"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_dmd_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg dmd=2\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg dmd=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String dmdInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_dmd"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = dmdInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_dar_cmd(int value) { + // 验证无握手启动时间参数范围:0~60 + if (value < 0 || value > 60) { + DebugSerial.print("Invalid demo auto run value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_dar"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "DAR value must be between 0 and 60"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_dar_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg dar=5\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg dar=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String darInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_dar"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = darInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_msr_cmd(int value) { + // 验证负极限传感器参数范围:0~6 + if (value < 0 || value > 6) { + DebugSerial.print("Invalid minus sensor value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_msr"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "MSR value must be between 0 and 6"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_msr_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg msr=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg msr=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String msrInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_msr"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = msrInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_msv_cmd(int value) { + // 验证负极限触发电平参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid minus sensor value level: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_msv"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "MSV value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_msv_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg msv=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg msv=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String msvInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_msv"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = msvInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_psr_cmd(int value) { + // 验证正极限传感器参数范围:0~6 + if (value < 0 || value > 6) { + DebugSerial.print("Invalid plus sensor value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_psr"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "PSR value must be between 0 and 6"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_psr_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg psr=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg psr=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String psrInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_psr"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = psrInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_psv_cmd(int value) { + // 验证正极限触发电平参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid plus sensor value level: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_psv"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "PSV value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_psv_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg psv=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg psv=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String psvInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_psv"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = psvInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_pae_cmd(int value) { + // 验证上电使能参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid power auto enable value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_pae"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "PAE value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_pae_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg pae=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg pae=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String paeInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_pae"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = paeInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_emod_cmd(int value) { + // 验证编码器模式参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid encoder mode value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_emod"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "EMOD value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_emod_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg emod=1\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg emod=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String emodInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_emod"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = emodInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_elns_cmd(int value) { + // 验证编码器线数参数范围:10~10000 + if (value < 10 || value > 10000) { + DebugSerial.print("Invalid encoder lines value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_elns"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "ELNS value must be between 10 and 10000"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_elns_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg elns=1000\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg elns=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String elnsInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_elns"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = elnsInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_estp_cmd(int value) { + // 验证电机每圈整步数参数范围:10~10000 + if (value < 10 || value > 10000) { + DebugSerial.print("Invalid motor steps per revolution value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_estp"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "ESTP value must be between 10 and 10000"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_estp_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg estp=200\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg estp=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String estpInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_estp"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = estpInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_erty_cmd(int value) { + // 验证堵转后重试次数参数范围:0~100 + if (value < 0 || value > 100) { + DebugSerial.print("Invalid encoder retry times value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_erty"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "ERTY value must be between 0 and 100"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_erty_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg erty=0\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg erty=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String ertyInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_erty"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = ertyInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_edir_cmd(int value) { + // 验证编码器方向参数范围:0~1 + if (value < 0 || value > 1) { + DebugSerial.print("Invalid encoder direction value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_edir"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "EDIR value must be 0 or 1"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_edir_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg edir=0\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg edir=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String edirInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_edir"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = edirInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_ez_cmd(int value) { + // 验证编码器灵敏度参数范围:0~100 + if (value < 0 || value > 100) { + DebugSerial.print("Invalid encoder sensitivity value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_ez"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "EZ value must be between 0 and 100"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_ez_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg ez=4\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg ez=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String ezInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_ez"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = ezInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_cfg_ewr_cmd(int value) { + // 验证堵转后动作参数范围:0~2 + if (value < 0 || value > 2) { + DebugSerial.print("Invalid encoder warning action value: "); + DebugSerial.println(value); + + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_ewr"; + responseDoc["ST"] = "ERROR"; + responseDoc["value"] = value; + responseDoc["Other"] = "EWR value must be between 0 and 2"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print("cfg_ewr_cmd: "); + DebugSerial.println(value); + + // 构造命令字符串 "cfg ewr=0\n" + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "cfg ewr=%d\n", value); + send_vsmd_cmd(cmdBuffer); + + String ewrInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "cfg_ewr"; + responseDoc["ST"] = "OK"; + responseDoc["value"] = value; + responseDoc["Other"] = ewrInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + + +void handle_sav_cmd() { + DebugSerial.println("sav_cmd"); + send_vsmd_cmd("sav\n"); + + String savInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = "sav"; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = savInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} + +void handle_port_output_cmd(const char* portType, const char* action) { + // 验证端口类型 + if (strcmp(portType, "s3") != 0 && strcmp(portType, "s4") != 0 && + strcmp(portType, "s5") != 0 && strcmp(portType, "s6") != 0) { + DebugSerial.print("Invalid port type: "); + DebugSerial.println(portType); + + StaticJsonDocument responseDoc; + responseDoc["command"] = String(portType) + "_" + action; + responseDoc["ST"] = "ERROR"; + responseDoc["Other"] = "Invalid port type, must be s3, s4, s5, or s6"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + // 验证动作类型 + if (strcmp(action, "on") != 0 && strcmp(action, "off") != 0) { + DebugSerial.print("Invalid action: "); + DebugSerial.println(action); + + StaticJsonDocument responseDoc; + responseDoc["command"] = String(portType) + "_" + action; + responseDoc["ST"] = "ERROR"; + responseDoc["Other"] = "Invalid action, must be 'on' or 'off'"; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + return; + } + + DebugSerial.print(portType); + DebugSerial.print("_"); + DebugSerial.print(action); + DebugSerial.println("_cmd"); + + // 构造命令字符串 + char cmdBuffer[32]; + snprintf(cmdBuffer, sizeof(cmdBuffer), "%s %s\n", portType, action); + send_vsmd_cmd(cmdBuffer); + + String portInfo = readAndParseSerialData(); + + // 创建带有解析数据的JSON响应 + StaticJsonDocument responseDoc; + responseDoc["command"] = String(portType) + "_" + action; + responseDoc["ST"] = "OK"; + responseDoc["Other"] = portInfo; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 打包并发送JSON响应 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); +} \ No newline at end of file diff --git a/src/VSMD_command_handler.h b/src/VSMD_command_handler.h new file mode 100644 index 0000000..0c61da7 --- /dev/null +++ b/src/VSMD_command_handler.h @@ -0,0 +1,138 @@ +#ifndef VSMD_COMMAND_HANDLER_H +#define VSMD_COMMAND_HANDLER_H + +#include +#include "vsmd_parser.h" + +// 外部变量声明 +extern bool deviceEnabled; +extern int32_t pulsePerSecond; +extern uint8_t sendBuffer[]; +extern int32_t packedLength; +extern VSMDParser parser; + +// 串口通信函数 +void send_vsmd_cmd(const char* cmd, int value = -1); + // send_vsmd_cmd("dev"); // 不带参数,使用默认值-1 + // send_vsmd_cmd("pos", value); // 带参数 + +// 打印Serial串口收到的原始数据 +void printSerialRawData(const uint8_t* buffer, int bytesRead); +//VSMD串口数据解析 +String readAndParseSerialData(); + +// JSON响应函数 +void sendJsonResponse(const char* command, const char* status, int value = -1); +// 打包并发送字符串响应 +void sendStringResponse(const char* response); + +// JSON命令处理函数 +void processJsonCommand(const char* jsonString); +// 处理字符串命令 +void processStringCommand(const char* stringData); + +//获取参数值 +void handle_cfg_cmd(); +//握手,获取型号和版本号 +void handle_dev_cmd(); +//获取运行状态(速度、位置、状态) +void handle_sts_cmd(); +//电机使能 +void handle_ena_cmd(); +//电机脱机 +void handle_off_cmd(); +//以指定速度连续运转 +void handle_mov_cmd(); +//移动到指定位置 +void handle_pos_cmd(int value); +//相对移动 +void handle_rmv_cmd(int value); +//移动到预设位置,有value时,预设目标位置 +void handle_pps_cmd(int value = -1); +//指定当前位置位原点 +void handle_org_cmd(); +//停止 +void handle_stp_cmd(int value = 0); +//执行归零 +void handle_zero_start_cmd(); +//停止归零 +void handle_zero_stop_cmd(); +//设置波特率 +void handle_cfg_bdr_cmd(int baudrate); +//设置微步细分 +void handle_cfg_mcs_cmd(int value); +//设置速度 +void handle_cfg_spd_cmd(int value); +//设置加速速度 +void handle_cfg_accSpeed_cmd(int value); +//设置减速度 +void handle_cfg_decSpeed_cmd(int value); +//设置加速时电流 +void handle_cfg_cra_cmd(float value); +//设置匀速时电流 +void handle_cfg_crn_cmd(float value); +//设置HOLD电流 +void handle_cfg_crh_cmd(float value); + +// ***************************SENSOR事件、工作模式处理**********************/ +void handle_cfg_sensor_cmd(const char* sensorType, int value); +// ***************************SENSOR端口输出控制处理**********************/ +void handle_port_output_cmd(const char* portType, const char* action); + +// //设置S1下降沿触发事件 +// void handle_cfg_s1f_cmd(int value); +// //设置S1上升沿触发事件 +// void handle_cfg_s1r_cmd(int value); +// //设置S2下降沿触发事件 +// void handle_cfg_s2f_cmd(int value); +// //设置S2上升沿触发事件 +// void handle_cfg_s2r_cmd(int value); + +//设置归零功能 +void handle_cfg_zmd_cmd(int value); +//设置归零用传感器 +void handle_cfg_snr_cmd(int value); +//设置归零传感器OPEN时电平 +void handle_cfg_osv_cmd(int value); +//设置归零速度 +void handle_cfg_zsd_cmd(int value); +//设置归零后的安全位置 +void handle_cfg_zsp_cmd(int value); +//设置离线运行模式 +void handle_cfg_dmd_cmd(int value); +//设置无握手启动时间 +void handle_cfg_dar_cmd(int value); +//设置负极限传感器 +void handle_cfg_msr_cmd(int value); +//设置负极限触发电平 +void handle_cfg_msv_cmd(int value); +//设置正极限传感器 +void handle_cfg_psr_cmd(int value); +//设置正极限触发电平 +void handle_cfg_psv_cmd(int value); +//设置上电使能 +void handle_cfg_pae_cmd(int value); + +//***************************编码器相关配置*********************************/ + +// 编码器模式 +void handle_cfg_emod_cmd(int value); +// 编码器线数 +void handle_cfg_elns_cmd(int value); +// 电机每圈整步数 +void handle_cfg_estp_cmd(int value); +// 堵转后重试次数 +void handle_cfg_erty_cmd(int value); +// 编码器方向 +void handle_cfg_edir_cmd(int value); +// 编码器灵敏度 +void handle_cfg_ez_cmd(int value); +// 堵转后动作 +void handle_cfg_ewr_cmd(int value); +//*********************************************************************/ + + +//保存参数到FLASH +void handle_sav_cmd(); + +#endif \ No newline at end of file diff --git a/src/bluetooth_handler.cpp b/src/bluetooth_handler.cpp new file mode 100644 index 0000000..b9e45a4 --- /dev/null +++ b/src/bluetooth_handler.cpp @@ -0,0 +1,310 @@ +#include "bluetooth_handler.h" +#include "VSMD_command_handler.h" +#include "IRIS_Method.h" + +// 添加缺失的常量定义 +#define BUFFER_SIZE 1024 +#define JSON_BUFFER_SIZE 512 + +// 蓝牙服务和特征值UUID +#define BT_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" +#define BT_CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" +#define BT_CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" + +// 全局变量 +BLEServer* btServer = nullptr; +BLECharacteristic* btCharacteristicTX = nullptr; +BLECharacteristic* btCharacteristicRX = nullptr; +bool bluetoothConnected = false; +bool bluetoothDataReceived = false; + +// 蓝牙接收缓冲区和数据包重组 +#define BT_BUFFER_SIZE 1024 +#define BT_PACKET_TIMEOUT 1000 // 1秒超时 +uint8_t btReceiveBuffer[BT_BUFFER_SIZE]; +size_t btDataLength = 0; +uint8_t btPacketBuffer[BT_BUFFER_SIZE]; // 数据包重组缓冲区 +size_t btPacketLength = 0; +unsigned long lastPacketTime = 0; +bool isReceivingPacket = false; + +// 外部变量声明 +extern uint8_t receiveBuffer[BUFFER_SIZE]; +extern uint8_t sendBuffer[BUFFER_SIZE]; +extern uint8_t responseBuffer[BUFFER_SIZE]; +extern uint8_t command; +extern int32_t bytesProcessed; +extern int32_t unpackResult; +extern int32_t packedLength; +extern HardwareSerial DebugSerial; + +// 数据包是否完整(检查IRIS协议的头部和长度,支持CRC校验) +bool isPacketComplete(const uint8_t* buffer, size_t length) { + if (length < 7) return false; // 最小包长度:头部(2) + 命令(1) + 长度(2) + CRC(2) + + // 检查包头 0x55 0xAA + if (buffer[0] != 0x55 || buffer[1] != 0xAA) return false; + + // 获取数据长度字段(第3和第4字节) + uint16_t dataLen = (buffer[3] << 8) | buffer[4]; + + // 计算期望的总包长度:头部(2字节) + 命令(1字节) + 长度(2字节) + 数据长度 + CRC(2字节) + size_t expectedLength = 5 + dataLen + 2; + + if (length < expectedLength) { + return false; // 数据包还不完整 + } + + if (length >= expectedLength && + buffer[expectedLength-2] == 0xEE && + buffer[expectedLength-1] == 0xEE) { + return true; // 无校验包,直接返回完整 + } + + // 对于有校验的包,验证CRC + if (length >= expectedLength) { + // 计算数据部分的CRC(从第5字节开始,长度为dataLen) + uint16_t calculatedCRC = IRIS_calcCRC(buffer + 5, dataLen); + + uint16_t packetCRC = (buffer[expectedLength-2] << 8) | buffer[expectedLength-1]; + + if (calculatedCRC == packetCRC) { + return true; + } else { + DebugSerial.print("CRC err: calculated=0x"); + DebugSerial.print(calculatedCRC, HEX); + DebugSerial.print(", packet=0x"); + DebugSerial.println(packetCRC, HEX); + return false; // CRC校验失败 + } + } + + return false; // 默认返回不完整 +} + +// 重置数据包接收状态 +void resetPacketReceiving() { + isReceivingPacket = false; + btPacketLength = 0; + lastPacketTime = 0; +} + +// 处理接收到的数据片段 +void processBluetoothFragment(const uint8_t* data, size_t length) { + unsigned long currentTime = millis(); + + // 检查超时 + if (isReceivingPacket && (currentTime - lastPacketTime > BT_PACKET_TIMEOUT)) { + DebugSerial.println("Bluetooth packet timeout, resetting..."); + resetPacketReceiving(); + } + + // 如果缓冲区空间不足,重置 + if (btPacketLength + length > BT_BUFFER_SIZE) { + DebugSerial.println("Bluetooth packet buffer overflow, resetting..."); + resetPacketReceiving(); + } + + // 将新数据添加到包缓冲区 + memcpy(btPacketBuffer + btPacketLength, data, length); + btPacketLength += length; + lastPacketTime = currentTime; + isReceivingPacket = true; + + DebugSerial.print("Ble rec: "); + DebugSerial.print(length); + DebugSerial.print(" bytes, total: "); + DebugSerial.println(btPacketLength); + + // 检查数据包是否完整 + if (isPacketComplete(btPacketBuffer, btPacketLength)) { + //DebugSerial.println("Complete packet received via Bluetooth"); + + // 复制完整数据包到接收缓冲区 + memcpy(btReceiveBuffer, btPacketBuffer, btPacketLength); + btDataLength = btPacketLength; + bluetoothDataReceived = true; + + // 重置包接收状态 + resetPacketReceiving(); + } +} + +// 服务器回调类 +class BluetoothServerCallbacks : public BLEServerCallbacks { + void onConnect(BLEServer* pServer) { + bluetoothConnected = true; + DebugSerial.println("Bluetooth Client Connected"); + resetPacketReceiving(); // 连接时重置状态 + } + + void onDisconnect(BLEServer* pServer) { + bluetoothConnected = false; + bluetoothDataReceived = false; + DebugSerial.println("Bluetooth Client Disconnected"); + resetPacketReceiving(); // 断开时重置状态 + + // 重新开始广播 + delay(500); + pServer->startAdvertising(); + DebugSerial.println("Bluetooth advertising restarted"); + } +}; + +// 特征值回调类 +class BluetoothCharacteristicCallbacks : public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic* pCharacteristic) { + std::string rxValue = pCharacteristic->getValue(); + + if (rxValue.length() > 0) { + // 处理接收到的数据片段 + processBluetoothFragment((const uint8_t*)rxValue.c_str(), rxValue.length()); + } + } +}; + +void initBluetooth() { + //DebugSerial.println("Initializing Bluetooth..."); + + // 初始化BLE设备 + BLEDevice::init("VSMD_Controller"); + + // 创建BLE服务器 + btServer = BLEDevice::createServer(); + btServer->setCallbacks(new BluetoothServerCallbacks()); + + // 创建BLE服务 + BLEService* btService = btServer->createService(BT_SERVICE_UUID); + + // 创建TX特征值(用于发送数据到客户端) + btCharacteristicTX = btService->createCharacteristic( + BT_CHARACTERISTIC_UUID_TX, + BLECharacteristic::PROPERTY_NOTIFY + ); + btCharacteristicTX->addDescriptor(new BLE2902()); + + // 创建RX特征值(用于接收来自客户端的数据) + btCharacteristicRX = btService->createCharacteristic( + BT_CHARACTERISTIC_UUID_RX, + BLECharacteristic::PROPERTY_WRITE + ); + btCharacteristicRX->setCallbacks(new BluetoothCharacteristicCallbacks()); + + // 启动服务 + btService->start(); + + // 开始广播 + BLEAdvertising* btAdvertising = BLEDevice::getAdvertising(); + btAdvertising->addServiceUUID(BT_SERVICE_UUID); + btAdvertising->setScanResponse(false); + btAdvertising->setMinPreferred(0x0); + BLEDevice::startAdvertising(); + + DebugSerial.println("VSMD_Ble_Controller initialized"); + + // 初始化包接收状态 + resetPacketReceiving(); +} + +void handleBluetoothData() { + if (!bluetoothDataReceived) { + return; + } + + // 复制数据到主接收缓冲区 + memcpy(receiveBuffer, btReceiveBuffer, btDataLength); + + DebugSerial.print("Total: "); + DebugSerial.print(btDataLength); + DebugSerial.println(" bytes"); + + // 处理数据(使用与DebugSerial相同的处理逻辑) + bytesProcessed = IRIS_Cut_Befor_Header(receiveBuffer, btDataLength); + + if (bytesProcessed > 0) { + unpackResult = IRIS_STM32_Protocol_Unpack(receiveBuffer, bytesProcessed, &command, responseBuffer); + + if (unpackResult >= 0) { + // 根据命令类型处理数据 + switch (command) { + case 0x00: { // JSON数据 + char jsonString[JSON_BUFFER_SIZE]; + memcpy(jsonString, responseBuffer, unpackResult); + jsonString[unpackResult] = '\0'; + + DebugSerial.print("Bluetooth JSON: "); + DebugSerial.println(jsonString); + + processJsonCommand(jsonString); + //BLE Notify + sendBluetoothData(sendBuffer, packedLength); + break; + } + + case 0x01: { // 字符串数据 + char stringData[BUFFER_SIZE]; + memcpy(stringData, responseBuffer, unpackResult); + stringData[unpackResult] = '\0'; + + DebugSerial.print("Bluetooth String: "); + DebugSerial.println(stringData); + + processStringCommand(stringData); + break; + } + + default: { // 自定义二进制数据 + DebugSerial.print("Bluetooth Binary Data, Command: 0x"); + DebugSerial.print(command, HEX); + DebugSerial.print(", Length: "); + DebugSerial.println(unpackResult); + + break; + } + } + } else { + DebugSerial.print("Bluetooth unpack failed, error code: "); + DebugSerial.println(unpackResult); + } + } + + // 重置标志 + bluetoothDataReceived = false; +} + +void sendBluetoothData(const uint8_t* data, size_t length) { + if (bluetoothConnected && btCharacteristicTX != nullptr) { + // BLE特征值有最大传输长度限制,通常为20字节 + const size_t maxChunkSize = 20; + size_t offset = 0; + + // DebugSerial.println("Sending "); + // DebugSerial.print(length); + // DebugSerial.println(" bytes via Bluetooth in chunks:"); + + while (offset < length) { + size_t chunkSize = min(maxChunkSize, length - offset); + btCharacteristicTX->setValue((uint8_t*)(data + offset), chunkSize); + btCharacteristicTX->notify(); + + // DebugSerial.print(" Chunk "); + // DebugSerial.print(offset/maxChunkSize + 1); + // DebugSerial.print(": "); + // DebugSerial.print(chunkSize); + // DebugSerial.println(" bytes"); + + offset += chunkSize; + + // 短暂延迟以确保数据传输稳定 + if (offset < length) { + delay(20); // 增加延迟确保接收端有时间处理 + } + } + + //DebugSerial.println("Bluetooth transmission complete"); + } +} + +bool isBluetoothConnected() { + return bluetoothConnected; +} \ No newline at end of file diff --git a/src/bluetooth_handler.h b/src/bluetooth_handler.h new file mode 100644 index 0000000..cd59eea --- /dev/null +++ b/src/bluetooth_handler.h @@ -0,0 +1,27 @@ +#ifndef BLUETOOTH_HANDLER_H +#define BLUETOOTH_HANDLER_H + +#include +#include +#include +#include +#include +#include + +// 蓝牙状态 +extern bool bluetoothConnected; +extern bool bluetoothDataReceived; + +// 蓝牙初始化 +void initBluetooth(); + +// 蓝牙数据处理 +void handleBluetoothData(); + +// 蓝牙发送数据 +void sendBluetoothData(const uint8_t* data, size_t length); + +// 蓝牙连接状态 +bool isBluetoothConnected(); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index aecc9ea..6bb90c6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,19 @@ #include #include "IRIS_Method.h" #include "vsmd_parser.h" +#include "VSMD_command_handler.h" +#include "bluetooth_handler.h" // 添加蓝牙模块 VSMDParser parser; #define DEBUG_TX_PIN 17 // TX #define DEBUG_RX_PIN 16 // RX - - -HardwareSerial DebugSerial(1); // 使用UART1 - #define BUFFER_SIZE 1024 #define JSON_BUFFER_SIZE 512 -// 全局变量声明 +HardwareSerial DebugSerial(1); // 使用UART1 + +// 全局变量 uint8_t receiveBuffer[BUFFER_SIZE]; // 接收缓冲区 uint8_t sendBuffer[BUFFER_SIZE]; // 发送缓冲区 uint8_t responseBuffer[BUFFER_SIZE]; // 响应缓冲区 @@ -24,86 +24,54 @@ int32_t packedLength; // 打包后的数据长度 // 设备状态变量 bool deviceEnabled = false; -int32_t currentPosition = 0; -int32_t targetPosition = 0; -int32_t pulsePerSecond = 1200; -bool isMoving = false; -bool isBackZero = false; +int32_t pulsePerSecond; -// 设备配置参数 -struct DeviceConfig { - int zmd = 1; // cfg zmd - int snr = 0; // cfg snr - int osv = 0; // cfg osv - int zsd = 1200; // cfg zsd - int zsp = 2400; // cfg zsp - int dmd = 2; // cfg dmd - int dar = 5; // cfg dar - int msr = 1; // cfg msr - int msv = 1; // cfg msv - int psr = 1; // cfg psr - int psv = 1; // cfg psv - int bdr = 115200; // cfg bdr - int cid = 1; // cfg cid - int mcs = 5; // cfg mcs - int spd = 1200; // cfg spd - int acc = 12000; // cfg acc - int dec = 12000; // cfg dec - float cra = 0.8; // cfg cra - float crn = 0.4; // cfg crn - float crh = 0.0; // cfg crh -} deviceConfig; - - -void processJsonCommand(const char* jsonString); -void processStringCommand(const char* stringData); -void sendJsonResponse(const char* cmd, const char* status, int value = -1); -void sendStringResponse(const char* response); -void handle_cfg_cmd(); -void handle_ena_cmd(); -void handle_off_cmd(); -void handle_mov_cmd(); -void handle_pos_cmd(int value); -void handle_rmv_cmd(int value); -void handle_pps_cmd(int value = -1); -void handle_org_cmd(); -void handle_stp_cmd(); -void handle_zero_cmd(const char* value); -void handle_dev_cmd(); void printDeviceStatus(); +void handleSerialData(); + +void printDeviceStatus() { + DebugSerial.println("\n=== Device Status ==="); + DebugSerial.print("Enabled: "); + DebugSerial.println(deviceEnabled ? "Yes" : "No"); + DebugSerial.print("Bluetooth: "); + DebugSerial.println(isBluetoothConnected() ? "Connected" : "Disconnected"); + DebugSerial.println("==================\n"); +} void setup() { // 调试串口 DebugSerial.begin(115200, SERIAL_8N1, DEBUG_RX_PIN, DEBUG_TX_PIN); + DebugSerial.setTimeout(1); - // 主串口(用于数据通信) + // 电机数据通信串口 Serial.begin(115200); + Serial.setTimeout(100); DebugSerial.println("VSMD INIT"); - DebugSerial.println("Commands:dev, cfg, ena, off, mov, pos, rmv, pps, org, stp, zero"); + DebugSerial.println("CMD: dev, sts, cfg, ena, off, mov, pos, rmv, pps, org, stp, zero, sav"); + DebugSerial.println("cfg CMD: bdr, mcs, spd, acc, dec, cra, crn, crh, s1f, s1r, s2f, s2r, zmd, snr, osv, zsd, zsp, dmd, dar, msr, msv, psr, psv"); + + // 初始化蓝牙 + initBluetooth(); + printDeviceStatus(); } void loop() { + // 处理串口数据 + handleSerialData(); + + // 处理蓝牙数据 + handleBluetoothData(); + + // 短暂延迟 + delay(10); +} + +void handleSerialData() { if (DebugSerial.available() > 0) { int bytesRead = DebugSerial.readBytes(receiveBuffer, sizeof(receiveBuffer)); - // // 添加调试信息 - // DebugSerial.print("Bytes read: "); - // DebugSerial.println(bytesRead); - // DebugSerial.print("Raw data: "); - // for(int i = 0; i < bytesRead; i++) { - // DebugSerial.print(receiveBuffer[i], HEX); - // DebugSerial.print(" "); - // } - // DebugSerial.println(); - - // bytesProcessed = IRIS_Cut_Befor_Header(receiveBuffer, bytesRead); - - // // 添加更多调试信息 - // DebugSerial.print("Bytes after header cut: "); - // DebugSerial.println(bytesProcessed); - bytesProcessed = IRIS_Cut_Befor_Header(receiveBuffer, bytesRead); if (bytesProcessed > 0) { @@ -115,12 +83,11 @@ void loop() { case 0x00: { // JSON数据 char jsonString[JSON_BUFFER_SIZE]; memcpy(jsonString, responseBuffer, unpackResult); - jsonString[unpackResult] = '\0'; // 确保字符串以null结尾 - - DebugSerial.print("Received JSON: "); - DebugSerial.println(jsonString); + jsonString[unpackResult] = '\0'; processJsonCommand(jsonString); + //BLE Notify + sendBluetoothData(sendBuffer, packedLength); break; } @@ -136,7 +103,7 @@ void loop() { break; } - default: { // 自定义二进制数据 (0x02-0xff) + default: { // 自定义二进制数据 DebugSerial.print("Received Binary Data, Command: 0x"); DebugSerial.print(command, HEX); DebugSerial.print(", Length: "); @@ -154,268 +121,4 @@ void loop() { } } } -} - -void processJsonCommand(const char* jsonString) { - StaticJsonDocument doc; - DeserializationError error = deserializeJson(doc, jsonString); - - if (error) { - DebugSerial.print("JSON parsing failed: "); - DebugSerial.println(error.c_str()); - sendJsonResponse("error", "JSON_PARSE_ERROR"); - return; - } - - // 使用 command 字段替代 cmd - const char* command = doc["command"]; - if (command == nullptr) { - sendJsonResponse("error", "NO_COMMAND_FIELD"); - return; - } - - // 获取 PA 参数对象(可选) - JsonObject params = doc["PA"]; - - // 根据command执行相应操作 - if (strcmp(command, "cfg") == 0) { - handle_cfg_cmd(); - } else if (strcmp(command, "ena") == 0) { - handle_ena_cmd(); - } else if (strcmp(command, "off") == 0) { - handle_off_cmd(); - } else if (strcmp(command, "mov") == 0) { - handle_mov_cmd(); - } else if (strcmp(command, "pos") == 0) { - // 修正:正确的类型转换 - int value = params.containsKey("value") ? params["value"].as() : 0; - handle_pos_cmd(value); - } else if (strcmp(command, "rmv") == 0) { - int value = params.containsKey("value") ? params["value"].as() : 0; - handle_rmv_cmd(value); - } else if (strcmp(command, "pps") == 0) { - if (params.containsKey("value")) { - int value = params["value"].as(); - handle_pps_cmd(value); - } else { - handle_pps_cmd(); - } - } else if (strcmp(command, "org") == 0) { - handle_org_cmd(); - } else if (strcmp(command, "stp") == 0) { - handle_stp_cmd(); - } else if (strcmp(command, "zero") == 0) { - // 修正:正确的字符串类型转换 - const char* value = params.containsKey("value") ? params["value"].as() : ""; - handle_zero_cmd(value); - } else if (strcmp(command, "dev") == 0) { - handle_dev_cmd(); - } else { - sendJsonResponse(command, "UNKNOWN_COMMAND"); - } -} - -void processStringCommand(const char* stringData) { - // 处理字符串命令 - String response = "Echo: " + String(stringData); - sendStringResponse(response.c_str()); -} - -void sendJsonResponse(const char* command, const char* status, int value) { - StaticJsonDocument responseDoc; - - // 修改:使用新的JSON格式 - responseDoc["command"] = command; // 使用 command 替代 cmd - responseDoc["ST"] = status; // 使用 ST 替代 status - - // 如果有参数值,添加到 PA 对象中 - if (value != -1) { - JsonObject params = responseDoc.createNestedObject("PA"); - params["value"] = value; - } - - // 可选:添加其他信息 - responseDoc["Other"] = ""; - - char jsonResponse[JSON_BUFFER_SIZE]; - serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); - - // 打包并发送JSON响应 - DebugSerial.print("JSON Response: "); - packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); - DebugSerial.write(sendBuffer, packedLength); - - // DebugSerial.println(); - - // DebugSerial.print("Sent JSON Response: "); - // DebugSerial.println(jsonResponse); -} - -void sendStringResponse(const char* response) { - // 打包并发送字符串响应 - packedLength = IRIS_Protocol_Pack(0x01, strlen(response), (uint8_t*)response, sendBuffer); - DebugSerial.write(sendBuffer, packedLength); - - DebugSerial.print("Sent String Response: "); - DebugSerial.println(response); -} - -void handle_cfg_cmd() { - DebugSerial.println("cfg requested"); - Serial.println("cfg\n"); - // 返回配置相关信息 - sendJsonResponse("cfg", "OK"); -} - -void handle_dev_cmd() { - DebugSerial.println("dev_requested"); - Serial.println("dev\n"); - - // vsmd test data - uint8_t testData[] = { - 0xFF, 0x00, 0x01, 0x56, 0x53, 0x4D, 0x44, 0x31, 0x34, 0x32, 0x2D, 0x30, 0x32, 0x35, 0x54, 0x2D,0x31, 0x2E, 0x31, 0x2E, 0x30, 0x32, 0x30, 0x2E, 0x32, 0x31, 0x31, 0x31, 0x31, 0x35, 0x00, 0x42, 0xFE - }; - - // parseInfo获取解析结果 - String deviceInfo = parser.parseInfo(testData, sizeof(testData)); - - // 创建带有解析数据的JSON响应 - StaticJsonDocument responseDoc; - responseDoc["command"] = "dev"; - responseDoc["ST"] = "OK"; - responseDoc["Other"] = deviceInfo; - - char jsonResponse[JSON_BUFFER_SIZE]; - serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); - - // 打包并发送JSON响应 - packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); - DebugSerial.write(sendBuffer, packedLength); - - DebugSerial.println(); - - DebugSerial.print("Sent JSON Response: "); - DebugSerial.println(jsonResponse); -} - -void handle_ena_cmd() { - deviceEnabled = true; - DebugSerial.println("Device enabled"); - Serial.print("ena\n"); - sendJsonResponse("ena", "OK"); -} - -void handle_off_cmd() { - deviceEnabled = false; - isMoving = false; - DebugSerial.println("Device disabled"); - Serial.print("off\n"); - sendJsonResponse("off", "OK"); -} - -void handle_mov_cmd() { - if (!deviceEnabled) { - sendJsonResponse("mov", "DEVICE_DISABLED"); - return; - } - - isMoving = true; - DebugSerial.println("Starting movement"); - Serial.print("mov\n"); - sendJsonResponse("mov", "OK"); -} - -void handle_pos_cmd(int value) { - if (!deviceEnabled) { - sendJsonResponse("pos", "DEVICE_DISABLED"); - return; - } - - targetPosition = value; - currentPosition = value; // 模拟立即到达 - DebugSerial.print("Position set to: "); - DebugSerial.println(value); - sendJsonResponse("pos", "OK", value); -} - -void handle_rmv_cmd(int value) { - if (!deviceEnabled) { - sendJsonResponse("rmv", "DEVICE_DISABLED"); - return; - } - - currentPosition += value; - DebugSerial.print("Relative move by: "); - DebugSerial.print(value); - DebugSerial.print(", new position: "); - DebugSerial.println(currentPosition); - sendJsonResponse("rmv", "OK", currentPosition); -} - -void handle_pps_cmd(int value) { - if (value == -1) { - // SET PPS - Serial.print("pps\n"); - sendJsonResponse("pps", "OK", pulsePerSecond); - } else { - // 设置PPS值 - pulsePerSecond = value; - DebugSerial.print("Pulse per second set to: "); - DebugSerial.println(value); - sendJsonResponse("pps", "OK", value); - } -} - -void handle_org_cmd() { - if (!deviceEnabled) { - sendJsonResponse("org", "DEVICE_DISABLED"); - return; - } - - currentPosition = 0; - targetPosition = 0; - DebugSerial.println("Returned to origin"); - Serial.print("org\n"); - sendJsonResponse("org", "OK", 0); -} - -void handle_stp_cmd() { - isMoving = false; - DebugSerial.println("Movement stopped"); - Serial.print("stp\n"); - sendJsonResponse("stp", "OK"); -} - -void handle_zero_cmd(const char* value) { - if (strcmp(value, "start") == 0) { - Serial.print("zero start\n"); - isBackZero = true; - DebugSerial.println("Zero started"); - sendJsonResponse("zero", "ZERO_STARTED"); - } else if (strcmp(value, "stop") == 0) { - Serial.print("zero stop\n"); - isBackZero = false; - currentPosition = 0; - DebugSerial.println("Zero completed"); - sendJsonResponse("zero", "ZERO_COMPLETED"); - } else { - sendJsonResponse("zero", "INVALID_VALUE"); - } -} - -void printDeviceStatus() { - DebugSerial.println("\n=== Device Status ==="); - DebugSerial.print("Enabled: "); - DebugSerial.println(deviceEnabled ? "Yes" : "No"); - DebugSerial.print("Current Position: "); - DebugSerial.println(currentPosition); - DebugSerial.print("Target Position: "); - DebugSerial.println(targetPosition); - DebugSerial.print("Pulse Per Second: "); - DebugSerial.println(pulsePerSecond); - DebugSerial.print("Moving: "); - DebugSerial.println(isMoving ? "Yes" : "No"); - DebugSerial.print("Zero Backing: "); - DebugSerial.println(isBackZero ? "Yes" : "No"); - DebugSerial.println("==================\n"); } \ No newline at end of file diff --git a/src/vsmd_parser.cpp b/src/vsmd_parser.cpp index 8edf366..346afed 100644 --- a/src/vsmd_parser.cpp +++ b/src/vsmd_parser.cpp @@ -185,7 +185,8 @@ String VSMDParser::parseInfo(uint8_t* data, int size) { result += (char)data[i]; } } else { - result = "数据长度不足"; + result = "Not enough data"; + } break; }