Monday, 21 February 2011

Find your serial port


 First we need to track down our serial port, in both a hardware and software sense.
____________________


The serial connector on most PCs, if there is one, is a D9, named as it has 9 connectors, and the shell has a D shape. D25 connectors are also common on equipment, but not so much on PCs. The serial connection is a disappearing feature from most PCs and laptops, however USB to serial converters are both cheap, and easy to  to find. Alternatively laptops can use a PC Card, and desktops can use PCI or PCI Express boards.


Having found where our serial connector is, we need to find out how our computer refers to it. Everything is a file in things Unixy, and serial ports are no different. Devices - hard disks, printers network connections and the like, reside in the /dev/ directory. So, which of the many files in the /dev directory are serial ports? Originally, before the advent of graphics cards,  the main use of serial ports was for terminals, often shortened to tty.











$ls /dev/tty*
tty   tty14 tty20 tty27 tty33 tty4  tty46 tty52 tty59 tty8  ttyS5
tty0  tty15 tty21 tty28 tty34 tty40 tty47 tty53 tty6  tty9  ttyS6
tty1  tty16 tty22 tty29 tty35 tty41 tty48 tty54 tty60 ttyS0 ttyS7
tty10 tty17 tty23 tty3  tty36 tty42 tty49 tty55 tty61 ttyS1
tty11 tty18 tty24 tty30 tty37 tty43 tty5  tty56 tty62 ttyS2
tty12 tty19 tty25 tty31 tty38 tty44 tty50 tty57 tty63 ttyS3
tty13 tty2  tty26 tty32 tty39 tty45 tty51 tty58 tty7  ttyS4


This shows up quite a few terminals, most of which are not attached to serial ports. It's the ones with a capital 'S' that we're interested in.

$ls /dev/ttyS*
/dev/ttyS0 /dev/ttyS2 /dev/ttyS4 /dev/ttyS6
/dev/ttyS1 /dev/ttyS3 /dev/ttyS5 /dev/ttyS7

Are there really that many serial ports on my computer? No, not all of these are used, so we have to figure out which ones are. Setserial is the command line tool for configuring serial port, and also reporting the current settings. The -g option lets you interrogate a list of ports.

Depending how your user is configured, you may need to do the following as root.

$setserial -g /dev/ttyS[0-7]
/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
/dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3
/dev/ttyS2, UART: 16550A, Port: 0x03e8, IRQ: 4
/dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3
/dev/ttyS4: No such device or address
/dev/ttyS5: No such device or address
/dev/ttyS6: No such device or address
/dev/ttyS7: No such device or address

Here we can see that /dev/ttyS0 and /dev/ttyS2 are our real, working serial ports.

This covers serial ports on main boards, bus based boards, and PC Cards, but USB serial ports are slightly different. They're still in /dev but have a different name, and only appear in /dev when plugged in.

$ls /dev/ttyUSB*
/dev/ttyUSB0

$setserial /dev/ttyUSB0
/dev/ttyUSB0, UART: 16654, Port: 0x0000, IRQ: 0


Now we have found our serial port, we need permission to access it. And that's what we'll be doing in the next post.
____________________

No comments:

Post a Comment