# Communicator **Repository Path**: sunwenqi0713/communicator ## Basic Information - **Project Name**: Communicator - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-15 - **Last Updated**: 2026-04-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Communicator C++ 通信工厂库,支持串口和网口通信,预留编解码自定义实现接口。 ## 特性 - **串口通信**:支持配置波特率、数据位、停止位、校验位 - **网口通信**:支持 TCP Server、TCP Client、UDP - **编解码接口**:通过 `ICodec` 接口自定义数据编解码(如 HEX、CRC、ProtoBuf 等) - **跨平台**:支持 Windows 和 Linux - **线程安全**:各通信类内部使用 mutex 保护 ## 项目结构 ``` Communicator/ ├── include/communicator/ │ ├── communicator.h # 核心接口定义 │ ├── serial.h # 串口通信类 │ ├── tcp_server.h # TCP服务器类 │ ├── tcp_client.h # TCP客户端类 │ └── udp.h # UDP通信类 ├── src/ │ ├── factory.cpp # 工厂实现 │ ├── serial.cpp # 串口实现 │ └── socket/ │ ├── tcp_server.cpp │ ├── tcp_client.cpp │ └── udp.cpp ├── examples/ │ └── example.cpp # 使用示例 ├── tests/ # 单元测试 ├── CMakeLists.txt └── README.md ``` ## 快速开始 ### 编译 ```bash mkdir build && cd build cmake .. make -j4 ``` ### 运行示例 ```bash # TCP Server ./example tcp_server # TCP Client ./example tcp_client # UDP ./example udp # Serial (需要指定端口) ./example serial ``` ## 使用示例 ### 1. 基本用法 ```cpp #include "communicator/communicator.h" using namespace communicator; // 串口配置 Config config; config.type = CommunicatorType::Serial; config.serial.port = "/dev/ttyUSB0"; config.serial.baudrate = 115200; // 创建通信实例 auto comm = CommunicatorFactory::create(config); // 打开并通信 comm->open(); uint8_t data[] = "Hello"; comm->send(data, sizeof(data) - 1); uint8_t buffer[256]; int received = comm->receive(buffer, sizeof(buffer), 1000); comm->close(); ``` ### 2. TCP 通信 ```cpp // TCP Server Config server_config; server_config.type = CommunicatorType::TcpServer; server_config.network.local_port = 8888; auto server = CommunicatorFactory::create(server_config); server->open(); server->accept(); // TCP Client Config client_config; client_config.type = CommunicatorType::TcpClient; client_config.network.remote_ip = "127.0.0.1"; client_config.network.remote_port = 8888; auto client = CommunicatorFactory::create(client_config); client->open(); ``` ### 3. UDP 通信 ```cpp Config config; config.type = CommunicatorType::Udp; config.network.local_port = 9999; config.network.remote_ip = "127.0.0.1"; config.network.remote_port = 8888; auto udp = CommunicatorFactory::create(config); udp->open(); ``` ### 4. 自定义编解码器 ```cpp // 实现 ICodec 接口 class MyCodec : public ICodec { public: std::vector encode(const void* data, size_t size) override { // 添加帧头、校验等 std::vector result; result.push_back(0xAA); // 帧头 const uint8_t* bytes = static_cast(data); result.insert(result.end(), bytes, bytes + size); result.push_back(calculateChecksum(bytes, size)); // 校验和 return result; } bool decode(const uint8_t* data, size_t size, std::vector& out) override { if (size < 3 || data[0] != 0xAA) return false; uint8_t expected_crc = calculateChecksum(data + 1, size - 2); if (data[size - 1] == expected_crc) { out.assign(data + 1, data + size - 1); return true; } return false; } }; // 使用自定义编解码器 comm->setCodec(std::make_shared()); ``` ## API 参考 ### Config 结构 ```cpp struct Config { CommunicatorType type; // Serial/TcpServer/TcpClient/Udp SerialConfig serial; // 串口配置 NetworkConfig network; // 网络配置 }; struct SerialConfig { std::string port; // 串口路径 int baudrate = 9600; // 波特率 int databits = 8; // 数据位 int stopbits = 1; // 停止位 char parity = 'N'; // 校验位 N/E/O }; struct NetworkConfig { std::string local_ip; uint16_t local_port; std::string remote_ip; uint16_t remote_port; bool non_blocking = true; }; ``` ### ICommunicator 接口 ```cpp class ICommunicator { virtual ~ICommunicator() = default; virtual bool open() = 0; // 打开连接 virtual void close() = 0; // 关闭连接 virtual bool isOpen() const = 0; // 是否已打开 virtual int send(const uint8_t* data, size_t size) = 0; // 发送数据 virtual int receive(uint8_t* buffer, size_t max_size, int timeout_ms = 1000) = 0; // 接收数据 virtual void setCodec(std::shared_ptr codec); // 设置编解码器 virtual std::shared_ptr getCodec() const; // 获取编解码器 }; ``` ### ICodec 接口 ```cpp class ICodec { public: virtual ~ICodec() = default; virtual std::vector encode(const void* data, size_t size) = 0; virtual bool decode(const uint8_t* data, size_t size, std::vector& out) = 0; }; ``` ## 单元测试 ```bash cd build cmake .. -DBUILD_TESTS=ON make -j4 ctest --output-on-failure ``` 或直接运行测试可执行文件: ```bash ./tests/test_communicator ``` ## 依赖 - C++17 编译器 - CMake 3.10+ - Linux: pthread - Windows: Winsock2 (自动链接) ## 许可证 MIT License