Scanserial

From wiki.emacinc.com
Revision as of 17:17, 5 November 2015 by Kyoungmeyer (talk | contribs) (Fixed a link and some overlapping text/formatting.)
Jump to: navigation, search
TODO: {{#todo: FinalDraft (11.04.2015-12:17->MD+);(11.05.2015-11:05->MD+);(11.05.2015-17:15->KY+)|Mike Dean|OE 5.0,MD,KY,FinalDraft}}

EMAC provides a tool named scanserial to assist a user in finding out which device nodes on a computer correspond to its serial ports, and to assist in finding out which particular device node corresponds to a particular serial port. The scanserial tool was created because no existing tool provides this functionality in a convenient way. Other methods for determining this all require a slow trial and error approach.

This guide walks the reader through the steps required to perform these tasks using the EMAC scanserial tool.

Background

Linux systems use virtual files to provide device nodes which represent hardware devices attached to a system. These device nodes reside in the /dev directory. Each device node corresponds to one device, or to one endpoint within a device.

Software which wishes to use a hardware device will open() the virtual file, call ioctl() on it to control aspects of the I/O operations, and read() and write() data through the file descriptor acquired in the open() call. The Pages with Related Content section provides a link to an example program which does this, but many more can be found online via your favorite search engine.

The serial ports typically are represented by device nodes with one of the two following naming schemes:

Standard Serial Devices
/dev/ttyS*
USB Serial Devices
/dev/ttyUSB*

Any given system may have one or both of these types of serial devices attached to it. Serial ports provided by a motherboard or add-in PCI/PCIe card are classified in this document as Standard Serial Devices. USB to serial devices, including USB devices which provide several serial ports, are classified in this document as USB Serial Devices.

Each device node describes the class of device it is associated with, and has an associated major and minor magic number. Serial devices normally provide a character device, rather than a block or FIFO class device. The magic numbers are used to identify to the operating system kernel the specific device on the system to which the device node corresponds. Further details about device nodes are beyond the scope of this document, but can be found on this site and elsewhere in documentation on Linux device drivers.

General Information

The scanserial tool is provided by EMAC to assist customers in getting a development environment set up. Since scanserial is an EMAC provided tool, you will need to install it unless you are using an EMAC LDC (which has it pre-installed).

If your Ubuntu machine isn't already configured to use the EMAC apt repository for installing software, you will need to follow this guide on how to do so. When your system is configured for the EMAC apt repository, you can install the scanserial tool by installing the emac-tools-util package. To install it with apt-get on the command line, run:

developer@developerpc:~# sudo apt-get install emac-tools-util

Alternatively, you may open the Ubuntu Software Center and search for the emac-tools-util project. Clicking on the search result will provide you with an option to install the package. Click the Install button to install it.

scanserial

The scanserial utility has a few options which allow you to customize the behavior of scanserial or find out more about it. The list below describes these options.

-v, --verbose
Display verbose output. When displaying a list of devices, this will show metadata associated with each device node.
-c, --color
Displays output in color (if available).
-f, --find-loopback
Uses the same algorithm which is used to find a list of serial port device nodes, then scans the list of device nodes for loopback devices. Displays every device it finds which appears to have a loopback attached. Note that this can be fooled by connected devices which echo input back to the port, such as a logged in console on a board.
-h, --help
Displays help for using the scanserial tool.
-l, --list-serials
Lists the serial device nodes found in /dev which appear to be serial ports. If combined with the verbose option, this will also show metadata about each one. Add the color switch to colorize the output to make it easier to read.
--version
Displays the version number of the scanserial tool.

Return Value

The return value of scanserial corresponds to the success of its operation. If the operation succeeded, it returns zero, as is the customary behavior for all *nix software. If it fails, it will return a value greater than zero (which is also the standard behavior for software on Linux systems).

Examples

Listing Available Serial Ports

Use the -l (lowercase L) option to list available serial ports. Here, there are 6 standard serial ports and 8 USB serial ports available.

developer@developerpc:~# scanserial -l

/dev/ttyS0 /dev/ttyS1 /dev/ttyS4 /dev/ttyS5 /dev/ttyS6 /dev/ttyS7 /dev/ttyUSB0 /dev/ttyUSB1 /dev/ttyUSB2 /dev/ttyUSB3 /dev/ttyUSB4 /dev/ttyUSB5 /dev/ttyUSB6 /dev/ttyUSB7

developer@developerpc:~#


Listing Detailed Information About Serial Ports

The verbose option, --verbose or -v, causes scanserial to output much more detailed information.

developer@developerpc:~# scanserial -lv

/dev/ttyS0: type: 4 line: 0 port: 1016 irq: 4 flags: 0x10000040 xmit_fifo_sz: 16 custom_divisor: 0 baud_base: 115200 close_delay: 50 closing_wait: 3000

/dev/ttyS1: type: 4 line: 1 port: 760 irq: 3 flags: 0x10000040 xmit_fifo_sz: 16 custom_divisor: 0 baud_base: 115200 close_delay: 50 closing_wait: 3000

developer@developerpc:~#


Further output, in this example, was truncated for the sake of brevity.

Finding an Attached Loopback

When you need to figure out which device node corresponds to the specific one you wish to use, which can be especially difficult when you have a lot of serial ports (like the 14 on the system shown above), a loopback is very useful. A loopback is a connection between the Rx and Tx lines on a serial port. Many large vendors sell RS-232 serial loopback dongles. In a pinch, you can make your own loopback by connecting the Rx and Tx lines together with a wire or jumper.

Once you have a loopback connected to the desired port, run the scanserial command with the --find-loopback or -f (the shortened abbreviated version) argument:

developer@developerpc:~# scanserial -f

/dev/ttyUSB6 : Found!

developer@developerpc:~#


If you have multiple loopbacks attached, they will all show up in the output list. If you wish to ensure that scanserial is checking every device node you're expecting it to check, you can add the verbose flag (--verbose or -v for short) when you call scanserial. For example, if you have two loopbacks attached and call scanserial with the verbose flag set, it might look like this:

developer@developerpc:~# scanserial -fv

/dev/ttyS0 : no /dev/ttyS1 : no /dev/ttyS4 : no /dev/ttyS5 : no /dev/ttyS6 : no /dev/ttyS7 : no /dev/ttyUSB0 : no /dev/ttyUSB1 : no /dev/ttyUSB2 : Found! /dev/ttyUSB3 : no /dev/ttyUSB4 : no /dev/ttyUSB5 : no /dev/ttyUSB6 : Found! /dev/ttyUSB7 : no

developer@developerpc:~#


Conclusion

The scanserial tool greatly simplifies the once cumbersome, manual, trial-and-error process of figuring out which of the potentially 100+ device nodes have associated physical serial ports, and which of those is/are the one(s) you're looking for. This freely available tool can save you a great deal of time when configuring serial ports. If you don't need to perform this task right now, perhaps because you're using an EMAC LDC, keeping in mind that EMAC provides this tool may help you save time and frustration at a later date.

Further Information

Where to Go Next
Pages with Related Content