MyLib
This commit is contained in:
47
Test/LogTest.cpp
Normal file
47
Test/LogTest.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "Test.h"
|
||||
|
||||
#include "ConfigParser.h"
|
||||
#include "LogL.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Log_Test() {
|
||||
unordered_map<string, string> config;
|
||||
configFromIni("ini\\config.ini", config);
|
||||
//日志类型
|
||||
LogType tmpLogType = LTStd;
|
||||
auto tmp = config.find("logType");
|
||||
if (tmp != config.end()) {
|
||||
if (tmp->second == "udp") {
|
||||
tmpLogType = LTUdp;
|
||||
}
|
||||
else if (tmp->second == "txt") {
|
||||
tmpLogType = LTTxt;
|
||||
}
|
||||
else if (tmp->second == "std") {
|
||||
tmpLogType = LTStd;
|
||||
}
|
||||
}
|
||||
//日志地址
|
||||
string tmpLogPath;
|
||||
tmp = config.find("logPath");
|
||||
if (tmp != config.end()) {
|
||||
tmpLogPath = tmp->second;
|
||||
}
|
||||
//日志状态
|
||||
bool tmpLogStatus = true;
|
||||
tmp = config.find("logStatus");
|
||||
if (tmp != config.end()) {
|
||||
if (tmp->second == "sync") {
|
||||
tmpLogStatus = true;
|
||||
}
|
||||
else {
|
||||
tmpLogStatus = false;
|
||||
}
|
||||
}
|
||||
LogL logL(tmpLogType, tmpLogStatus, tmpLogPath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
19
Test/ParserTest.cpp
Normal file
19
Test/ParserTest.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "Test.h"
|
||||
#include "ConfigParser.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Parser_Test() {
|
||||
std::unordered_map<string, string> configList;
|
||||
auto status = configFromIni("ini/config.ini", configList);
|
||||
if (status == CPSUCCESS) {
|
||||
for (auto &item: configList) {
|
||||
cout << item.first << "\r" << item.second << endl;
|
||||
}
|
||||
}
|
||||
cout << "test:" << configList["test"] << endl;
|
||||
cout << "test:" << configList["test"] << endl;
|
||||
cout << "test:" << configList["test"] << endl;
|
||||
|
||||
}
|
59
Test/SerialTest.cpp
Normal file
59
Test/SerialTest.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include "Test.h"
|
||||
#include "SerialPort.h"
|
||||
|
||||
void Serial_Test() {
|
||||
|
||||
try {
|
||||
// 创建两个串口实例(模拟双向通信)
|
||||
SerialPort sender("/dev/pts/2", 115200);
|
||||
SerialPort receiver("/dev/pts/3", 115200);
|
||||
|
||||
// 设置接收回调
|
||||
auto callback = [](const std::vector<uint8_t> &data) {
|
||||
std::cout << "Received: ";
|
||||
for (auto c: data) {
|
||||
std::cout << static_cast<char>(c);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
};
|
||||
|
||||
// 启动接收线程
|
||||
receiver.start_receiving(callback);
|
||||
|
||||
// 发送测试数据
|
||||
std::cout << "Starting serial port test...\n";
|
||||
|
||||
// 测试1: 发送字符串
|
||||
std::string testStr = "Hello Serial Port!\n";
|
||||
sender.write(testStr);
|
||||
std::cout << "Sent: " << testStr;
|
||||
|
||||
// 测试2: 发送二进制数据
|
||||
std::vector<uint8_t> binaryData = {0x41, 0x42, 0x43, 0x44, 0x45}; // ABCDE
|
||||
sender.write(binaryData);
|
||||
std::cout << "Sent binary data: ABCDE\n";
|
||||
|
||||
// 测试3: 发送大量数据
|
||||
std::vector<uint8_t> largeData(1024);
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
largeData[i] = i % 256;
|
||||
}
|
||||
sender.write(largeData);
|
||||
std::cout << "Sent 1024 bytes of data\n";
|
||||
|
||||
// 等待用户输入结束测试
|
||||
std::cout << "Press Enter to exit...";
|
||||
std::cin.get();
|
||||
|
||||
// 清理
|
||||
receiver.stop_receiving();
|
||||
sender.close();
|
||||
receiver.close();
|
||||
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
55
Test/TcpTest.cpp
Normal file
55
Test/TcpTest.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include "Test.h"
|
||||
|
||||
#include "TCP.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Tcp_Test() {
|
||||
// AsyncTCPClient client;
|
||||
// client.connect("127.0.0.1", 9999);
|
||||
// client.start_async();
|
||||
// while (client.is_connected()) {
|
||||
// std::vector<uint8_t> str;
|
||||
// if (client.try_pop(str)) {
|
||||
// cout << string(str.begin(), str.end()) << endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
try {
|
||||
TCPClient client;
|
||||
client.connect("192.168.80.1", 9999);
|
||||
while (client.is_connected()) {
|
||||
vector<uint8_t> buffer(1024);
|
||||
auto size = client.receive(buffer.data(), buffer.size());
|
||||
string info;
|
||||
if (size > 0) {
|
||||
info = std::string(buffer.begin(), buffer.begin() + static_cast<int>(size));
|
||||
cout << info << endl;
|
||||
}
|
||||
if (info.find("stop") != string::npos) {
|
||||
client.close_socket();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
|
||||
|
||||
TCPServer tcpServer;
|
||||
tcpServer.listen(9998);
|
||||
auto client = tcpServer.accept();
|
||||
while (client->is_connected()) {
|
||||
client->send("hello");
|
||||
vector<uint8_t> buffer(1024);
|
||||
auto size = client->receive(buffer.data(), buffer.size());
|
||||
string info;
|
||||
if (size > 0) {
|
||||
info = std::string(buffer.begin(), buffer.begin() + static_cast<int>(size));
|
||||
cout << info << endl;
|
||||
}
|
||||
if (info.find("stop") != string::npos) {
|
||||
client->close_socket();
|
||||
}
|
||||
}
|
||||
}
|
16
Test/Test.h
Normal file
16
Test/Test.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef LXZL_TEST_H
|
||||
#define LXZL_TEST_H
|
||||
#include <iostream>
|
||||
|
||||
|
||||
void Serial_Test();
|
||||
|
||||
void Log_Test();
|
||||
|
||||
void Parser_Test();
|
||||
|
||||
void Tcp_Test();
|
||||
|
||||
void Udp_Test();
|
||||
|
||||
#endif //LXZL_TEST_H
|
29
Test/UdpTest.cpp
Normal file
29
Test/UdpTest.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "Test.h"
|
||||
#include "UDP.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Udp_Test() {
|
||||
//UDP
|
||||
uint16_t port = 8900;
|
||||
UDPReceiver udpReceiver(port);
|
||||
udpReceiver.start_receiving([](const std::vector<uint8_t> &date, sockaddr_in sockAdd, socklen_t socklen){
|
||||
string da(date.begin(), date.end());
|
||||
cout << inet_ntoa(sockAdd.sin_addr) << ":" << ntohs(sockAdd.sin_port) << endl;
|
||||
cout << da << endl;
|
||||
});
|
||||
UDPSender udpSenderA(udpReceiver.sockfd_);
|
||||
udpSenderA.set_destination("127.0.0.1", 8200);
|
||||
UDPSender udpSenderB(udpReceiver.sockfd_);
|
||||
udpSenderB.set_destination("127.0.0.1", 8201);
|
||||
UDPSender udpSenderC(udpReceiver.sockfd_);
|
||||
udpSenderC.set_destination("127.0.0.1", 8200);
|
||||
|
||||
|
||||
while (true) {
|
||||
udpSenderA.send("helloA");
|
||||
udpSenderB.send("B");
|
||||
udpSenderC.send("C");
|
||||
_sleep(5000);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user