In Linux, is it possible to use a local serial port? Something similar to this:

ssh user@localhost

I tried this on Raspbian but it doesn't work (it should place in my shell but it doesn't):

microcom -d /dev/ttyAMA0

I also tried /dev/ttyS0 but to no avail.

I can of course access Raspberry Pi through serial console from another machine.

There is no specific use-case for this question - I just cannot understand how really serial works. If it's possible to connect to the localhost with ssh shouldn't it be also possible with serial port?

  • What's the purpose of this (apart from testing hardware or drivers for expected behavior)? I mean, why someone would develop a useless thing?
    – Serge
    Sep 8 '16 at 20:57
  • Serial is not a packet lobbing network, and must connect to something. You could do this with a USB serial cable and wiring the appropriate RX/TX/etc. lines to the appropriate TX/RX/etc. lines on the rpi.
    – thrig
    Sep 8 '16 at 20:57
  • Also! Beware that the (usually) red wire on USB serial thingies is 5V, which can be bad news for 3.3V things it might accidentally be plugged into. Also, old serial ports on desktops may run at 12V, which can be bad news for 5V or 3.3V things that might be connected to that. Level shifters may help, along with appropriate caution and reading tech specs.
    – thrig
    Sep 9 '16 at 15:18

UART, which I believe you are referring to is a bi-directional 2 wire communication protocol that is typically used by micro-controllers to talk to each other - commonly used to transfer text between two devices. It is what most people mean when they say serial, but there are a lot of different serial protocols out there.

/dev/ttyAMA0 is a UART serial device on the raspberry pi. This device file controls the rx (10) and tx (8) pins on the raspberry pi header. Any program can open the serial port and read/write data to it, but with nothing attached to it all the data sent is just lost and there is nothing sending data back.

Most raspberry pi Linux distributions have a getty listening on /dev/ttyACM0, this is a program that gives you a login prompt and shell. When you launch minicom on /dev/ttyAMA0 you and the getty are sending data on the tx pin, and receiving data on the rx pin - this will confuse anything that is attached to them if you had anything attached you them.

To make the serial device do anything useful, it needs something to communicate with. You can get it to communicate with your pc by getting one of these USB to UART adaptors and attaching the rx to the tx pin, the tx to the rx pin and the gnd pin to gnd then plugging it into your pc. Once plugged in you will get a serial device on your pc (/dev/ttyUSB0 on most Linux distros) and any data you send, will appear on /dev/ttyAMA0 on the pi, and any data the pi sends on /dev/ttyAMA0 will appear on your pc.

Given you the pi already has a getty listening, all you need to do is start minicom or similar program on your pc and you should get a login prompt (might need to hit enter few times). You can of course stop the getty and run another program that can talk to the serial line.

  • 1
    An UART is the device (chip, or part of one) that handles the data transfer
    – ilkkachu
    Sep 8 '16 at 22:30

When you open an SSH connection to localhost, you create a connection that has two endpoints. One of them is the server, one is the client. When one sends, the other receives and vice versa. With SSH, the connection would be over TCP, where the endpoints are identified by their IP addresses and TCP ports. Network connections are simple in that they are mostly implemented in software so new TCP ports can be opened by basically just deciding on a number. That's not the case with a serial port, which is a piece of hardware.

To get useful two-directional communication over serial ports, you'd again need to have two endpoints: two serial ports. Connect one to the other, and you can run a server (e.g.getty) on one, and a client (e.g.minicom) on the other. It doesn't matter if they are on the same machine or two separate machines, but since it's hardware, you have to connect some actual wires.

Usually you'd pull the cable between two devices, since within a single computer there are faster ways of moving data. But nothing would stop you from making a serial connection between two serial ports on a single machine.

With only one serial port, the best you can do is connect the transmit wire to the receive wire, but in that case the program using the port will see its own output as the input from the port. That's not very useful for communication, but might be useful for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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