0
0

Auto commit from DCSP - 2026/1/9 10:43:14

This commit is contained in:
xb
2026-01-09 10:43:14 +08:00
parent 0fd98e248b
commit 10d2060201
10 changed files with 186 additions and 109 deletions

View File

@@ -3,6 +3,7 @@
#include <string>
#include <unistd.h>
#include <signal.h>
#include <fstream>
#include "ComHS.h"
@@ -15,52 +16,99 @@ static volatile int keep_running = 1;
void signal_handler(int signum)
{
keep_running = 0;
cout << "\n[INFO] Received signal " << signum << ", stopping..." << endl;
}
/*
* 主函数
*
* 命令行参数:
* argv[1]: 遥控串口设备路径 (默认: /dev/ttyUSB0)
* argv[2]: 遥测串口设备路径 (默认: /dev/ttyUSB1)
* argv[3]: 波特率 (默认: 115200)
* 简单读取配置文件
*/
bool read_config_simple(const char* filename, string& telec_port, string& telem_port, int& baudrate)
{
// 默认值
telec_port = "/dev/pts/10";
telem_port = "/dev/pts/12";
baudrate = 115200;
// 尝试打开文件
ifstream file(filename);
if (!file.is_open()) {
return false; // 文件不存在,使用默认值
}
string line;
while (getline(file, line)) {
// 查找关键字段
if (line.find("\"telecontrol_port\"") != string::npos) {
size_t pos = line.find(":");
if (pos != string::npos) {
size_t start = line.find("\"", pos + 1);
size_t end = line.find("\"", start + 1);
if (start != string::npos && end != string::npos) {
telec_port = line.substr(start + 1, end - start - 1);
}
}
}
else if (line.find("\"telemetry_port\"") != string::npos) {
size_t pos = line.find(":");
if (pos != string::npos) {
size_t start = line.find("\"", pos + 1);
size_t end = line.find("\"", start + 1);
if (start != string::npos && end != string::npos) {
telem_port = line.substr(start + 1, end - start - 1);
}
}
}
else if (line.find("\"baudrate\"") != string::npos) {
size_t pos = line.find(":");
if (pos != string::npos) {
// 跳过空格和冒号
pos++;
while (pos < line.length() && (line[pos] == ' ' || line[pos] == ':')) pos++;
// 提取数字部分
string num_str;
while (pos < line.length() && isdigit(line[pos])) {
num_str += line[pos];
pos++;
}
if (!num_str.empty()) {
baudrate = stoi(num_str);
}
}
}
}
return true;
}
/*
* 主函数
*/
int main(int argc, char *argv[])
{
const char *dev_telec = "/dev/ttyUSB0"; /* 遥控串口 */
const char *dev_telem = "/dev/ttyUSB1"; /* 遥测串口 */
int baudrate = 115200;
/* 从命令行参数获取配置 */
if (argc >= 3) {
dev_telec = argv[1];
dev_telem = argv[2];
string telec_port, telem_port;
int baudrate;
const char* config_file = "ComHS.json";
// 尝试读取配置文件
if (read_config_simple(config_file, telec_port, telem_port, baudrate)) {
cout << "[INFO] Loaded config from " << config_file << endl;
} else {
cout << "[INFO] Using default configuration" << endl;
}
if (argc >= 4) {
baudrate = atoi(argv[3]);
start_scomm_service(telec_port.c_str(), telem_port.c_str(), baudrate);
if (S_COMM_ON_OFF != 1) {
return -1;
}
/* 设置信号处理 */
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* 启动服务 */
start_scomm_service(dev_telec, dev_telem, baudrate);
while (keep_running) {
static int counter = 0;
if (counter++ >= 10) {
counter = 0;
}
extern volatile int g_running;
while (g_running) {
sleep(1);
}
cout << "[INFO] Stopping service..." << endl;
stop_scomm_service();
cout << "[INFO] Service stopped successfully" << endl;
return 0;
}