41 int main(
int argc,
char **argv)
43 ros::init(argc, argv,
"modbus_client_node");
48 std::vector<unsigned short> registers_to_read;
52 if (!has_register_range_parameters)
54 ROS_INFO_STREAM(
"Parameters for register range are not set. Will try to determine range from api spec...");
60 ROS_DEBUG(
"registers_to_read.size() %d", registers_to_read.size());
80 if (has_register_range_parameters)
84 registers_to_read = std::vector<unsigned short>(num_registers_to_read);
85 std::iota(registers_to_read.begin(), registers_to_read.end(), index_of_first_register);
88 catch (
const std::runtime_error &ex)
101 int response_timeout_ms;
105 std::string modbus_read_topic_name;
109 std::string modbus_write_service_name;
119 static_cast<unsigned int>(response_timeout_ms),
120 modbus_read_topic_name, modbus_write_service_name);
123 std::ostringstream oss;
124 if (!registers_to_read.empty())
126 std::copy(registers_to_read.begin(), registers_to_read.end()-1,
127 std::ostream_iterator<unsigned short>(oss,
","));
128 oss << registers_to_read.back();
132 ROS_DEBUG_STREAM(
"Modbus read topic: \"" << modbus_read_topic_name <<
"\"");
133 ROS_DEBUG_STREAM(
"Modbus write service: \"" << modbus_write_service_name <<
"\"");
135 bool res = modbus_client.init(ip.c_str(),
static_cast<unsigned int>(port),
136 static_cast<unsigned int>(modbus_connection_retries),
139 ROS_DEBUG_STREAM(
"Connection with modbus server " << ip <<
":" << port <<
" established");
144 ROS_ERROR_STREAM(
"Connection to modbus server " << ip <<
":" << port <<
" could not be established");
Expection thrown by prbt_hardware_support::ModbusApiSpec.
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
ROSCPP_DECL void spin(Spinner &spinner)
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