Skip to content

Commit

Permalink
Merge pull request #2 from janekbaraniewski/init
Browse files Browse the repository at this point in the history
Init2
  • Loading branch information
janekbaraniewski authored Apr 28, 2024
2 parents 951c769 + b57e4e5 commit 7c85d20
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 78 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,68 @@
# ser2net2ser

> This still needs testing
## Requirements

- Linux or macOS operating system
- Boost libraries (system, program_options, log_setup, log, date_time)
- CMake for building the project
- Docker (optional) for containerization

## Building the Project

Clone the repository and use CMake to build the project:

```bash
git clone https://github.com/janekbaraniewski/ser2net2ser.git
cd ser2net2ser
make build
```

This will compile both the server and client applications.

## Running the Server

The server needs to be connected to a serial device. It can be started with the following command:

```bash
./build/serial_server \
--device /dev/ttyUSB0 \
--baud 9600 \
--port 12345
```

```text
--device: Specifies the serial device.
--baud: Sets the baud rate for the serial device.
--port: TCP port on which the server will listen for incoming connections.
```

![server](docs/server.png)

## Running the Client

The client should be run on the machine where you want the virtual serial port to be created:

```bash
sudo ./build/serial_client \
--server 192.168.1.100 \
--port 12345 \
--vsp "tty.usbserial-666"
```

```text
--server: IP address of the server.
--port: TCP port on which the server is running.
--vsp: Name of the virtual serial port to be created.
```

![client](docs/client.png)

## Docker Containers

Dockerfiles for both the server and client are included. Build and run the containers using:

```bash
make build-images
```
Binary file added docs/client.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion include/VirtualSerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

#include <string>
#include <functional>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdexcept>
#include <iostream>
#include <sys/stat.h>

class VirtualSerialPort {
public:
VirtualSerialPort(const std::string& device);
~VirtualSerialPort();

void open();
void close();
bool write(const std::string& data);
std::string read();
Expand Down
44 changes: 44 additions & 0 deletions include/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef CLIENT_H
#define CLIENT_H

#include <iostream>
#include <array>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

#include <boost/asio.hpp>
#include <boost/program_options.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>

#include "logging.h"
#include "VirtualSerialPort.h"

using namespace boost::asio;
using ip::tcp;
using std::string;

class SerialClient {
public:
SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name);
void run();

private:
io_service io_service_;
tcp::socket socket_;
std::array<char, 256> buffer_;
VirtualSerialPort vsp_;

void do_read_write();
};


#endif // CLIENT_H
9 changes: 8 additions & 1 deletion include/logging.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#ifndef LOGGING_H
#define LOGGING_H

#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp> // Ensure this is included for date-time support

// Namespace aliases
namespace logging = boost::log;
Expand Down
23 changes: 23 additions & 0 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
#include <string>
#include "ISerialPort.h"

#include <iostream>
#include <string>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/program_options.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>

#include "logging.h"
#include "ISerialPort.h"
#include "RealSerialPort.h"

using namespace boost::asio;
using ip::tcp;
using std::string;
Expand Down
6 changes: 0 additions & 6 deletions src/VirtualSerialPort.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#include "VirtualSerialPort.h"
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdexcept>
#include <iostream>
#include <sys/stat.h>

VirtualSerialPort::VirtualSerialPort(const std::string& device) : device_name_("/dev/" + device) {
master_fd_ = posix_openpt(O_RDWR | O_NOCTTY);
Expand Down
89 changes: 30 additions & 59 deletions src/client.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
#include <iostream>
#include <array>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

#include <boost/asio.hpp>
#include <boost/program_options.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>

#include "logging.h"
#include "VirtualSerialPort.h"
#include "client.h"

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

class SerialClient {
private:
io_service io_service_;
tcp::socket socket_;
std::array<char, 256> buffer_;
VirtualSerialPort vsp_;

public:
SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name)
: socket_(io_service_), vsp_(vsp_name) {
BOOST_LOG_TRIVIAL(info) << "Initializing client...";
tcp::resolver resolver(io_service_);
auto endpoint_iterator = resolver.resolve({server_ip, std::to_string(server_port)});
BOOST_LOG_TRIVIAL(info) << "Connecting to server at " << server_ip << ":" << server_port;
connect(socket_, endpoint_iterator);
BOOST_LOG_TRIVIAL(info) << "Connected to server.";
BOOST_LOG_TRIVIAL(info) << "Opening virtual serial port: " << vsp_name;
}
SerialClient::SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name)
: socket_(io_service_), vsp_(vsp_name) {
BOOST_LOG_TRIVIAL(info) << "Initializing client...";
tcp::resolver resolver(io_service_);
auto endpoint_iterator = resolver.resolve({server_ip, std::to_string(server_port)});
BOOST_LOG_TRIVIAL(info) << "Connecting to server at " << server_ip << ":" << server_port;
connect(socket_, endpoint_iterator);
BOOST_LOG_TRIVIAL(info) << "Connected to server.";
BOOST_LOG_TRIVIAL(info) << "Opening virtual serial port: " << vsp_name;
}

void run() {
BOOST_LOG_TRIVIAL(info) << "Starting client I/O operations.";
do_read_write();
io_service_.run();
}
void SerialClient::run() {
BOOST_LOG_TRIVIAL(info) << "Starting client I/O operations.";
do_read_write();
io_service_.run();
}

private:
void do_read_write() {
socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) {
if (!ec) {
string data(buffer_.data(), length);
BOOST_LOG_TRIVIAL(info) << "Received data: " << data;
if (vsp_.write(data)) {
BOOST_LOG_TRIVIAL(info) << "Data written to virtual serial port.";
} else {
BOOST_LOG_TRIVIAL(error) << "Failed to write to virtual serial port.";
}
do_read_write();
void SerialClient::do_read_write() {
socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) {
if (!ec) {
string data(buffer_.data(), length);
BOOST_LOG_TRIVIAL(info) << "Received data: " << data;
if (vsp_.write(data)) {
BOOST_LOG_TRIVIAL(info) << "Data written to virtual serial port.";
} else {
BOOST_LOG_TRIVIAL(error) << "Read error: " << ec.message();
BOOST_LOG_TRIVIAL(error) << "Failed to write to virtual serial port.";
}
});
}
};
do_read_write();
} else {
BOOST_LOG_TRIVIAL(error) << "Read error: " << ec.message();
}
});
}

int main(int argc, char* argv[]) {
init_logging();
Expand Down
11 changes: 0 additions & 11 deletions src/logging.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
#include "logging.h"

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp> // Ensure this is included for date-time support


// Define the global logger
src::severity_logger<boost::log::trivial::severity_level> lg;

Expand Down

0 comments on commit 7c85d20

Please sign in to comment.