#include <termios.h>
#include <fcntl.h>
#include <AsyncFw/MainThread>
#include <AsyncFw/PollNotifier>
#include <AsyncFw/LogStream>
int main(int argc, char *argv[]) {
const char _ttyS[] = "/dev/ttyUSB0";
int _rate = 115200;
termios tio {};
tio.c_cflag = CS8;
int fd;
if ((fd = open(_ttyS, O_RDWR | O_NONBLOCK)) < 0) {
logWarning() << "Open failed.";
return -1;
}
cfsetospeed(&tio, _rate);
cfsetispeed(&tio, _rate);
tcsetattr(fd, TCSANOW, &tio);
std::string str;
notifier.
notify([¬ifier, &fd, &str](AsyncFw::AbstractThread::PollEvents) {
char buf[128];
int r = read(fd, buf, sizeof(buf) - 1);
if (r > 0) {
buf[r] = 0;
str += buf;
for (;;) {
int i = str.find('\n');
if (i == std::string::npos) break;
lsDebug() << "readed: " << str.substr(0, ++i);
str.erase(0, i);
}
}
});
lsNotice() << "Start Applicaiton";
int ret = AsyncFw::MainThread::exec();
lsNotice() << "End Applicaiton" << ret;
return ret;
}
The PollNotifier class Example:
Definition PollNotifier.h:15
FunctionConnectorProtected< PollNotifier >::Connector< AbstractThread::PollEvents > notify
The PollNotifier::notify connector.
Definition PollNotifier.h:33