Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/Serial/boost_serial_console.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,71 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/serial_port.hpp>
#include <string>
#include <iostream>
#include <thread>

int main(int argc, char* argv[])
{
std::string port_name = "/dev/ttyUSB1";

boost::asio::io_context io_context;
boost::asio::serial_port serial_port(io_context, port_name);
boost::asio::serial_port serial(io_context, port_name);


serial.set_option(boost::asio::serial_port::baud_rate(115200));
serial.set_option(boost::asio::serial_port::character_size(8));
serial.set_option(boost::asio::serial_port::stop_bits(
boost::asio::serial_port::stop_bits::one));
serial.set_option(boost::asio::serial_port::parity(
boost::asio::serial_port::parity::none));
serial.set_option(boost::asio::serial_port::flow_control(
boost::asio::serial_port::flow_control::none));

if (!serial.is_open()) return 1;

//实现了如下功能:
//接收用户输入指令,发送至backend
//接收backend的回复,打印到控制台
//用户输入exit时,退出程序

// std::string str = "help";
// std::string str = "encrypt BUPT-RobotTe-2023212641";
// std::string str = "decrypt JqE4hHAU2miDNoPqks9N0WviV0w1TduFq8fCxo/OFKr629TWTE5Ezzar2hDBrdc3JNCO3nVzBXtG11O0O/bSyQThVgQo3LKZB69KW2rRWoJYweT2ewoWgjzVhar/9Jfv3Il3BJciOuAiw3IKhT7SHsoaAEPGWZBxdGnO3IgfFK8i97w97CC7IJ7MNDMkDmfEJfYWXVPT/1jf7+jQSUQqqyGhYAqp6ydESrnfdJYvp7BxEPxhEyEKkSZbLqvdnu1DtnobI0h7+JgAFBcw9zu8fUqZeJpQvJx5F0K6gFo8jYT1wRHnA+0rPfhbZQgawhUKs9NcdNwehNEEvoEsxmKZEg==";

std::array<char, 10240> buffer;
boost::system::error_code ec;
std::string user_input;

while (true)
{
std::cout << "Enter command: ";
std::getline(std::cin, user_input);

if (user_input == "exit")
{
break;
}

serial.write_some(boost::asio::buffer(user_input), ec);
if (ec)
{
std::cerr << "Error when writing to " << port_name << std::endl;
}
else
{
std::cout << "Writing " << user_input << " to " << port_name
<< std::endl;
}
buffer.fill(0);
auto size = serial.read_some(boost::asio::buffer(buffer), ec);
if (ec)
{
std::cerr << "Error when reading from " << port_name << std::endl;
}
else
{
std::string response(buffer.data(), size);
std::cout << "Received: " << response << std::endl;
}
}
}
18 changes: 12 additions & 6 deletions src/Socket/boost_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ int main()
boost::asio::ip::tcp::socket socket(io_context);
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::connect(socket, resolver.resolve("localhost", "12345"));

while (true) {
std::string message;
std::cout << "Enter message: ";
std::getline(std::cin, message);
if (message.empty()) break; // Exit loop if input is empty

std::string message = "time";
socket.write_some(boost::asio::buffer(message));
std::string response;
response.resize(1024);
socket.read_some(boost::asio::buffer(response));
std::cout << "Response: " << response << std::endl;
socket.write_some(boost::asio::buffer(message));
std::string response;
response.resize(1024);
size_t len = socket.read_some(boost::asio::buffer(response));
std::cout << "Response: " << response.substr(0, len) << std::endl;
}

return 0;
}
30 changes: 24 additions & 6 deletions src/Socket/boost_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,36 @@ class Session : public std::enable_shared_from_this<Session>
// Modify Code here
read_buffer[length] = '\0';
std::cout << "Received: " << read_buffer << std::endl;
std::string response;
if (strcmp(read_buffer, "time") == 0)
{
std::string time = get_current_time();
std::copy(time.begin(), time.end(), write_buffer);
do_write(time.size());
response = get_current_time();
}
else if (strncmp(read_buffer, "echo ", 5) == 0)
{
response = std::string(read_buffer + 5);
}
else if (strcmp(read_buffer, "exit") == 0)
{
close();
return;
}
else if (strcmp(read_buffer, "help") == 0)
{
response = "\n------------------------------------------------\n"
"Commands:\n"
"time \t- get current time\n"
"echo <your string> \t- echo back your string\n"
"exit \t- close the connection\nn"
"help \t- show this help message\n"
"------------------------------------------------";
}
else
{
std::copy(read_buffer, read_buffer + length,
write_buffer);
do_write(length);
response = "Unknown command. Type 'help' for a list of commands.";
}
std::copy(response.begin(), response.end(), write_buffer);
do_write(response.size());
}
});
}
Expand Down