The example Communication/Serial shows you how to read data from UART in C++. That makes things easy enough for reading from each UART on a dedicated thread. If reading from both from the same thread, you either resort to checking both, reading if necessary and then sleeping, or have to get the file descriptor for each and use them both in a poll()/select() call.
Assuming you have a vector or array of Serial pointers:
// polling: check both, read if necessary and then sleep
while(!Bela_stopRequested())
{
for(size_t n = 0; n < uarts.size(); ++n)
{
if(uarts[n]->available())
{
uarts[n]->read(...);
processData(n, ...);
}
usleep(50000);
}
}
// get the file descriptor for each and use them both in a `poll()`/`select()` call.
struct pollfd pfd[uarts.size()];
for(size_t n = 0; n < uarts.size(); ++n)
pfd[n] = uarts[n]->getFd(); // this method needs to be implemented
while(!Bela_stopRequested())
{
int result = poll(pfd, uarts.size(), 500); // wait for data for at most one second
if(result){
handleErrror();
return;
}
for(size_t n = 0; n < uarts.size(); ++n)
{
if(pfd[0].revents & POLLIN)
{
uarts[n]->read(...);
processData(n, ...);
}
}
}
Note that poll() doesn't actually poll the file (as in check, sleep, check, sleep ...), but rather it blocks until a maximum timeout waiting for data to be ready. Note that the second solution is more efficient because it doesn't unnecessarily sleep/wake up at a high rate, which is both a waste of CPU and causes latency and jitter in the readings.