With recent Linux kernels, there will be the directory
/dev/serial containing links to the serial devices. This contains only links to existing devices.
Classic serial ports are linked as
/dev/ttySx and USB to serial adapters are linked as
/dev/ttyUSBx where
x
is enumerating the ports starting at zero.
Especially the
/dev/ttySx links may be also present for not existing ports. So these must be checked. This can be done on the command line using
setserial -g /dev/ttySx
To check the existance programmatically, open the device and issue an
ioctl
TIOCGSERIAL
request:
int fd = open(device, O_RDWR | O_NONBLOCK);
if (fd != -1)
{
serial_struct info;
ioctl(fd, TIOCGSERIAL, &info);
close(fd);
}