2

I am trying to understand how computers work and I was thinking about peripherals. Here is what I understand so far but some parts are missing.

Using an Arduino, I send some text every second (a then b then c, ...) via UART to a Pandaboard. I guess that there is a special chip on the Pandaboard or its microprocessor that is always listening to the rx pin. When it sees some voltage change, it starts recording the byte and sends an interrupt to the Linux system. The system stops everything, asks the chip for the new byte, then writes it to /dev/ttyUSB3 for instance.

First thing that I don't understand: if I do cat /dev/ttyUSB3 I see the characters showing up every second (a..b..c). If I stop cat, wait 5 seconds, and do cat again, I see (i..j..k...) but I expect to see the missing characters (d..e..f..g..h) that were sent but not catted but I don't see them. How does cat work? What's in this /dev/ttyUSB3 file?

Conversely, if I do echo "hello" > /dev/ttyUSB3, the system will somehow tell the chip to send electrical signals on the tx pin. Does it send it byte by byte to the chip? Or all together?

Zelda
  • 6,158
  • 1
  • 21
  • 27
Thomas
  • 883
  • 2
  • 12
  • 25
  • somewhat related: [The TTY demystified](http://www.linusakesson.net/programming/tty/) – jfs Dec 16 '13 at 00:31

1 Answers1

4

The files in /dev are device files, they are not real files. They don't, usually, have content on disk (a few representing raw disks do). There is nothing in the file. If you do ls -l /dev, you will see all there is to these "files": A name, some special bits, see first character of mode (is is not d=directory, -=regular file, l=symlink, it is c=character device or b=block device), then there are 2 numbers where the files size should be. These map to tables in the kernel, they are device major and minor numbers.

When a process reads a serial device the data is streamed to the process, the same as a file is streamed when you read it sequentially, or the stream of a pipe. The OS will not remember what arrives at the port when no process is interested, this would require a huge amount (infinite) of storage. If you want this you need a process to record it.

When data is sent out, it may be sent a byte at a time or a block of several bytes it depends on the hardware, and is not a unix specific thing.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
  • It seems that on some micro controllers there is a small ring buffer that holds the last (256 ?) bytes, so nothing like that on PCs ? – Thomas Dec 25 '13 at 17:30