0
0
Files
test/ComHS-main.cpp

114 lines
3.0 KiB
C++

#include <iostream>
#include <cstdlib>
#include <string>
#include <unistd.h>
#include <signal.h>
#include <fstream>
#include "ComHS.h"
using namespace std;
/* 全局运行标志 */
static volatile int keep_running = 1;
/* 信号处理函数 */
void signal_handler(int signum)
{
keep_running = 0;
}
/*
* 简单读取配置文件
*/
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[])
{
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;
}
start_scomm_service(telec_port.c_str(), telem_port.c_str(), baudrate);
if (S_COMM_ON_OFF != 1) {
return -1;
}
extern volatile int g_running;
while (g_running) {
sleep(1);
}
stop_scomm_service();
return 0;
}