29 : holding_register_size_(holding_register_size + 1)
30 , terminate_register_idx_(holding_register_size)
34 static constexpr
unsigned BITS_NB {0x00};
35 static constexpr
unsigned INPUT_BITS_NB {0x00};
36 static constexpr
unsigned int INPUT_REGISTERS_NB {0x0};
41 throw std::runtime_error(
"mb_mapping_ is NULL.");
49 ROS_INFO_NAMED(
"ServerMock",
"ModbusServer Mock is closing connection");
72 for(
auto entry : reg_list)
77 <<
" Setting Register " << entry.first <<
" ... " <<
" is not possible");
83 for(
auto entry : reg_list)
85 mb_mapping_->tab_registers[entry.first] = entry.second;
103 <<
" Setting Register " << start_index <<
" ... " << start_index + data.size() - 1
104 <<
" is not possible");
110 for (
size_t index = 0; index < data.size(); ++index)
112 mb_mapping_->tab_registers[start_index+index] = data.at(index);
119 const RegCont::size_type num_reg_to_read)
121 RegCont ret_val(num_reg_to_read, 0);
123 for (RegCont::size_type i = 0; i < num_reg_to_read; ++i)
125 ret_val.at(i) =
mb_mapping_->tab_registers[start_index + i];
141 thread_ = std::thread{ [
this, ip, port]{
142 this->
start(ip, port);
151 #if LIBMODBUS_VERSION_CHECK(3,1,2) // API changed from timeval to uint,uint) in version >= 3.1.2 154 struct timeval response_timeout;
160 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
165 ROS_DEBUG_NAMED(
"ServerMock",
"About to start to listen for a modbus connection");
180 ROS_DEBUG_NAMED(
"ServerMock",
"Accepted new connection, result: %i", result);
#define ROS_INFO_NAMED(name,...)
bool init(const char *ip, unsigned int port)
Allocates needed resources for running the server.
#define ROS_DEBUG_STREAM_NAMED(name, args)
#define ROS_ERROR_STREAM_NAMED(name, args)
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.
void setHoldingRegister(std::initializer_list< std::pair< unsigned int, uint16_t > > reg_list)
std::mutex modbus_register_access_mutex
std::condition_variable running_cv_
const unsigned int holding_register_size_
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,...)
std::atomic_bool terminate_
static constexpr uint32_t RESPONSE_TIMEOUT_IN_USEC
static constexpr uint32_t RESPONSE_TIMEOUT_IN_SEC
modbus_mapping_t * mb_mapping_
void start(const char *ip, const unsigned int port)
Start the modbus server and make it accessible for clients.
modbus_t * modbus_connection_
bool shutdownSignalReceived()
#define ROS_ERROR_NAMED(name,...)
std::mutex running_mutex_
PilzModbusServerMock(const unsigned int &holding_register_size)