pilz_modbus_server_mock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef PRBT_HARDWARE_SUPPORT_PILZ_MODBUS_SERVER_MOCK_H
19 #define PRBT_HARDWARE_SUPPORT_PILZ_MODBUS_SERVER_MOCK_H
20 
21 #include <mutex>
22 #include <atomic>
23 #include <cstdint>
24 #include <vector>
25 #include <thread>
26 #include <condition_variable>
27 
28 #include <modbus/modbus.h>
29 
30 #include <ros/console.h>
31 
33 
34 namespace prbt_hardware_support
35 {
41 {
42 public:
43  PilzModbusServerMock(const unsigned int& holding_register_size);
44 
46 
53  void start(const char* ip, const unsigned int port);
54 
63  void startAsync(const char* ip, const unsigned int port);
64 
65  void setHoldingRegister(std::initializer_list<std::pair<unsigned int, uint16_t> > reg_list);
66 
73  void setHoldingRegister(const RegCont& data, unsigned int start_index);
74 
84  RegCont readHoldingRegister(const RegCont::size_type start_index, const RegCont::size_type num_reg_to_read);
85 
89  void terminate();
90 
91  void setTerminateFlag();
92 
102  bool init(const char* ip, unsigned int port);
103 
112  void run();
113 
114 private:
119  bool shutdownSignalReceived();
120 
121 private:
122  const unsigned int holding_register_size_;
124  const unsigned int terminate_register_idx_;
125 
126 private:
128  int socket_{ -1 };
129  modbus_t* modbus_connection_{ nullptr };
130  modbus_mapping_t* mb_mapping_{ nullptr };
131 
132  std::atomic_bool terminate_{ false };
133 
135 
136  std::mutex running_mutex_;
137  std::condition_variable running_cv_;
138 
139  std::thread thread_;
140 
141 private:
142  static constexpr uint32_t DISCONNECT_TIMEOUT_IN_SEC{ 1 };
143  static constexpr uint32_t DISCONNECT_TIMEOUT_IN_USEC{ 0 };
144 
145  static constexpr uint32_t RESPONSE_TIMEOUT_IN_SEC{ 0 };
146  static constexpr uint32_t RESPONSE_TIMEOUT_IN_USEC{ 10000 };
147 
149  static constexpr uint16_t TERMINATE_SIGNAL{ 1 };
150 };
151 
153 {
154  ROS_INFO_NAMED("ServerMock", "Set terminate to true.");
155  terminate_ = true;
156 }
157 
159 {
160  ROS_INFO_NAMED("ServerMock", "Terminate called on ServerMock.");
162 
163  if (thread_.joinable())
164  {
165  ROS_DEBUG_NAMED("ServerMock", "Waiting for worker Thread of ServerMock to be joined.");
166  thread_.join();
167  ROS_DEBUG_NAMED("ServerMock", "Waiting for worker Thread of ServerMock joined.");
168  }
169 }
170 
172 {
174 }
175 
176 } // namespace prbt_hardware_support
177 #endif // PRBT_HARDWARE_SUPPORT_PILZ_MODBUS_SERVER_MOCK_H
#define ROS_INFO_NAMED(name,...)
Offers a modbus server and read/write functionality via subscription/publication. ...
bool init(const char *ip, unsigned int port)
Allocates needed resources for running the server.
std::vector< uint16_t > RegCont
Convenience data type defining the data type for a collection of registers.
RegCont readHoldingRegister(const RegCont::size_type start_index, const RegCont::size_type num_reg_to_read)
Reads the specified number of registers, beginning at the specified start point from the holding regi...
void startAsync(const char *ip, const unsigned int port)
Start the modbus server asynchronously and make it accessible for clients.
const unsigned int terminate_register_idx_
Index of register for server shutdown signal.
void setHoldingRegister(std::initializer_list< std::pair< unsigned int, uint16_t > > reg_list)
data
void run()
Run the server and publish the register values as messages. The value of the modbus register is read ...
#define ROS_DEBUG_NAMED(name,...)
void terminate()
Terminate the Server. Reading or connecting to it will fail.
void start(const char *ip, const unsigned int port)
Start the modbus server and make it accessible for clients.
static constexpr uint16_t TERMINATE_SIGNAL
Register value which indicates that server has to shutdown.
PilzModbusServerMock(const unsigned int &holding_register_size)


prbt_hardware_support
Author(s):
autogenerated on Wed Nov 25 2020 03:10:38