-1

I feel like this should be rather easy, but it continues to vex me, so here it is. I've been trying to find a simple solution to iterate through the available UART serial ports on linux (I'm running the most recent version of Manjaro Linux) and then printing these to the console. Yet, all of the solutions I've found thus far have been incredibly convoluted or they end up throwing a bunch of errors that I can'at figure out when I test them.

So, I've resorted to coming back here to see if anybody else has any ideas. On Windows, there is a GetPortNames() of Windows' System::IO::Ports, maybe a similar API call would be the most ideal.

Update:

So after receiving an answer and using that information to learn a bit more about interacting with serial ports on linux, I eventually ran across a good way to accomplish this effect of iterating through the available ports. It's not quite as simple as a one-line command, but it works so it's fine with me. I found this method of iteration/sorting through the /sys/class/tty directory as an answer to another question (the author of the code that I used is named Søren Holm), and you can look at that here.

Community
  • 1
  • 1
Scorch
  • 437
  • 1
  • 3
  • 14

1 Answers1

1

Two simple possibilities:

  1. Serial ports on linux are character device files, you can see them on /dev/ttyS*. Serial ports created by an usb device are in /dev/ttyUSB*. This reduces your problem to a dirent iteration.

  2. There is also a thing named sysfs, it is essentially a runtime, non-persistant, kernel-internal registry which is exported to the user space via a virtual filesystem. Normally it is mounted below /sys. Below /sys/bus/serio/devices you can find the devices.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • With reference to the dirent iteration listed under possibility #1, the /dev directory contains many more files, not all of which are serial ports, thus I imagine that, by iterating through them and returning all of the entries in the directory, I would need to somehow sift through these files and find which are truly serial ports. How would I go about determining the nature of the files programatically after I have iterated through them? – Scorch Jun 28 '16 at 16:32
  • After some searching, I actually found a solution that worked for me as a method for going about the iteration (i'll update the question above for anybody who may stumble on this eventually). Thanks for pointing me in the right direction, I'll mark this as correct – Scorch Jun 28 '16 at 18:06
  • Yes, but not so many, and the /dev is also a virtual filesystem since around a decade, so you can read its content very fast. Afaik you don't need to worry about that maybe you miss a serial device, or that you open a device which is not serial, although it would be possible (you can simply rename / delete them as root) I never ever in my life meet a scenario where it had happened. – peterh Jun 29 '16 at 01:06
  • 1
    But, if you want to be 100% sure, then you could try to call on them a readonly `ioctl()` which is only usable on serial ports. For example, ask their bit rate or data-stop-parity setting. If it gives back an error, then it is not a serial device. I suspect, you can even re-implement a `GetPortNames()` call in around 20 lines of code. – peterh Jun 29 '16 at 01:08