Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have been writing an application in C++ that transmits radio messages by writing to a modem connected to the serial port. To do this I need to get a file descriptor using code of the form

fd1=open(“/dev/ttyS0″, O_RDWR | O_NOCTTY | O_NDELAY);

This made me curious about the nature of the items in /dev. To try to get an idea of the contents of these items I tried a

sudo cat /dev/ttyS0

only to get the message

cat: /dev/ttyS0: Input/output error

which didn't help my understanding at all. Are these items actually a file at all or just a kind of link that allows access to the driver functionality?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Well... everything in Linux is a file or a directory. Things in /dev are special files.

The majority of devices are either block or character devices; however other types of devices exist and can be created. In general, 'block devices' are devices that store or hold data, 'character devices' can be thought of as devices that transmit or transfer data. For example, diskette drives, hard drives and CD-ROM drives are all block devices while serial ports, mice and parallel printer ports are all character devices. There is a naming scheme of sorts but in the vast majority of cases these are completely illogical.

Devices are defined by type, such as 'block' or 'character', and 'major' and 'minor' number. The major number is used to categorize a device and the minor number is used to identify a specific device type. For example, all IDE device connected to the primary controller have a major number of 3. Master and slave devices, as well as individual partitions are further defined by the use of minor numbers.

Taken from Linux-Filesystem-Hierarchy


2 options:

  1. Try

     cat < /dev/ttyS0
    

    and see if that works.

  2. You might solve this error by adding your user to the same group (ie. dialout).

share|improve this answer
    
Thanks this is very informative. –  mathematician1975 Jul 18 '12 at 18:44
    
'character devices' can be thought of as devices that transmit or transfer data. And hence why you get this: crw-rw---- 1 root dialout 4, 64 Feb 2 15:34 ttyS0 I assume that "c" refers to character. –  JohnMerlino Feb 2 at 23:19

From The Linux Documentation Project:

The computer considers each serial port to be a "device". It's sometimes called a terminal device since at one time terminals were the most common use for a serial port. For each such serial port there is a special file in the /dev (device) directory. /dev/ttyS0) is the special file for the serial port known as COM1 in the DOS/Windows world.

To send text to a terminal you may redirect standard output of some command-line command to the appropriate special file. For example typing "echo test > /dev/ttyS1" at the command prompt should send the word "test" to the terminal on ttyS1 (COM2) provided you have write permission on /dev/ttyS1. Similarly, typing "cat my_file > /dev/ttyS0" will send the contents of the file my_file to COM1 (ttyS0).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.