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 {
36 
42 {
43 public:
44  PilzModbusServerMock(const unsigned int& holding_register_size);
45 
47 
54  void start(const char* ip, const unsigned int port);
55 
64  void startAsync(const char* ip, const unsigned int port);
65 
66  void setHoldingRegister(std::initializer_list< std::pair<unsigned int, uint16_t> > reg_list);
67 
74  void setHoldingRegister(const RegCont& data, unsigned int start_index);
75 
85  RegCont readHoldingRegister(const RegCont::size_type start_index,
86  const RegCont::size_type num_reg_to_read);
87 
91  void terminate();
92 
93  void setTerminateFlag();
94 
104  bool init(const char* ip, unsigned int port);
105 
114  void run();
115 
116 private:
121  bool shutdownSignalReceived();
122 
123 private:
124  const unsigned int holding_register_size_;
126  const unsigned int terminate_register_idx_;
127 
128 private:
129 
131  int socket_ {-1};
132  modbus_t *modbus_connection_ {nullptr};
133  modbus_mapping_t *mb_mapping_ {nullptr};
134 
135  std::atomic_bool terminate_ {false};
136 
138 
139  std::mutex running_mutex_;
140  std::condition_variable running_cv_;
141 
142  std::thread thread_;
143 
144 private:
145  static constexpr uint32_t DISCONNECT_TIMEOUT_IN_SEC {1};
146  static constexpr uint32_t DISCONNECT_TIMEOUT_IN_USEC {0};
147 
148  static constexpr uint32_t RESPONSE_TIMEOUT_IN_SEC {0};
149  static constexpr uint32_t RESPONSE_TIMEOUT_IN_USEC {10000};
150 
152  static constexpr uint16_t TERMINATE_SIGNAL {1};
153 
154 };
155 
157 {
158  ROS_INFO_NAMED("ServerMock", "Set terminate to true.");
159  terminate_ = true;
160 }
161 
163 {
164  ROS_INFO_NAMED("ServerMock", "Terminate called on ServerMock.");
166 
167  if(thread_.joinable())
168  {
169  ROS_DEBUG_NAMED("ServerMock", "Waiting for worker Thread of ServerMock to be joined.");
170  thread_.join();
171  ROS_DEBUG_NAMED("ServerMock", "Waiting for worker Thread of ServerMock joined.");
172  }
173 }
174 
176 {
178 }
179 
180 }
181 #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 Tue Feb 2 2021 03:50:17