加入vsmd test

This commit is contained in:
2025-06-30 13:27:25 +08:00
parent 89a0366629
commit c9383ce175
3 changed files with 390 additions and 366 deletions

View File

@ -1,6 +1,8 @@
#include <ArduinoJson.h>
#include "IRIS_Method.h"
#include "vsmd_parser.h"
VSMDParser parser;
#define DEBUG_TX_PIN 17 // TX
#define DEBUG_RX_PIN 16 // RX
@ -57,16 +59,17 @@ 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 handleConfigCommand(JsonObject& doc);
void handleEnableCommand();
void handleOffCommand();
void handleMoveCommand();
void handlePositionCommand(int value);
void handleRelativeMoveCommand(int value);
void handlePulsePerSecondCommand(int value = -1);
void handleOriginCommand();
void handleStopCommand();
void handleZeroCommand(const char* value);
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 setup() {
@ -77,7 +80,7 @@ void setup() {
Serial.begin(115200);
DebugSerial.println("VSMD INIT");
DebugSerial.println("Commands: cfg, ena, off, mov, pos, rmv, pps, org, stp, zero");
DebugSerial.println("Commands:dev, cfg, ena, off, mov, pos, rmv, pps, org, stp, zero");
printDeviceStatus();
}
@ -85,6 +88,22 @@ void loop() {
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) {
@ -148,44 +167,51 @@ void processJsonCommand(const char* jsonString) {
return;
}
const char* cmd = doc["cmd"];
if (cmd == nullptr) {
sendJsonResponse("error", "NO_CMD_FIELD");
// 使用 command 字段替代 cmd
const char* command = doc["command"];
if (command == nullptr) {
sendJsonResponse("error", "NO_COMMAND_FIELD");
return;
}
// 根据cmd执行相应操作
if (strcmp(cmd, "cfg") == 0) {
JsonObject configObj = doc.as<JsonObject>();
handleConfigCommand(configObj);
} else if (strcmp(cmd, "ena") == 0) {
handleEnableCommand();
} else if (strcmp(cmd, "off") == 0) {
handleOffCommand();
} else if (strcmp(cmd, "mov") == 0) {
handleMoveCommand();
} else if (strcmp(cmd, "pos") == 0) {
int value = doc["value"] | 0;
handlePositionCommand(value);
} else if (strcmp(cmd, "rmv") == 0) {
int value = doc["value"] | 0;
handleRelativeMoveCommand(value);
} else if (strcmp(cmd, "pps") == 0) {
if (doc.containsKey("value")) {
int value = doc["value"];
handlePulsePerSecondCommand(value);
// 获取 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<int>() : 0;
handle_pos_cmd(value);
} else if (strcmp(command, "rmv") == 0) {
int value = params.containsKey("value") ? params["value"].as<int>() : 0;
handle_rmv_cmd(value);
} else if (strcmp(command, "pps") == 0) {
if (params.containsKey("value")) {
int value = params["value"].as<int>();
handle_pps_cmd(value);
} else {
handlePulsePerSecondCommand();
handle_pps_cmd();
}
} else if (strcmp(cmd, "org") == 0) {
handleOriginCommand();
} else if (strcmp(cmd, "stp") == 0) {
handleStopCommand();
} else if (strcmp(cmd, "zero") == 0) {
const char* value = doc["value"] | "";
handleZeroCommand(value);
} 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<const char*>() : "";
handle_zero_cmd(value);
} else if (strcmp(command, "dev") == 0) {
handle_dev_cmd();
} else {
sendJsonResponse(cmd, "UNKNOWN_COMMAND");
sendJsonResponse(command, "UNKNOWN_COMMAND");
}
}
@ -195,31 +221,34 @@ void processStringCommand(const char* stringData) {
sendStringResponse(response.c_str());
}
void sendJsonResponse(const char* cmd, const char* status, int value) {
void sendJsonResponse(const char* command, const char* status, int value) {
StaticJsonDocument<JSON_BUFFER_SIZE> responseDoc;
responseDoc["cmd"] = cmd;
responseDoc["status"] = status;
// 修改使用新的JSON格式
responseDoc["command"] = command; // 使用 command 替代 cmd
responseDoc["ST"] = status; // 使用 ST 替代 status
// 如果有参数值,添加到 PA 对象中
if (value != -1) {
responseDoc["value"] = value;
JsonObject params = responseDoc.createNestedObject("PA");
params["value"] = value;
}
// 设备状态信息
responseDoc["enabled"] = deviceEnabled;
responseDoc["position"] = currentPosition;
responseDoc["moving"] = isMoving;
// 可选:添加其他信息
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.println();
DebugSerial.print("Sent JSON Response: ");
DebugSerial.println(jsonResponse);
// DebugSerial.print("Sent JSON Response: ");
// DebugSerial.println(jsonResponse);
}
void sendStringResponse(const char* response) {
@ -231,134 +260,45 @@ void sendStringResponse(const char* response) {
DebugSerial.println(response);
}
void handleConfigCommand(JsonObject& doc) {
DebugSerial.println("Processing configuration command...");
void handle_cfg_cmd() {
DebugSerial.println("Configuration requested");
Serial.println("cfg\n");
// 返回配置相关信息
sendJsonResponse("cfg", "OK");
}
// void handle_dev_cmd() {
// DebugSerial.println("dev_requested");
// Serial.println("dev\n");
// //test VSMD数据
// 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
// };
// // 解析
// DebugSerial.println("Parsing VSMD test data:");
// parser.parse(testData, sizeof(testData));
// // 返回设备相关信息
// sendJsonResponse("dev", "OK");
// }
void handle_dev_cmd() {
DebugSerial.println("dev_requested");
Serial.println("dev\n");
// 创建响应JSON对象
// 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<JSON_BUFFER_SIZE> responseDoc;
responseDoc["cmd"] = "cfg";
responseDoc["status"] = "OK";
responseDoc["command"] = "dev";
responseDoc["ST"] = "OK";
responseDoc["Other"] = deviceInfo;
// 添加设备状态信息
responseDoc["enabled"] = deviceEnabled;
responseDoc["position"] = currentPosition;
responseDoc["moving"] = isMoving;
// 创建配置参数对象
JsonObject configParams = responseDoc.createNestedObject("config");
// 处理配置参数
if (doc.containsKey("params")) {
JsonObject params = doc["params"];
// 更新配置参数并添加到响应中
if (params.containsKey("cfg zmd")) {
deviceConfig.zmd = params["cfg zmd"];
configParams["cfg zmd"] = deviceConfig.zmd;
}
if (params.containsKey("cfg snr")) {
deviceConfig.snr = params["cfg snr"];
configParams["cfg snr"] = deviceConfig.snr;
}
if (params.containsKey("cfg osv")) {
deviceConfig.osv = params["cfg osv"];
configParams["cfg osv"] = deviceConfig.osv;
}
if (params.containsKey("cfg zsd")) {
deviceConfig.zsd = params["cfg zsd"];
configParams["cfg zsd"] = deviceConfig.zsd;
}
if (params.containsKey("cfg zsp")) {
deviceConfig.zsp = params["cfg zsp"];
configParams["cfg zsp"] = deviceConfig.zsp;
}
if (params.containsKey("cfg dmd")) {
deviceConfig.dmd = params["cfg dmd"];
configParams["cfg dmd"] = deviceConfig.dmd;
}
if (params.containsKey("cfg dar")) {
deviceConfig.dar = params["cfg dar"];
configParams["cfg dar"] = deviceConfig.dar;
}
if (params.containsKey("cfg msr")) {
deviceConfig.msr = params["cfg msr"];
configParams["cfg msr"] = deviceConfig.msr;
}
if (params.containsKey("cfg msv")) {
deviceConfig.msv = params["cfg msv"];
configParams["cfg msv"] = deviceConfig.msv;
}
if (params.containsKey("cfg psr")) {
deviceConfig.psr = params["cfg psr"];
configParams["cfg psr"] = deviceConfig.psr;
}
if (params.containsKey("cfg psv")) {
deviceConfig.psv = params["cfg psv"];
configParams["cfg psv"] = deviceConfig.psv;
}
if (params.containsKey("cfg bdr")) {
deviceConfig.bdr = params["cfg bdr"];
configParams["cfg bdr"] = deviceConfig.bdr;
}
if (params.containsKey("cfg cid")) {
deviceConfig.cid = params["cfg cid"];
configParams["cfg cid"] = deviceConfig.cid;
}
if (params.containsKey("cfg mcs")) {
deviceConfig.mcs = params["cfg mcs"];
configParams["cfg mcs"] = deviceConfig.mcs;
}
if (params.containsKey("cfg spd")) {
deviceConfig.spd = params["cfg spd"];
configParams["cfg spd"] = deviceConfig.spd;
}
if (params.containsKey("cfg acc")) {
deviceConfig.acc = params["cfg acc"];
configParams["cfg acc"] = deviceConfig.acc;
}
if (params.containsKey("cfg dec")) {
deviceConfig.dec = params["cfg dec"];
configParams["cfg dec"] = deviceConfig.dec;
}
if (params.containsKey("cfg cra")) {
deviceConfig.cra = params["cfg cra"];
configParams["cfg cra"] = deviceConfig.cra;
}
if (params.containsKey("cfg crn")) {
deviceConfig.crn = params["cfg crn"];
configParams["cfg crn"] = deviceConfig.crn;
}
if (params.containsKey("cfg crh")) {
deviceConfig.crh = params["cfg crh"];
configParams["cfg crh"] = deviceConfig.crh;
}
DebugSerial.println("Configuration updated successfully");
} else {
// 如果没有params返回所有当前配置
configParams["cfg zmd"] = deviceConfig.zmd;
configParams["cfg snr"] = deviceConfig.snr;
configParams["cfg osv"] = deviceConfig.osv;
configParams["cfg zsd"] = deviceConfig.zsd;
configParams["cfg zsp"] = deviceConfig.zsp;
configParams["cfg dmd"] = deviceConfig.dmd;
configParams["cfg dar"] = deviceConfig.dar;
configParams["cfg msr"] = deviceConfig.msr;
configParams["cfg msv"] = deviceConfig.msv;
configParams["cfg psr"] = deviceConfig.psr;
configParams["cfg psv"] = deviceConfig.psv;
configParams["cfg bdr"] = deviceConfig.bdr;
configParams["cfg cid"] = deviceConfig.cid;
configParams["cfg mcs"] = deviceConfig.mcs;
configParams["cfg spd"] = deviceConfig.spd;
configParams["cfg acc"] = deviceConfig.acc;
configParams["cfg dec"] = deviceConfig.dec;
configParams["cfg cra"] = deviceConfig.cra;
configParams["cfg crn"] = deviceConfig.crn;
configParams["cfg crh"] = deviceConfig.crh;
}
// 序列化并发送响应
char jsonResponse[JSON_BUFFER_SIZE];
serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse));
@ -366,18 +306,20 @@ void handleConfigCommand(JsonObject& doc) {
packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer);
DebugSerial.write(sendBuffer, packedLength);
DebugSerial.print("Sent Config Response: ");
DebugSerial.println();
DebugSerial.print("Sent JSON Response: ");
DebugSerial.println(jsonResponse);
}
void handleEnableCommand() {
void handle_ena_cmd() {
deviceEnabled = true;
DebugSerial.println("Device enabled");
Serial.print("ena\n");
sendJsonResponse("ena", "OK");
}
void handleOffCommand() {
void handle_off_cmd() {
deviceEnabled = false;
isMoving = false;
DebugSerial.println("Device disabled");
@ -385,7 +327,7 @@ void handleOffCommand() {
sendJsonResponse("off", "OK");
}
void handleMoveCommand() {
void handle_mov_cmd() {
if (!deviceEnabled) {
sendJsonResponse("mov", "DEVICE_DISABLED");
return;
@ -397,7 +339,7 @@ void handleMoveCommand() {
sendJsonResponse("mov", "OK");
}
void handlePositionCommand(int value) {
void handle_pos_cmd(int value) {
if (!deviceEnabled) {
sendJsonResponse("pos", "DEVICE_DISABLED");
return;
@ -410,7 +352,7 @@ void handlePositionCommand(int value) {
sendJsonResponse("pos", "OK", value);
}
void handleRelativeMoveCommand(int value) {
void handle_rmv_cmd(int value) {
if (!deviceEnabled) {
sendJsonResponse("rmv", "DEVICE_DISABLED");
return;
@ -424,7 +366,7 @@ void handleRelativeMoveCommand(int value) {
sendJsonResponse("rmv", "OK", currentPosition);
}
void handlePulsePerSecondCommand(int value) {
void handle_pps_cmd(int value) {
if (value == -1) {
// SET PPS
Serial.print("pps\n");
@ -438,7 +380,7 @@ void handlePulsePerSecondCommand(int value) {
}
}
void handleOriginCommand() {
void handle_org_cmd() {
if (!deviceEnabled) {
sendJsonResponse("org", "DEVICE_DISABLED");
return;
@ -451,14 +393,14 @@ void handleOriginCommand() {
sendJsonResponse("org", "OK", 0);
}
void handleStopCommand() {
void handle_stp_cmd() {
isMoving = false;
DebugSerial.println("Movement stopped");
Serial.print("stp\n");
sendJsonResponse("stp", "OK");
}
void handleZeroCommand(const char* value) {
void handle_zero_cmd(const char* value) {
if (strcmp(value, "start") == 0) {
Serial.print("zero start\n");
isBackZero = true;