Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

platform support for windows and msvc compiler #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*.app

build
out
.vs

# Gtags
GTAGS
Expand Down
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cmake_minimum_required(VERSION 3.13)
project(protocolConverter)
project(modbus)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)

if (DCMAKE_BUILD_TYPE MATCHES Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -lpthread -Wall")
endif()

option(MODBUS_EXAMPLE "Build example program" OFF)
option(MODBUS_TESTS "Build tests" OFF)
option(MODBUS_COMMUNICATION "Use Modbus communication library" ON)
option(MODBUS_TCP_COMMUNICATION "Use Modbus TCP communication library" ON)
option(MODBUS_SERIAL_COMMUNICATION "Use Modbus serial communication library" OFF) # not supported by windows platform

add_subdirectory(src)

Expand All @@ -21,4 +21,3 @@ if(MODBUS_EXAMPLE)
add_executable(ex example/main.cpp)
target_link_libraries(ex Modbus)
endif()

254 changes: 127 additions & 127 deletions include/MB/Serial/connection.hpp
Original file line number Diff line number Diff line change
@@ -1,127 +1,127 @@
// Modbus for c++ <https://github.com/Mazurel/Modbus>
// Copyright (c) 2020 Mateusz Mazur aka Mazurel
// Licensed under: MIT License <http://opensource.org/licenses/MIT>

#pragma once

#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <tuple>

#include <cerrno>
#include <fcntl.h>
#include <poll.h>
#include <termios.h>
#include <unistd.h>

#include "MB/modbusException.hpp"
#include "MB/modbusRequest.hpp"
#include "MB/modbusResponse.hpp"
#include "MB/modbusUtils.hpp"

namespace MB::Serial {
class Connection {
public:
// Pretty high timeout
static const unsigned int DefaultSerialTimeout = 100;

private:
struct termios _termios;
int _fd;

int _timeout = Connection::DefaultSerialTimeout;

public:
constexpr explicit Connection() : _termios(), _fd(-1) {}
explicit Connection(const std::string &path);
explicit Connection(const Connection &) = delete;
explicit Connection(Connection &&) noexcept;
Connection &operator=(Connection &&);
~Connection();

void connect();

std::vector<uint8_t> sendRequest(const MB::ModbusRequest &request);
std::vector<uint8_t> sendResponse(const MB::ModbusResponse &response);
std::vector<uint8_t> sendException(const MB::ModbusException &exception);

/**
* @brief Sends data through the serial
* @param data - Vectorized data
*/
std::vector<uint8_t> send(std::vector<uint8_t> data);

void clearInput();

[[nodiscard]] std::tuple<MB::ModbusResponse, std::vector<uint8_t>> awaitResponse();
[[nodiscard]] std::tuple<MB::ModbusRequest, std::vector<uint8_t>> awaitRequest();

[[nodiscard]] std::vector<uint8_t> awaitRawMessage();

void enableParity(const bool parity) {
if (parity)
getTTY().c_cflag |= PARENB;
else
getTTY().c_cflag &= ~PARENB;
}

void setEvenParity() {
enableParity(true);
getTTY().c_cflag &= ~PARODD;
}

void setOddParity() {
enableParity(true);
getTTY().c_cflag |= PARODD;
}

void setTwoStopBits(const bool two) {
if (two) {
getTTY().c_cflag |= CSTOPB;
} else {
getTTY().c_cflag &= ~CSTOPB;
}
}

#define setBaud(s) \
case s: \
speed = B##s; \
break;
void setBaudRate(speed_t speed) {
switch (speed) {
setBaud(0);
setBaud(50);
setBaud(75);
setBaud(110);
setBaud(134);
setBaud(150);
setBaud(200);
setBaud(300);
setBaud(600);
setBaud(1200);
setBaud(1800);
setBaud(2400);
setBaud(4800);
setBaud(9600);
setBaud(19200);
setBaud(38400);
setBaud(57600);
setBaud(115200);
setBaud(230400);
default:
throw std::runtime_error("Invalid baud rate");
}
cfsetospeed(&_termios, speed);
cfsetispeed(&_termios, speed);
}
#undef setBaud

termios &getTTY() { return _termios; }

int getTimeout() const { return _timeout; }

void setTimeout(int timeout) { _timeout = timeout; }
};
} // namespace MB::Serial
// Modbus for c++ <https://github.com/Mazurel/Modbus>
// Copyright (c) 2020 Mateusz Mazur aka Mazurel
// Licensed under: MIT License <http://opensource.org/licenses/MIT>
#pragma once
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <tuple>
#include <cerrno>
#include <fcntl.h>
#include <poll.h>
#include <termios.h>
#include <unistd.h>
#include "MB/modbusException.hpp"
#include "MB/modbusRequest.hpp"
#include "MB/modbusResponse.hpp"
#include "MB/modbusUtils.hpp"
namespace MB::Serial {
class Connection {
public:
// Pretty high timeout
static const unsigned int DefaultSerialTimeout = 100;
private:
struct termios _termios;
int _fd;
int _timeout = Connection::DefaultSerialTimeout;
public:
constexpr explicit Connection() : _termios(), _fd(-1) {}
explicit Connection(const std::string &path);
explicit Connection(const Connection &) = delete;
explicit Connection(Connection &&) noexcept;
Connection &operator=(Connection &&);
~Connection();
void connect();
std::vector<uint8_t> sendRequest(const MB::ModbusRequest &request);
std::vector<uint8_t> sendResponse(const MB::ModbusResponse &response);
std::vector<uint8_t> sendException(const MB::ModbusException &exception);
/**
* @brief Sends data through the serial
* @param data - Vectorized data
*/
std::vector<uint8_t> send(std::vector<uint8_t> data);
void clearInput();
[[nodiscard]] std::tuple<MB::ModbusResponse, std::vector<uint8_t>> awaitResponse();
[[nodiscard]] std::tuple<MB::ModbusRequest, std::vector<uint8_t>> awaitRequest();
[[nodiscard]] std::vector<uint8_t> awaitRawMessage();
void enableParity(const bool parity) {
if (parity)
getTTY().c_cflag |= PARENB;
else
getTTY().c_cflag &= ~PARENB;
}
void setEvenParity() {
enableParity(true);
getTTY().c_cflag &= ~PARODD;
}
void setOddParity() {
enableParity(true);
getTTY().c_cflag |= PARODD;
}
void setTwoStopBits(const bool two) {
if (two) {
getTTY().c_cflag |= CSTOPB;
} else {
getTTY().c_cflag &= ~CSTOPB;
}
}
#define setBaud(s) \
case s: \
speed = B##s; \
break;
void setBaudRate(speed_t speed) {
switch (speed) {
setBaud(0);
setBaud(50);
setBaud(75);
setBaud(110);
setBaud(134);
setBaud(150);
setBaud(200);
setBaud(300);
setBaud(600);
setBaud(1200);
setBaud(1800);
setBaud(2400);
setBaud(4800);
setBaud(9600);
setBaud(19200);
setBaud(38400);
setBaud(57600);
setBaud(115200);
setBaud(230400);
default:
throw std::runtime_error("Invalid baud rate");
}
cfsetospeed(&_termios, speed);
cfsetispeed(&_termios, speed);
}
#undef setBaud
termios &getTTY() { return _termios; }
int getTimeout() const { return _timeout; }
void setTimeout(int timeout) { _timeout = timeout; }
};
} // namespace MB::Serial
38 changes: 12 additions & 26 deletions include/MB/TCP/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

#pragma once

#include <memory>
#include <type_traits>
#include <string>
#include <vector>

#include <cerrno>
#include <libnet.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
#include "../modbusException.hpp"
#include "../modbusRequest.hpp"
#include "../modbusResponse.hpp"

#include "MB/modbusException.hpp"
#include "MB/modbusRequest.hpp"
#include "MB/modbusResponse.hpp"
namespace MB {
namespace TCP {

namespace MB::TCP {
class Connection {
public:
static const unsigned int DefaultTCPTimeout = 500;
Expand All @@ -27,28 +23,18 @@ class Connection {
uint16_t _messageID = 0;
int _timeout = Connection::DefaultTCPTimeout;

void closeSockfd(void);

public:
explicit Connection() noexcept : _sockfd(-1), _messageID(0){};
explicit Connection(int sockfd) noexcept;
Connection(const Connection &copy) = delete;
Connection(Connection &&moved) noexcept;
Connection &operator=(Connection &&other) noexcept {
if (this == &other)
return *this;

if (_sockfd != -1 && _sockfd != other._sockfd)
::close(_sockfd);

_sockfd = other._sockfd;
_messageID = other._messageID;
other._sockfd = -1;

return *this;
}
Connection& operator=(Connection&& other) noexcept;

[[nodiscard]] int getSockfd() const { return _sockfd; }

static Connection with(std::string addr, int port);
static Connection with(const std::string &addr, int port);

~Connection();

Expand All @@ -65,4 +51,4 @@ class Connection {

void setMessageId(uint16_t messageId) { _messageID = messageId; }
};
} // namespace MB::TCP
}} // namespace MB::TCP
Loading