AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
Uart/main.cpp

PollNotifier example

Warning
Unix-like systems only
/*
Copyright (c) 2026 Alexandr Kuzmuk
This file is part of the AsyncFw project. Licensed under the MIT License.
See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
*/
/*
* This exfample for Unix-like systems only
*/
#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;
AsyncFw::PollNotifier notifier(fd);
notifier.notify([&notifier, &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