protobuf server

This commit is contained in:
Ivan I. Ovchinnikov
2026-07-28 20:46:41 +03:00
parent 5a702fe0f8
commit 8c4d40b66a
11 changed files with 150 additions and 56 deletions
+19 -20
View File
@@ -1,6 +1,5 @@
#include "../include/ConnectionWindow.h"
#include "imgui.h"
#include <cstdio>
ConnectionWindow::ConnectionWindow()
: selectedPortIdx(0), connected(false), activePort(nullptr) {
@@ -27,10 +26,10 @@ void ConnectionWindow::refreshPorts() {
ports.clear();
selectedPortIdx = 0;
struct sp_port** port_list;
if (sp_list_ports(&port_list) == SP_OK) {
for (int i = 0; port_list[i] != nullptr; i++) {
struct sp_port* port = port_list[i];
sp_port** portList;
if (sp_list_ports(&portList) == SP_OK) {
for (int i = 0; portList[i] != nullptr; i++) {
const sp_port* port = portList[i];
SerialPortInfo info;
const char* name = sp_get_port_name(port);
info.portName = name ? name : "Unknown";
@@ -38,7 +37,7 @@ void ConnectionWindow::refreshPorts() {
info.description = desc ? desc : "No Description";
ports.push_back(info);
}
sp_free_port_list(port_list);
sp_free_port_list(portList);
}
if (ports.empty()) {
@@ -50,8 +49,8 @@ void ConnectionWindow::forceLogMessage(const std::string& msg) {
txLog += msg + "\n";
}
void ConnectionWindow::render(bool* p_open) {
if (!ImGui::Begin("Connection Settings", p_open)) {
void ConnectionWindow::render(bool* paramOpen) {
if (!ImGui::Begin("Connection Settings", paramOpen)) {
ImGui::End();
return;
}
@@ -62,17 +61,17 @@ void ConnectionWindow::render(bool* p_open) {
ImGui::SameLine();
// 1. Выбор COM-порта
std::string combo_preview = ports[selectedPortIdx].portName;
std::string preview = ports[selectedPortIdx].portName;
if (ports[selectedPortIdx].portName != "None") {
combo_preview += " (" + ports[selectedPortIdx].description + ")";
preview += " (" + ports[selectedPortIdx].description + ")";
}
ImGui::BeginDisabled(connected);
if (ImGui::BeginCombo("Serial Port", combo_preview.c_str())) {
if (ImGui::BeginCombo("Serial Port", preview.c_str())) {
for (int n = 0; n < ports.size(); n++) {
const bool is_selected = (selectedPortIdx == n);
const bool isSelected = (selectedPortIdx == n);
std::string item_text = ports[n].portName + " - " + ports[n].description;
if (ImGui::Selectable(item_text.c_str(), is_selected)) {
if (ImGui::Selectable(item_text.c_str(), isSelected)) {
selectedPortIdx = n;
}
}
@@ -80,13 +79,13 @@ void ConnectionWindow::render(bool* p_open) {
}
// 2. Выбор Baud Rate через выпадающий список
std::string baud_preview = std::to_string(baudRates[selectedBaudIdx]);
if (ImGui::BeginCombo("Baud Rate", baud_preview.c_str())) {
const std::string baudPreview = std::to_string(baudRates[selectedBaudIdx]);
if (ImGui::BeginCombo("Baud Rate", baudPreview.c_str())) {
for (int b = 0; b < baudRates.size(); b++) {
const bool is_selected = (selectedBaudIdx == b);
const bool isSelected = (selectedBaudIdx == b);
std::string baud_item_text = std::to_string(baudRates[b]);
if (ImGui::Selectable(baud_item_text.c_str(), is_selected)) {
if (ImGui::Selectable(baud_item_text.c_str(), isSelected)) {
selectedBaudIdx = b;
}
}
@@ -102,15 +101,15 @@ void ConnectionWindow::render(bool* p_open) {
if (sp_open(activePort, SP_MODE_READ_WRITE) == SP_OK) {
// Передаем реальное значение скорости из выбранного индекса массива
int real_baud = baudRates[selectedBaudIdx];
sp_set_baudrate(activePort, real_baud);
const int selectedBaud = baudRates[selectedBaudIdx];
sp_set_baudrate(activePort, selectedBaud);
sp_set_bits(activePort, 8);
sp_set_parity(activePort, SP_PARITY_NONE);
sp_set_stopbits(activePort, 1);
connected = true;
forceLogMessage("[SYSTEM] Connected to " + ports[selectedPortIdx].portName + " at " + std::to_string(real_baud) + " baud.");
forceLogMessage("[SYSTEM] Connected to " + ports[selectedPortIdx].portName + " at " + std::to_string(selectedBaud) + " baud.");
} else {
forceLogMessage("[SYSTEM ERROR] Could not open port " + ports[selectedPortIdx].portName);
sp_free_port(activePort);
+10 -5
View File
@@ -33,13 +33,18 @@ void PayloadWindow::sendHexPayload(const std::string& hexString, ConnectionWindo
return;
}
int bytes_written = sp_nonblocking_write(port, bytes.data(), bytes.size());
const int bytesWritten = sp_nonblocking_write(port, bytes.data(), bytes.size());
if (bytes_written >= 0) {
if (bytesWritten >= 0) {
std::stringstream log_ss;
log_ss << "[TX] Sent " << bytes_written << " bytes: ";
for (size_t i = 0; i < bytes.size(); ++i) {
log_ss << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (int)bytes[i] << " ";
log_ss << "[TX] Sent " << bytesWritten << " bytes: ";
for (unsigned char byte : bytes) {
log_ss << std::uppercase
<< std::hex
<< std::setw(2)
<< std::setfill('0')
<< static_cast<int>(byte)
<< " ";
}
connectionWindow.forceLogMessage(log_ss.str());
} else {
+11 -4
View File
@@ -19,7 +19,9 @@ void SerialApp::initialize() {
// Вспомогательная функция перевода HEX строки (например, "282a36ff") в ImVec4
static ImVec4 HexToImVec4(const std::string& hex_str) {
if (hex_str.length() < 8) return ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
if (hex_str.length() < 8) {
return {1.0f, 1.0f, 1.0f, 1.0f};
}
unsigned int r, g, b, a;
std::stringstream ss;
@@ -28,7 +30,12 @@ static ImVec4 HexToImVec4(const std::string& hex_str) {
ss << std::hex << hex_str.substr(4, 2); ss >> b; ss.clear();
ss << std::hex << hex_str.substr(6, 2); ss >> a;
return ImVec4(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
return {
static_cast<float>(r) / 255.0f,
static_cast<float>(g) / 255.0f,
static_cast<float>(b) / 255.0f,
static_cast<float>(a) / 255.0f
};
}
bool SerialApp::loadTheme(const std::string& filepath) {
@@ -82,7 +89,7 @@ bool SerialApp::loadTheme(const std::string& filepath) {
// Читаем HEX цвета
std::string hex_val;
ss >> hex_val;
if (color_map.find(key) != color_map.end()) {
if (color_map.contains(key)) {
style.Colors[color_map[key]] = HexToImVec4(hex_val);
}
} else {
@@ -110,7 +117,7 @@ void SerialApp::renderUI() {
payloadWindow.render(&showPayloadWindow, connectionWindow);
}
if (showLogWindow) {
logWindow.render(&showLogWindow, connectionWindow);
LogWindow::render(&showLogWindow, connectionWindow);
}
}