Wes Miller
Wes Miller

Reputation: 2241

Determine is a Serial Port exists, Linux C/C++

My development target is a Linux computer that has two physical serial ports located at /dev/ttyS0 and /dev/ttyS1. I also expect /dev/ttyS2 and /dev/ttyS3 to be defined.

Using stty -f /dev/ttyS0 and S1 reports the configuration of the two serial ports and reports something menaing "doesn't exist" for S2 and S3.

The hardware designers are talking about offering USB to Serial ports built onto the main board. They'll be DB9 connectors on the outside and just circuitry - no USB connectors on the inside. The number of USB-to-serial connections is not guaranteed and I know enough to design for "many" instead of one.

So, in setting up my port server daemon, I need to be able to determine which ttyS's and which ttyUSB's are "real" and which aren't. Will there ever be placeholdeer ttyUSB's? What if one were to be "unplugged" (say it was, indeed, a real USB coupler on the inside of the PC)?

Is there a better approach than popen()ing stty and examining its output to determine the status of the serial ports? Is there a C API for stty?

Thanks!

Upvotes: 2

Views: 5075

Answers (1)

A.H.
A.H.

Reputation: 66283

The "C-API" which stty uses is tcsetattr(3) and tcgetattr(3).

For finding TTYs without opening the device you may look at this question:

How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?

Upvotes: 4

Related Questions