41 int main(
int argc,
char** argv)
43 ros::init(argc, argv,
"modbus_client_node");
52 std::vector<unsigned short> registers_to_read;
59 bool has_register_range_parameters =
61 if (has_register_range_parameters)
65 registers_to_read = std::vector<unsigned short>(
static_cast<unsigned long>(num_registers_to_read));
66 std::iota(registers_to_read.begin(), registers_to_read.end(), index_of_first_register);
70 ROS_INFO_STREAM(
"Parameters for register range are not set. Will try to determine range from api spec...");
73 ROS_DEBUG(
"registers_to_read.size() %zu", registers_to_read.size());
76 catch (
const std::runtime_error& ex)
89 int response_timeout_ms;
92 std::string modbus_read_topic_name;
95 std::string modbus_write_service_name;
101 pnh, registers_to_read, std::unique_ptr<LibModbusClient>(
new LibModbusClient()),
102 static_cast<unsigned int>(response_timeout_ms), modbus_read_topic_name, modbus_write_service_name);
105 std::ostringstream oss;
106 if (!registers_to_read.empty())
108 std::copy(registers_to_read.begin(), registers_to_read.end() - 1, std::ostream_iterator<unsigned short>(oss,
","));
109 oss << registers_to_read.back();
113 ROS_DEBUG_STREAM(
"Modbus read topic: \"" << modbus_read_topic_name <<
"\"");
114 ROS_DEBUG_STREAM(
"Modbus write service: \"" << modbus_write_service_name <<
"\"");
116 bool res = modbus_client.init(ip.c_str(),
static_cast<unsigned int>(port), modbus_connection_retries,
119 ROS_DEBUG_STREAM(
"Connection with modbus server " << ip <<
":" << port <<
" established");
126 <<
". Make sure that your cables are connected properly and that " 127 "you have set the correct ip address and port.");
static const std::string PARAM_MODBUS_SERVER_PORT_STR
void getAllDefinedRegisters(std::vector< unsigned short > ®isters)
Expection thrown by prbt_hardware_support::PilzModbusClient.
static const std::string PARAM_MODBUS_RESPONSE_TIMEOUT_STR
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static const std::string SERVICE_MODBUS_WRITE
static const std::string PARAM_MODBUS_WRITE_SERVICE_NAME_STR
static constexpr double MODBUS_CONNECTION_RETRY_TIMEOUT_S_DEFAULT
static const std::string TOPIC_MODBUS_READ
static constexpr int32_t MODBUS_CONNECTION_RETRIES_DEFAULT
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
static const std::string PARAM_MODBUS_READ_TOPIC_NAME_STR
Connects to a modbus server and publishes the received data into ROS.
static const std::string PARAM_INDEX_OF_FIRST_REGISTER_TO_READ_STR
static constexpr int MODBUS_RESPONSE_TIMEOUT_MS
#define ROS_DEBUG_STREAM(args)
int main(int argc, char **argv)
Read requested parameters, start and initialize the prbt_hardware_support::PilzModbusClient.
#define ROS_INFO_STREAM(args)
static const std::string PARAM_MODBUS_CONNECTION_RETRIES
static const std::string PARAM_MODBUS_CONNECTION_RETRY_TIMEOUT
#define ROS_ERROR_STREAM(args)
Wrapper around libmodbus, see https://libmodbus.org/.
Specifies the meaning of the holding registers.
static const std::string PARAM_MODBUS_SERVER_IP_STR
static const std::string PARAM_NUM_REGISTERS_TO_READ_STR