Skip to content

Commit 40cbe20

Browse files
Merge pull request #8 from NikolasK-source/development
Development
2 parents afe8734 + d0073d8 commit 40cbe20

9 files changed

+189
-39
lines changed

.github/workflows/flatpak_release.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
on:
22
push:
33
branches: [release]
4-
pull_request:
54
name: Flatpak_release
65
jobs:
76
flatpak:

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cmake_minimum_required(VERSION 3.13.4 FATAL_ERROR)
77
project(Modbus_RTU_client_shm LANGUAGES CXX VERSION 0.1.3)
88

99
# settings
10-
set(Target "Modbus_RTU_client_shm") # Executable name (without file extension!)
10+
set(Target "modbus-rtu-client-shm") # Executable name (without file extension!)
1111
set(STANDARD 17) # C++ Standard
1212
set(ARCHITECTURE "native") # CPU architecture to optimize for (only relevant if OPTIMIZE_FOR_ARCHITECTURE is ON)
1313

@@ -41,6 +41,7 @@ endif()
4141

4242
# add executable
4343
add_executable(${Target})
44+
install(TARGETS ${Target})
4445

4546
# set source and libraries directory
4647
add_subdirectory("src")
@@ -53,9 +54,11 @@ set_target_properties(${Target} PROPERTIES
5354
CXX_EXTENSIONS ${COMPILER_EXTENSIONS}
5455
)
5556

56-
# project version and name
57+
# compiler definitions
5758
target_compile_definitions(${Target} PUBLIC "PROJECT_VERSION=\"${CMAKE_PROJECT_VERSION}\"")
5859
target_compile_definitions(${Target} PUBLIC "PROJECT_NAME=\"${CMAKE_PROJECT_NAME}\"")
60+
target_compile_definitions(${Target} PUBLIC "COMPILER_INFO=\"${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}\"")
61+
target_compile_definitions(${Target} PUBLIC "SYSTEM_INFO=\"${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION} ${CMAKE_HOST_SYSTEM_PROCESSOR}\"")
5962

6063
# options that are valid for gcc and clang
6164
function(commonopts)

network.koesling.modbus-rtu-client-shm.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ id: network.koesling.modbus-rtu-client-shm
22
runtime: org.freedesktop.Platform
33
runtime-version: '21.08'
44
sdk: org.freedesktop.Sdk
5-
command: Modbus_RTU_client_shm
5+
command: modbus-rtu-client-shm
66
finish-args:
77
- --device=shm
88
- --share=network
@@ -17,9 +17,7 @@ modules:
1717
- cmake --build build
1818

1919
# install
20-
- mkdir -p "${FLATPAK_DEST}/bin"
21-
- cp build/Modbus_RTU_client_shm ${FLATPAK_DEST}/bin
22-
- ls -lah ${FLATPAK_DEST}
20+
- install -D -m 755 -t ${FLATPAK_DEST}/bin build/modbus-rtu-client-shm
2321
sources:
2422
- type: git
2523
branch: release

network.koesling.test-modbus-rtu-client-shm.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ id: network.koesling.test-modbus-rtu-client-shm
22
runtime: org.freedesktop.Platform
33
runtime-version: '21.08'
44
sdk: org.freedesktop.Sdk
5-
command: Modbus_RTU_client_shm
5+
command: modbus-rtu-client-shm
66
finish-args:
77
- --device=shm
88
- --share=network
@@ -16,9 +16,7 @@ modules:
1616
- cmake --build build
1717

1818
# install
19-
- mkdir -p "${FLATPAK_DEST}/bin"
20-
- cp build/Modbus_RTU_client_shm ${FLATPAK_DEST}/bin
21-
- ls -lah ${FLATPAK_DEST}
19+
- install -D -m 755 -t ${FLATPAK_DEST}/bin build/modbus-rtu-client-shm
2220
sources:
2321
- type: dir
2422
path: .

src/Modbus_RTU_Slave.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ Slave::Slave(const std::string &device,
6262
const std::string error_msg = modbus_strerror(errno);
6363
throw std::runtime_error("Failed to set modbus rtu mode to RS232: " + error_msg);
6464
}
65+
66+
// get socket
67+
socket = modbus_get_socket(modbus);
68+
if (socket == -1) {
69+
const std::string error_msg = modbus_strerror(errno);
70+
throw std::runtime_error("Failed to get socket: " + error_msg);
71+
}
6572
}
6673

6774
Slave::~Slave() {
@@ -97,5 +104,67 @@ bool Slave::handle_request() {
97104
return false;
98105
}
99106

107+
struct timeout_t {
108+
uint32_t sec;
109+
uint32_t usec;
110+
};
111+
112+
static inline timeout_t double_to_timeout_t(double timeout) {
113+
timeout_t ret {};
114+
115+
ret.sec = static_cast<uint32_t>(timeout);
116+
117+
double fractional = timeout - static_cast<double>(ret.sec);
118+
ret.usec = static_cast<uint32_t>(fractional * 1000.0 * 1000.0);
119+
120+
return ret;
121+
}
122+
123+
void Slave::set_byte_timeout(double timeout) {
124+
const auto T = double_to_timeout_t(timeout);
125+
auto ret = modbus_set_byte_timeout(modbus, T.sec, T.usec);
126+
127+
if (ret != 0) {
128+
const std::string error_msg = modbus_strerror(errno);
129+
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
130+
}
131+
}
132+
133+
void Slave::set_response_timeout(double timeout) {
134+
const auto T = double_to_timeout_t(timeout);
135+
auto ret = modbus_set_response_timeout(modbus, T.sec, T.usec);
136+
137+
if (ret != 0) {
138+
const std::string error_msg = modbus_strerror(errno);
139+
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
140+
}
141+
}
142+
143+
double Slave::get_byte_timeout() {
144+
timeout_t timeout {};
145+
146+
auto ret = modbus_get_byte_timeout(modbus, &timeout.sec, &timeout.usec);
147+
148+
if (ret != 0) {
149+
const std::string error_msg = modbus_strerror(errno);
150+
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
151+
}
152+
153+
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0));
154+
}
155+
156+
double Slave::get_response_timeout() {
157+
timeout_t timeout {};
158+
159+
auto ret = modbus_get_response_timeout(modbus, &timeout.sec, &timeout.usec);
160+
161+
if (ret != 0) {
162+
const std::string error_msg = modbus_strerror(errno);
163+
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
164+
}
165+
166+
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0));
167+
}
168+
100169
} // namespace RTU
101170
} // namespace Modbus

src/Modbus_RTU_Slave.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Slave {
1717
modbus_t *modbus; //!< modbus object (see libmodbus library)
1818
modbus_mapping_t *mapping; //!< modbus data object (see libmodbus library)
1919
bool delete_mapping; //!< indicates whether the mapping object was created by this instance
20+
int socket = -1; //!< internal modbus communication socket
2021

2122
public:
2223
/*! \brief create modbus slave (TCP server)
@@ -57,6 +58,42 @@ class Slave {
5758
* @return true: connection closed
5859
*/
5960
bool handle_request();
61+
62+
/*!
63+
* \brief set byte timeout
64+
*
65+
* @details see https://libmodbus.org/docs/v3.1.7/modbus_set_byte_timeout.html
66+
*
67+
* @param timeout byte timeout in seconds
68+
*/
69+
void set_byte_timeout(double timeout);
70+
71+
/*!
72+
* \brief set byte timeout
73+
*
74+
* @details see https://libmodbus.org/docs/v3.1.7/modbus_set_response_timeout.html
75+
*
76+
* @param timeout byte response in seconds
77+
*/
78+
void set_response_timeout(double timeout);
79+
80+
/**
81+
* \brief get byte timeout in seconds
82+
* @return byte timeout
83+
*/
84+
double get_byte_timeout();
85+
86+
/**
87+
* \brief get response timeout in seconds
88+
* @return response timeout
89+
*/
90+
double get_response_timeout();
91+
92+
/*! \brief get the modbus socket
93+
*
94+
* @return socket of the modbus connection
95+
*/
96+
[[nodiscard]] int get_socket() const noexcept { return socket; }
6097
};
6198

6299
} // namespace RTU

0 commit comments

Comments
 (0)