What exactly are items in the /dev directory?
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?
Solution 1:
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:
-
Try
cat < /dev/ttyS0
and see if that works.
You might solve this error by adding your user to the same group (ie.
dialout
).
Solution 2:
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).