Skip to content

Commit 7c85d20

Browse files
Merge pull request #2 from janekbaraniewski/init
Init2
2 parents 951c769 + b57e4e5 commit 7c85d20

File tree

10 files changed

+178
-78
lines changed

10 files changed

+178
-78
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
11
# ser2net2ser
2+
3+
> This still needs testing
4+
5+
## Requirements
6+
7+
- Linux or macOS operating system
8+
- Boost libraries (system, program_options, log_setup, log, date_time)
9+
- CMake for building the project
10+
- Docker (optional) for containerization
11+
12+
## Building the Project
13+
14+
Clone the repository and use CMake to build the project:
15+
16+
```bash
17+
git clone https://github.com/janekbaraniewski/ser2net2ser.git
18+
cd ser2net2ser
19+
make build
20+
```
21+
22+
This will compile both the server and client applications.
23+
24+
## Running the Server
25+
26+
The server needs to be connected to a serial device. It can be started with the following command:
27+
28+
```bash
29+
./build/serial_server \
30+
--device /dev/ttyUSB0 \
31+
--baud 9600 \
32+
--port 12345
33+
```
34+
35+
```text
36+
--device: Specifies the serial device.
37+
--baud: Sets the baud rate for the serial device.
38+
--port: TCP port on which the server will listen for incoming connections.
39+
```
40+
41+
![server](docs/server.png)
42+
43+
## Running the Client
44+
45+
The client should be run on the machine where you want the virtual serial port to be created:
46+
47+
```bash
48+
sudo ./build/serial_client \
49+
--server 192.168.1.100 \
50+
--port 12345 \
51+
--vsp "tty.usbserial-666"
52+
```
53+
54+
```text
55+
--server: IP address of the server.
56+
--port: TCP port on which the server is running.
57+
--vsp: Name of the virtual serial port to be created.
58+
```
59+
60+
![client](docs/client.png)
61+
62+
## Docker Containers
63+
64+
Dockerfiles for both the server and client are included. Build and run the containers using:
65+
66+
```bash
67+
make build-images
68+
```

docs/client.png

323 KB
Loading

docs/server.png

181 KB
Loading

include/VirtualSerialPort.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33

44
#include <string>
55
#include <functional>
6+
#include <fcntl.h>
7+
#include <unistd.h>
8+
#include <termios.h>
9+
#include <stdexcept>
10+
#include <iostream>
11+
#include <sys/stat.h>
612

713
class VirtualSerialPort {
814
public:
915
VirtualSerialPort(const std::string& device);
1016
~VirtualSerialPort();
1117

12-
void open();
1318
void close();
1419
bool write(const std::string& data);
1520
std::string read();

include/client.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef CLIENT_H
2+
#define CLIENT_H
3+
4+
#include <iostream>
5+
#include <array>
6+
#include <fcntl.h>
7+
#include <unistd.h>
8+
#include <termios.h>
9+
10+
#include <boost/asio.hpp>
11+
#include <boost/program_options.hpp>
12+
#include <boost/log/core.hpp>
13+
#include <boost/log/trivial.hpp>
14+
#include <boost/log/expressions.hpp>
15+
#include <boost/log/utility/setup/file.hpp>
16+
#include <boost/log/utility/setup/console.hpp>
17+
#include <boost/log/utility/setup/common_attributes.hpp>
18+
#include <boost/log/sources/severity_logger.hpp>
19+
#include <boost/log/sources/record_ostream.hpp>
20+
#include <boost/log/support/date_time.hpp>
21+
22+
#include "logging.h"
23+
#include "VirtualSerialPort.h"
24+
25+
using namespace boost::asio;
26+
using ip::tcp;
27+
using std::string;
28+
29+
class SerialClient {
30+
public:
31+
SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name);
32+
void run();
33+
34+
private:
35+
io_service io_service_;
36+
tcp::socket socket_;
37+
std::array<char, 256> buffer_;
38+
VirtualSerialPort vsp_;
39+
40+
void do_read_write();
41+
};
42+
43+
44+
#endif // CLIENT_H

include/logging.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#ifndef LOGGING_H
22
#define LOGGING_H
33

4-
#include <boost/log/sources/severity_logger.hpp>
4+
#include <boost/log/core.hpp>
55
#include <boost/log/trivial.hpp>
6+
#include <boost/log/expressions.hpp>
7+
#include <boost/log/utility/setup/file.hpp>
8+
#include <boost/log/utility/setup/console.hpp>
9+
#include <boost/log/utility/setup/common_attributes.hpp>
10+
#include <boost/log/sources/severity_logger.hpp>
11+
#include <boost/log/sources/record_ostream.hpp>
12+
#include <boost/log/support/date_time.hpp> // Ensure this is included for date-time support
613

714
// Namespace aliases
815
namespace logging = boost::log;

include/server.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
#include <string>
66
#include "ISerialPort.h"
77

8+
#include <iostream>
9+
#include <string>
10+
#include <fcntl.h>
11+
#include <unistd.h>
12+
#include <termios.h>
13+
14+
#include <boost/asio.hpp>
15+
#include <boost/array.hpp>
16+
#include <boost/program_options.hpp>
17+
#include <boost/log/core.hpp>
18+
#include <boost/log/trivial.hpp>
19+
#include <boost/log/expressions.hpp>
20+
#include <boost/log/utility/setup/file.hpp>
21+
#include <boost/log/utility/setup/console.hpp>
22+
#include <boost/log/utility/setup/common_attributes.hpp>
23+
#include <boost/log/sources/severity_logger.hpp>
24+
#include <boost/log/sources/record_ostream.hpp>
25+
#include <boost/log/support/date_time.hpp>
26+
27+
#include "logging.h"
28+
#include "ISerialPort.h"
29+
#include "RealSerialPort.h"
30+
831
using namespace boost::asio;
932
using ip::tcp;
1033
using std::string;

src/VirtualSerialPort.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#include "VirtualSerialPort.h"
2-
#include <fcntl.h>
3-
#include <unistd.h>
4-
#include <termios.h>
5-
#include <stdexcept>
6-
#include <iostream>
7-
#include <sys/stat.h>
82

93
VirtualSerialPort::VirtualSerialPort(const std::string& device) : device_name_("/dev/" + device) {
104
master_fd_ = posix_openpt(O_RDWR | O_NOCTTY);

src/client.cpp

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
#include <iostream>
2-
#include <array>
3-
#include <fcntl.h>
4-
#include <unistd.h>
5-
#include <termios.h>
6-
7-
#include <boost/asio.hpp>
8-
#include <boost/program_options.hpp>
9-
#include <boost/log/core.hpp>
10-
#include <boost/log/trivial.hpp>
11-
#include <boost/log/expressions.hpp>
12-
#include <boost/log/utility/setup/file.hpp>
13-
#include <boost/log/utility/setup/console.hpp>
14-
#include <boost/log/utility/setup/common_attributes.hpp>
15-
#include <boost/log/sources/severity_logger.hpp>
16-
#include <boost/log/sources/record_ostream.hpp>
17-
#include <boost/log/support/date_time.hpp>
18-
19-
#include "logging.h"
20-
#include "VirtualSerialPort.h"
1+
#include "client.h"
212

223
using namespace boost::asio;
234
using namespace boost::program_options;
@@ -43,49 +24,39 @@ void init_logging() {
4324
boost::log::register_simple_formatter_factory<boost::log::trivial::severity_level, char>("Severity");
4425
}
4526

46-
class SerialClient {
47-
private:
48-
io_service io_service_;
49-
tcp::socket socket_;
50-
std::array<char, 256> buffer_;
51-
VirtualSerialPort vsp_;
52-
53-
public:
54-
SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name)
55-
: socket_(io_service_), vsp_(vsp_name) {
56-
BOOST_LOG_TRIVIAL(info) << "Initializing client...";
57-
tcp::resolver resolver(io_service_);
58-
auto endpoint_iterator = resolver.resolve({server_ip, std::to_string(server_port)});
59-
BOOST_LOG_TRIVIAL(info) << "Connecting to server at " << server_ip << ":" << server_port;
60-
connect(socket_, endpoint_iterator);
61-
BOOST_LOG_TRIVIAL(info) << "Connected to server.";
62-
BOOST_LOG_TRIVIAL(info) << "Opening virtual serial port: " << vsp_name;
63-
}
27+
SerialClient::SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name)
28+
: socket_(io_service_), vsp_(vsp_name) {
29+
BOOST_LOG_TRIVIAL(info) << "Initializing client...";
30+
tcp::resolver resolver(io_service_);
31+
auto endpoint_iterator = resolver.resolve({server_ip, std::to_string(server_port)});
32+
BOOST_LOG_TRIVIAL(info) << "Connecting to server at " << server_ip << ":" << server_port;
33+
connect(socket_, endpoint_iterator);
34+
BOOST_LOG_TRIVIAL(info) << "Connected to server.";
35+
BOOST_LOG_TRIVIAL(info) << "Opening virtual serial port: " << vsp_name;
36+
}
6437

65-
void run() {
66-
BOOST_LOG_TRIVIAL(info) << "Starting client I/O operations.";
67-
do_read_write();
68-
io_service_.run();
69-
}
38+
void SerialClient::run() {
39+
BOOST_LOG_TRIVIAL(info) << "Starting client I/O operations.";
40+
do_read_write();
41+
io_service_.run();
42+
}
7043

71-
private:
72-
void do_read_write() {
73-
socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) {
74-
if (!ec) {
75-
string data(buffer_.data(), length);
76-
BOOST_LOG_TRIVIAL(info) << "Received data: " << data;
77-
if (vsp_.write(data)) {
78-
BOOST_LOG_TRIVIAL(info) << "Data written to virtual serial port.";
79-
} else {
80-
BOOST_LOG_TRIVIAL(error) << "Failed to write to virtual serial port.";
81-
}
82-
do_read_write();
44+
void SerialClient::do_read_write() {
45+
socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) {
46+
if (!ec) {
47+
string data(buffer_.data(), length);
48+
BOOST_LOG_TRIVIAL(info) << "Received data: " << data;
49+
if (vsp_.write(data)) {
50+
BOOST_LOG_TRIVIAL(info) << "Data written to virtual serial port.";
8351
} else {
84-
BOOST_LOG_TRIVIAL(error) << "Read error: " << ec.message();
52+
BOOST_LOG_TRIVIAL(error) << "Failed to write to virtual serial port.";
8553
}
86-
});
87-
}
88-
};
54+
do_read_write();
55+
} else {
56+
BOOST_LOG_TRIVIAL(error) << "Read error: " << ec.message();
57+
}
58+
});
59+
}
8960

9061
int main(int argc, char* argv[]) {
9162
init_logging();

src/logging.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
#include "logging.h"
22

3-
#include <boost/log/core.hpp>
4-
#include <boost/log/trivial.hpp>
5-
#include <boost/log/expressions.hpp>
6-
#include <boost/log/utility/setup/file.hpp>
7-
#include <boost/log/utility/setup/console.hpp>
8-
#include <boost/log/utility/setup/common_attributes.hpp>
9-
#include <boost/log/sources/severity_logger.hpp>
10-
#include <boost/log/sources/record_ostream.hpp>
11-
#include <boost/log/support/date_time.hpp> // Ensure this is included for date-time support
12-
13-
143
// Define the global logger
154
src::severity_logger<boost::log::trivial::severity_level> lg;
165

0 commit comments

Comments
 (0)