This commit is contained in:
2026-04-23 11:30:05 +08:00
parent 4260918f36
commit 01df5523a7
11 changed files with 480 additions and 3229 deletions

View File

@ -8,6 +8,10 @@
// 外部串口对象声明
extern HardwareSerial DebugSerial;
// 全局变量用于CID和RS485模式管理
static uint8_t currentTargetCID = 0; // 当前目标设备CID
static bool isCurrentRS485Mode = false; // 当前是否为RS485模式
void sendJsonResponse(const char* command, const char* status, int value) {
StaticJsonDocument<JSON_BUFFER_SIZE> responseDoc;
@ -63,218 +67,244 @@ void processJsonCommand(const char* jsonString) {
return;
}
// 解析命令中的CID
uint8_t cid = 0;
char actualCommand[64];
bool isRS485 = parse_command_with_cid(command, &cid, actualCommand);
// 设置全局变量供其他函数使用
currentTargetCID = cid;
isCurrentRS485Mode = isRS485;
if (isRS485) {
DebugSerial.print("RS485 Command - CID: ");
DebugSerial.print(cid);
DebugSerial.print(", Command: ");
DebugSerial.println(actualCommand);
} else {
DebugSerial.print("RS232 Command: ");
DebugSerial.println(actualCommand);
}
// 获取 PA 参数对象(可选)
JsonObject params = doc["PA"];
// 根据command执行相应操作
if (strcmp(command, "cfg") == 0) {
// 根据actualCommand执行相应操作
if (strcmp(actualCommand, "cfg") == 0) {
handle_cfg_cmd();
} else if (strcmp(command, "ena") == 0) {
} else if (strcmp(actualCommand, "ena") == 0) {
handle_ena_cmd();
} else if (strcmp(command, "off") == 0) {
} else if (strcmp(actualCommand, "off") == 0) {
handle_off_cmd();
} else if (strcmp(command, "mov") == 0) {
} else if (strcmp(actualCommand, "mov") == 0) {
handle_mov_cmd();
} else if (strcmp(command, "pos") == 0) {
} else if (strcmp(actualCommand, "pos") == 0) {
int value = params.containsKey("value") ? params["value"].as<int>() : 0;
handle_pos_cmd(value);
} else if (strcmp(command, "rmv") == 0) {
} else if (strcmp(actualCommand, "rmv") == 0) {
int value = params.containsKey("value") ? params["value"].as<int>() : 0;
handle_rmv_cmd(value);
} else if (strcmp(command, "pps") == 0) {
} else if (strcmp(actualCommand, "pps") == 0) {
if (params.containsKey("value")) {
int value = params["value"].as<int>();
handle_pps_cmd(value);
} else {
handle_pps_cmd();
}
} else if (strcmp(command, "org") == 0) {
} else if (strcmp(actualCommand, "org") == 0) {
handle_org_cmd();
} else if (strcmp(command, "stp") == 0) {
} else if (strcmp(actualCommand, "stp") == 0) {
if (params.containsKey("value")) {
int value = params["value"].as<int>();
handle_stp_cmd(value);
} else {
handle_stp_cmd(); // 无参数默认为0减速停止
}
} else if (strcmp(command, "dev") == 0) {
} else if (strcmp(actualCommand, "dev") == 0) {
handle_dev_cmd();
} else if (strcmp(command, "sts") == 0) {
} else if (strcmp(actualCommand, "sts") == 0) {
handle_sts_cmd();
} else if (strcmp(command, "zero_start") == 0) {
} else if (strcmp(actualCommand, "zero_start") == 0) {
handle_zero_start_cmd();
} else if (strcmp(command, "zero_stop") == 0) {
} else if (strcmp(actualCommand, "zero_stop") == 0) {
handle_zero_stop_cmd();
} else if (strcmp(command, "cfg_bdr") == 0) {
} else if (strcmp(actualCommand, "cfg_bdr") == 0) {
int baudrate = params["value"].as<int>();
handle_cfg_bdr_cmd(baudrate);
} else if (strcmp(command, "cfg_mcs") == 0) {
} else if (strcmp(actualCommand, "cfg_mcs") == 0) {
//设置微步细分
int mcs = params["value"].as<int>();
handle_cfg_mcs_cmd(mcs);
} else if (strcmp(command, "cfg_spd") == 0) {
} else if (strcmp(actualCommand, "cfg_spd") == 0) {
//设置运行速度
int spd = params["value"].as<int>();
handle_cfg_spd_cmd(spd);
} else if (strcmp(command, "cfg_acc") == 0) {
} else if (strcmp(actualCommand, "cfg_acc") == 0) {
//设置加速度
int acc = params["value"].as<int>();
handle_cfg_accSpeed_cmd(acc);
} else if (strcmp(command, "cfg_dec") == 0) {
} else if (strcmp(actualCommand, "cfg_dec") == 0) {
//设置减速度
int dec = params["value"].as<int>();
handle_cfg_decSpeed_cmd(dec);
} else if (strcmp(command, "cfg_cra") == 0) {
} else if (strcmp(actualCommand, "cfg_cra") == 0) {
//设置加速时电流
float cra = params["value"].as<float>();
handle_cfg_cra_cmd(cra);
} else if (strcmp(command, "cfg_crn") == 0) {
} else if (strcmp(actualCommand, "cfg_crn") == 0) {
//设置匀速时电流
float crn = params["value"].as<float>();
handle_cfg_crn_cmd(crn);
} else if (strcmp(command, "cfg_crh") == 0) {
} else if (strcmp(actualCommand, "cfg_crh") == 0) {
//设置HOLD电流
float crh = params["value"].as<float>();
handle_cfg_crh_cmd(crh);
// ***************************S3-S6传感器触发事件配置参数范围0-9**********************/
} else if (strcmp(command, "cfg_s3f") == 0) {
} else if (strcmp(actualCommand, "cfg_s3f") == 0) {
int s3f = params["value"].as<int>();
handle_cfg_sensor_cmd("s3f", s3f);
} else if (strcmp(command, "cfg_s3r") == 0) {
} else if (strcmp(actualCommand, "cfg_s3r") == 0) {
int s3r = params["value"].as<int>();
handle_cfg_sensor_cmd("s3r", s3r);
} else if (strcmp(command, "cfg_s4f") == 0) {
} else if (strcmp(actualCommand, "cfg_s4f") == 0) {
int s4f = params["value"].as<int>();
handle_cfg_sensor_cmd("s4f", s4f);
} else if (strcmp(command, "cfg_s4r") == 0) {
} else if (strcmp(actualCommand, "cfg_s4r") == 0) {
int s4r = params["value"].as<int>();
handle_cfg_sensor_cmd("s4r", s4r);
} else if (strcmp(command, "cfg_s5f") == 0) {
} else if (strcmp(actualCommand, "cfg_s5f") == 0) {
int s5f = params["value"].as<int>();
handle_cfg_sensor_cmd("s5f", s5f);
} else if (strcmp(command, "cfg_s5r") == 0) {
} else if (strcmp(actualCommand, "cfg_s5r") == 0) {
int s5r = params["value"].as<int>();
handle_cfg_sensor_cmd("s5r", s5r);
} else if (strcmp(command, "cfg_s6f") == 0) {
} else if (strcmp(actualCommand, "cfg_s6f") == 0) {
int s6f = params["value"].as<int>();
handle_cfg_sensor_cmd("s6f", s6f);
} else if (strcmp(command, "cfg_s6r") == 0) {
} else if (strcmp(actualCommand, "cfg_s6r") == 0) {
int s6r = params["value"].as<int>();
handle_cfg_sensor_cmd("s6r", s6r);
// ***************************S3-S6传感器工作模式配置参数范围0-1**********************/
} else if (strcmp(command, "cfg_s3") == 0) {
} else if (strcmp(actualCommand, "cfg_s3") == 0) {
int s3 = params["value"].as<int>();
handle_cfg_sensor_cmd("s3", s3);
} else if (strcmp(command, "cfg_s4") == 0) {
} else if (strcmp(actualCommand, "cfg_s4") == 0) {
int s4 = params["value"].as<int>();
handle_cfg_sensor_cmd("s4", s4);
} else if (strcmp(command, "cfg_s5") == 0) {
} else if (strcmp(actualCommand, "cfg_s5") == 0) {
int s5 = params["value"].as<int>();
handle_cfg_sensor_cmd("s5", s5);
} else if (strcmp(command, "cfg_s6") == 0) {
} else if (strcmp(actualCommand, "cfg_s6") == 0) {
int s6 = params["value"].as<int>();
handle_cfg_sensor_cmd("s6", s6);
//**************************************************************** */
} else if (strcmp(command, "cfg_zmd") == 0) {
} else if (strcmp(actualCommand, "cfg_zmd") == 0) {
//设置归零功能
int zmd = params["value"].as<int>();
handle_cfg_zmd_cmd(zmd);
} else if (strcmp(command, "cfg_snr") == 0) {
} else if (strcmp(actualCommand, "cfg_snr") == 0) {
//设置归零用传感器
int snr = params["value"].as<int>();
handle_cfg_snr_cmd(snr);
} else if (strcmp(command, "cfg_osv") == 0) {
} else if (strcmp(actualCommand, "cfg_osv") == 0) {
//设置归零传感器OPEN时电平
int osv = params["value"].as<int>();
handle_cfg_osv_cmd(osv);
} else if (strcmp(command, "cfg_zsd") == 0) {
} else if (strcmp(actualCommand, "cfg_zsd") == 0) {
//设置归零速度
int zsd = params["value"].as<int>();
handle_cfg_zsd_cmd(zsd);
} else if (strcmp(command, "cfg_zsp") == 0) {
} else if (strcmp(actualCommand, "cfg_zsp") == 0) {
//设置归零后的安全位置
int zsp = params["value"].as<int>();
handle_cfg_zsp_cmd(zsp);
} else if (strcmp(command, "cfg_dmd") == 0) {
} else if (strcmp(actualCommand, "cfg_dmd") == 0) {
//设置离线运行模式
int dmd = params["value"].as<int>();
handle_cfg_dmd_cmd(dmd);
} else if (strcmp(command, "cfg_dar") == 0) {
} else if (strcmp(actualCommand, "cfg_dar") == 0) {
//设置无握手启动时间
int dar = params["value"].as<int>();
handle_cfg_dar_cmd(dar);
} else if (strcmp(command, "cfg_msr") == 0) {
} else if (strcmp(actualCommand, "cfg_msr") == 0) {
//设置负极限传感器
int msr = params["value"].as<int>();
handle_cfg_msr_cmd(msr);
} else if (strcmp(command, "cfg_msv") == 0) {
} else if (strcmp(actualCommand, "cfg_msv") == 0) {
//设置负极限触发电平
int msv = params["value"].as<int>();
handle_cfg_msv_cmd(msv);
} else if (strcmp(command, "cfg_psr") == 0) {
} else if (strcmp(actualCommand, "cfg_psr") == 0) {
//设置正极限传感器
int psr = params["value"].as<int>();
handle_cfg_psr_cmd(psr);
} else if (strcmp(command, "cfg_psv") == 0) {
} else if (strcmp(actualCommand, "cfg_psv") == 0) {
//设置正极限触发电平
int psv = params["value"].as<int>();
handle_cfg_psv_cmd(psv);
} else if (strcmp(command, "cfg_pae") == 0) {
} else if (strcmp(actualCommand, "cfg_pae") == 0) {
//设置上电使能
int pae = params["value"].as<int>();
handle_cfg_pae_cmd(pae);
} else if (strcmp(command, "cfg_emod") == 0) {
} else if (strcmp(actualCommand, "cfg_emod") == 0) {
//设置编码器模式
int emod = params["value"].as<int>();
handle_cfg_emod_cmd(emod);
} else if (strcmp(command, "cfg_elns") == 0) {
} else if (strcmp(actualCommand, "cfg_elns") == 0) {
//设置编码器线数
int elns = params["value"].as<int>();
handle_cfg_elns_cmd(elns);
} else if (strcmp(command, "cfg_estp") == 0) {
} else if (strcmp(actualCommand, "cfg_estp") == 0) {
//设置电机每圈整步数
int estp = params["value"].as<int>();
handle_cfg_estp_cmd(estp);
} else if (strcmp(command, "cfg_erty") == 0) {
} else if (strcmp(actualCommand, "cfg_erty") == 0) {
//设置堵转后重试次数
int erty = params["value"].as<int>();
handle_cfg_erty_cmd(erty);
} else if (strcmp(command, "cfg_edir") == 0) {
} else if (strcmp(actualCommand, "cfg_edir") == 0) {
//设置编码器方向
int edir = params["value"].as<int>();
handle_cfg_edir_cmd(edir);
} else if (strcmp(command, "cfg_ez") == 0) {
} else if (strcmp(actualCommand, "cfg_ez") == 0) {
//设置编码器灵敏度
int ez = params["value"].as<int>();
handle_cfg_ez_cmd(ez);
} else if (strcmp(command, "cfg_ewr") == 0) {
} else if (strcmp(actualCommand, "cfg_ewr") == 0) {
//设置堵转后动作
int ewr = params["value"].as<int>();
handle_cfg_ewr_cmd(ewr);
} else if (strcmp(command, "sav") == 0) {
} else if (strcmp(actualCommand, "sav") == 0) {
handle_sav_cmd();
// ***************************S3-S6端口输出控制**********************/
} else if (strcmp(command, "s3_on") == 0) {
} else if (strcmp(actualCommand, "s3_on") == 0) {
handle_port_output_cmd("s3", "on");
} else if (strcmp(command, "s3_off") == 0) {
} else if (strcmp(actualCommand, "s3_off") == 0) {
handle_port_output_cmd("s3", "off");
} else if (strcmp(command, "s4_on") == 0) {
} else if (strcmp(actualCommand, "s4_on") == 0) {
handle_port_output_cmd("s4", "on");
} else if (strcmp(command, "s4_off") == 0) {
} else if (strcmp(actualCommand, "s4_off") == 0) {
handle_port_output_cmd("s4", "off");
} else if (strcmp(command, "s5_on") == 0) {
} else if (strcmp(actualCommand, "s5_on") == 0) {
handle_port_output_cmd("s5", "on");
} else if (strcmp(command, "s5_off") == 0) {
} else if (strcmp(actualCommand, "s5_off") == 0) {
handle_port_output_cmd("s5", "off");
} else if (strcmp(command, "s6_on") == 0) {
} else if (strcmp(actualCommand, "s6_on") == 0) {
handle_port_output_cmd("s6", "on");
} else if (strcmp(command, "s6_off") == 0) {
} else if (strcmp(actualCommand, "s6_off") == 0) {
handle_port_output_cmd("s6", "off");
//************************************************************* */
}
else {
sendJsonResponse(command, "UNKNOWN_COMMAND");
sendJsonResponse(actualCommand, "UNKNOWN_COMMAND");
}
// // 存储当前使用的CID供命令处理函数使用
// if (isRS485) {
// // 可以在这里设置全局变量或传递给处理函数
// DebugSerial.print("Command will be sent to device CID: ");
// DebugSerial.println(cid);
// }
}
void processStringCommand(const char* stringData) {
@ -283,21 +313,69 @@ void processStringCommand(const char* stringData) {
sendStringResponse(response.c_str());
}
void send_vsmd_cmd(const char* cmd, int value) {
// 解析包含CID的命令字符串
bool parse_command_with_cid(const char* command, uint8_t* cid, char* actualCommand) {
// 检查命令是否以数字开头CID
if (isdigit(command[0])) {
// 解析CID
char* endPtr;
long parsedCID = strtol(command, &endPtr, 10);
// 检查CID范围和格式
if (parsedCID >= 0 && parsedCID <= 32 && *endPtr == ' ') {
*cid = (uint8_t)parsedCID;
// 跳过CID和空格获取实际命令
strcpy(actualCommand, endPtr + 1);
return true; // RS485模式
}
}
// 没有CID前缀直接复制命令
*cid = 0; // 默认CID
strcpy(actualCommand, command);
return false; // RS232模式
}
// 带CID的命令发送函数
void send_vsmd_cmd_with_cid(uint8_t cid, const char* cmd, int value) {
// 发送CID前缀
Serial.print(cid);
Serial.print(" ");
// 发送命令
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 send_vsmd_cmd(const char* cmd, int value) {
if (isCurrentRS485Mode) {
send_vsmd_cmd_with_cid(currentTargetCID, cmd, value);
} else {
// 原有RS232模式
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): ");

View File

@ -16,6 +16,9 @@ void send_vsmd_cmd(const char* cmd, int value = -1);
// send_vsmd_cmd("dev"); // 不带参数,使用默认值-1
// send_vsmd_cmd("pos", value); // 带参数
// RS485通信函数
void send_vsmd_cmd_with_cid(uint8_t cid, const char* cmd, int value = -1);
bool parse_command_with_cid(const char* command, uint8_t* cid, char* actualCommand);
// 打印Serial串口收到的原始数据
void printSerialRawData(const uint8_t* buffer, int bytesRead);
//VSMD串口数据解析

View File

@ -200,7 +200,7 @@ void initBluetooth() {
btAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
DebugSerial.println("VSMD_Ble_Controller initialized");
DebugSerial.println("Ble Initialized");
// 初始化包接收状态
resetPacketReceiving();

View File

@ -6,6 +6,11 @@
VSMDParser parser;
#define USE_RS232 // 注释掉这行表示使用 RS-485取消注释表示使用 RS-232
//#define USE_RS485
#define PWR_232CTL 5 // 232电源控制引脚
#define PWR_485CTL 15 // 485电源控制引脚
#define DEBUG_TX_PIN 17 // TX
#define DEBUG_RX_PIN 16 // RX
#define BUFFER_SIZE 1024
@ -29,27 +34,48 @@ int32_t pulsePerSecond;
void printDeviceStatus();
void handleSerialData();
bool Dev_Mode = false;
void printDeviceStatus() {
DebugSerial.println("\n=== Device Status ===");
DebugSerial.print("Enabled: ");
DebugSerial.print("Device Enabled: ");
DebugSerial.println(deviceEnabled ? "Yes" : "No");
DebugSerial.print("Bluetooth: ");
DebugSerial.println(isBluetoothConnected() ? "Connected" : "Disconnected");
DebugSerial.println("==================\n");
DebugSerial.print("Ble Connected: ");
DebugSerial.println(isBluetoothConnected() ? "Yes" : "No");
DebugSerial.print("Dev Mode: ");
DebugSerial.println(Dev_Mode ? "RS232" : "RS485");
}
void Power_Enable()
{
pinMode(PWR_232CTL, OUTPUT);
pinMode(PWR_485CTL, OUTPUT);
#ifdef USE_RS232
digitalWrite(PWR_232CTL, HIGH);
digitalWrite(PWR_485CTL, LOW);
Dev_Mode = true;
#elif defined(USE_RS485)
digitalWrite(PWR_485CTL, HIGH);
digitalWrite(PWR_232CTL, LOW);
Dev_Mode = false;
#endif
}
void setup() {
// 调试串口
DebugSerial.begin(9600, SERIAL_8N1, DEBUG_RX_PIN, DEBUG_TX_PIN);
DebugSerial.setTimeout(1);
DebugSerial.setTimeout(100);
// 电机数据通信串口
Serial.begin(9600);
Serial.setTimeout(100);
DebugSerial.println("VSMD INIT");
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");
// DebugSerial.println("VSMD INIT");
// 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");
//初始化电源控制引脚
Power_Enable();
// 初始化蓝牙
initBluetooth();