MyLib
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user