EmbeddedRelated.com
Forums
The 2026 Embedded Online Conference

Linux question -- how to tell if serial port in /dev is for real?

Started by Tim Wescott August 4, 2014
OK, wrong group.  But you guys are smart, and mostly aren't snobs.

So -- is there a way to tell if a device in /dev is for real?  There's 
about a bazzilion /dev/tty<this and that> in my computer at the moment, 
but there's only one (/dev/ttyUSB0) that's actually connected to a 
working serial port.

I would like to know how to know.

Thanks.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

On Mon, 04 Aug 2014 19:39:12 -0500
Tim Wescott <tim@seemywebsite.really> wrote:

> OK, wrong group. But you guys are smart, and mostly aren't snobs. > > So -- is there a way to tell if a device in /dev is for real? There's > about a bazzilion /dev/tty<this and that> in my computer at the moment, > but there's only one (/dev/ttyUSB0) that's actually connected to a > working serial port. > > I would like to know how to know. > > Thanks. > > -- > > Tim Wescott > Wescott Design Services > http://www.wescottdesign.com >
I'm assuming you're looking to do this from code, not the command line. Sooner or later, the lack of reality of a given /dev/dealiewhopper will become a problem. In your case, that should be when you actually try to treat it as a serial port. So the following test code keels over and dies with the error "tcgetattr /dev/ttyS18: Input/output error" --- #include <stdio.h> #include <fcntl.h> #include <termios.h> #define TTY "/dev/ttyS18" int main(void) { int fd = open(TTY, O_RDWR | O_NOCTTY); if (fd < 0) { perror("open " TTY); return 1; } struct termios tio; if (tcgetattr(fd, &tio)) { perror("tcgetattr " TTY); return 1; } return 0; } -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.
On Mon, 04 Aug 2014 18:06:47 -0700, Rob Gaddi wrote:

> On Mon, 04 Aug 2014 19:39:12 -0500 Tim Wescott <tim@seemywebsite.really> > wrote: > >> OK, wrong group. But you guys are smart, and mostly aren't snobs. >> >> So -- is there a way to tell if a device in /dev is for real? There's >> about a bazzilion /dev/tty<this and that> in my computer at the moment, >> but there's only one (/dev/ttyUSB0) that's actually connected to a >> working serial port. >> >> I would like to know how to know. >> >> Thanks. >> >> -- >> >> Tim Wescott Wescott Design Services http://www.wescottdesign.com >> >> > I'm assuming you're looking to do this from code, not the command line. > Sooner or later, the lack of reality of a given /dev/dealiewhopper will > become a problem. In your case, that should be when you actually try to > treat it as a serial port. So the following test code keels over and > dies with the error "tcgetattr /dev/ttyS18: Input/output error" > > --- > > #include <stdio.h> > #include <fcntl.h> > #include <termios.h> > > #define TTY "/dev/ttyS18" > > int main(void) { > int fd = open(TTY, O_RDWR | O_NOCTTY); > if (fd < 0) { > perror("open " TTY); return 1; > } > > struct termios tio; > if (tcgetattr(fd, &tio)) { > perror("tcgetattr " TTY); > return 1; > } > > return 0; > }
Thanks Rob. Eventually I may get my head wrapped around all the termios stuff, probably ten minutes before I get run over by a beer truck. -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
On 05/08/14 04:48, Tim Wescott wrote:
> On Mon, 04 Aug 2014 18:06:47 -0700, Rob Gaddi wrote: > >> On Mon, 04 Aug 2014 19:39:12 -0500 Tim Wescott <tim@seemywebsite.really> >> wrote: >> >>> OK, wrong group. But you guys are smart, and mostly aren't snobs. >>> >>> So -- is there a way to tell if a device in /dev is for real? There's >>> about a bazzilion /dev/tty<this and that> in my computer at the moment, >>> but there's only one (/dev/ttyUSB0) that's actually connected to a >>> working serial port. >>> >>> I would like to know how to know. >>> >>> Thanks. >>> >>> -- >>> >>> Tim Wescott Wescott Design Services http://www.wescottdesign.com >>> >>> >> I'm assuming you're looking to do this from code, not the command line. >> Sooner or later, the lack of reality of a given /dev/dealiewhopper will >> become a problem. In your case, that should be when you actually try to >> treat it as a serial port. So the following test code keels over and >> dies with the error "tcgetattr /dev/ttyS18: Input/output error" >> >> --- >> >> #include <stdio.h> >> #include <fcntl.h> >> #include <termios.h> >> >> #define TTY "/dev/ttyS18" >> >> int main(void) { >> int fd = open(TTY, O_RDWR | O_NOCTTY); >> if (fd < 0) { >> perror("open " TTY); return 1; >> } >> >> struct termios tio; >> if (tcgetattr(fd, &tio)) { >> perror("tcgetattr " TTY); >> return 1; >> } >> >> return 0; >> } > > Thanks Rob. Eventually I may get my head wrapped around all the termios > stuff, probably ten minutes before I get run over by a beer truck. >
You might prefer to use pyserial to access the port from Python - and let pyserial handle the messy termios stuff: import serial try : port = serial.Serial("/dev/ttyUSB0", 9600) print "It's a serial port :-)" except : print "It's not a serial port :-("
On 2014-08-05, Tim Wescott <tim@seemywebsite.please> wrote:

> Thanks Rob. Eventually I may get my head wrapped around all the > termios stuff, probably ten minutes before I get run over by a beer > truck.
If you think the application level termios stuff is a mess, you should take a look at the serial port APIs in the kernel... -- Grant Edwards grant.b.edwards Yow! I threw up on my at window! gmail.com
On Tue, 05 Aug 2014 08:30:08 +0200, David Brown wrote:

> On 05/08/14 04:48, Tim Wescott wrote: >> On Mon, 04 Aug 2014 18:06:47 -0700, Rob Gaddi wrote: >> >>> On Mon, 04 Aug 2014 19:39:12 -0500 Tim Wescott >>> <tim@seemywebsite.really> >>> wrote: >>> >>>> OK, wrong group. But you guys are smart, and mostly aren't snobs. >>>> >>>> So -- is there a way to tell if a device in /dev is for real? >>>> There's about a bazzilion /dev/tty<this and that> in my computer at >>>> the moment, >>>> but there's only one (/dev/ttyUSB0) that's actually connected to a >>>> working serial port. >>>> >>>> I would like to know how to know. >>>> >>>> Thanks. >>>> >>>> -- >>>> >>>> Tim Wescott Wescott Design Services http://www.wescottdesign.com >>>> >>>> >>> I'm assuming you're looking to do this from code, not the command >>> line. Sooner or later, the lack of reality of a given >>> /dev/dealiewhopper will become a problem. In your case, that should be >>> when you actually try to treat it as a serial port. So the following >>> test code keels over and dies with the error "tcgetattr /dev/ttyS18: >>> Input/output error" >>> >>> --- >>> >>> #include <stdio.h> >>> #include <fcntl.h> >>> #include <termios.h> >>> >>> #define TTY "/dev/ttyS18" >>> >>> int main(void) { >>> int fd = open(TTY, O_RDWR | O_NOCTTY); >>> if (fd < 0) { >>> perror("open " TTY); return 1; >>> } >>> >>> struct termios tio; >>> if (tcgetattr(fd, &tio)) { >>> perror("tcgetattr " TTY); return 1; >>> } >>> >>> return 0; >>> } >> >> Thanks Rob. Eventually I may get my head wrapped around all the >> termios stuff, probably ten minutes before I get run over by a beer >> truck. >> >> > You might prefer to use pyserial to access the port from Python - and > let pyserial handle the messy termios stuff: > > import serial > > try : > port = serial.Serial("/dev/ttyUSB0", 9600) > print "It's a serial port :-)" > except : > print "It's not a serial port :-("
All of the desktop serial-port stuff I've done in the last decade has been in support of embedded work, and it's all been heavily dependent on, and used as part of the testing framework for, serial communication code that is to be embedded. So I'm constrained to C or C++. Basically what I do when I write embedded stuff (unless it's really teeny) is to write it so that the actual hardware layer is abstracted -- usually into a class, because I'm usually working in C++. Then all of the "middleware" that I'm seeking validation for finds its way into both the embedded software and a test-bench running on a PC that's talking to the embedded system. Finally, the PC has some GUI (I'm currently using Qt, again because I'm constrained to C++), and the embedded system has whatever it needs to have piled atop the serial port. Knowing whether a port is real or not is really just a convenience for me -- when you do an "open file" dialog on /dev/tty* in Ubuntu you get a buttload of stuff, most of which is not valid; I'd like to fix that. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On Tue, 05 Aug 2014 14:36:14 +0000, Grant Edwards wrote:

> On 2014-08-05, Tim Wescott <tim@seemywebsite.please> wrote: > >> Thanks Rob. Eventually I may get my head wrapped around all the >> termios stuff, probably ten minutes before I get run over by a beer >> truck. > > If you think the application level termios stuff is a mess, you should > take a look at the serial port APIs in the kernel...
Oh, please no! -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Tim Wescott <tim@seemywebsite.really> writes:
> [...] > and mostly aren't snobs.
...mostly? :) -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com
> > If you think the application level termios stuff is a mess, you should > > take a look at the serial port APIs in the kernel...
So, you can hack the kernel.
> Oh, please no!
Back to your question. It might be driver specific, but most will announce what ports they claimed: For example: dmesg | grep tty [ 0.000000] console [tty0] enabled [ 53.006880] cdc_acm 1-3:1.1: ttyACM0: USB ACM device [ 53.008697] cdc_acm 1-3:1.3: ttyACM1: USB ACM device If not, you can always hack the kernel. The source is available for this reason, as well as others.
Tim Wescott <tim@seemywebsite.really> writes:
> All of the desktop serial-port stuff I've done in the last decade has > been in support of embedded work, ... > So I'm constrained to C or C++.
If this is about embedded Linux, Python works great for that.
The 2026 Embedded Online Conference