This repository has been archived on 2021-09-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
test_ros/src/test_topic/src/minPublisher.cpp
2021-09-07 20:45:14 +08:00

47 lines
843 B
C++

#include "test_topic/minPublisher.h"
MinPublisher::MinPublisher()
{
_sender = this->advertise<MsgType>("test_topic", 200);
}
MinPublisher::~MinPublisher()
{
_serial.close();
}
void MinPublisher::run(const char *device)
{
_serial.open(device, 115200, 1, 8, 'N');
char *read_buf = new char[500];
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
memset(read_buf, 0, 500);
int len = _serial.read_data(read_buf, 500);
if (len <= 0)
{
continue;
}
ss << read_buf;
msg.data = ss.str();
ROS_INFO("send: '%s'", msg.data.c_str());
_sender.publish(msg);
}
}
int main(int argc, char *argv[])
{
ros::init(argc, argv, "test_pub_node");
MinPublisher nh;
nh.run("/dev/ttyUSB0");
return 0;
}