10

I want to access the serial port by some BASH script files via /dev/ttyAMA0.

How do I configure the serial port settings like speed, parity even/odd?

Robert
  • 338
  • 2
  • 3
  • 11

2 Answers2

6

To configure the UART ttyAMA0 for your application (like minicom)

pi@raspberrypi:~ $ lsb_release -a
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 8.0 (jessie)
Release:        8.0
Codename:       jessie
pi@raspberrypi:~ $ gpio readall

+-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |    |     |
|   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
|   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
|     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 1 | IN   | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 1 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|  28 |  17 | GPIO.17 |   IN | 0 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
|  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+

Pins 8, GPIO14, TXD must be in mode ALT0 (this pin is the UART serial data input)

Pin 10, GPIO15, RXD, must be in mode ALT0 (this pin is the UART serial data output)

Your boot command line should look like this:

pi@raspberrypi:~ $ cat /boot/cmdline.txt
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

REBOOT the PI

Stop the process that is attached to /dev/ttyAMA0

Because OS version jessie has changed to using systemd, the file /etc/initab no longer exists. You must use the systemd command systemctl to stop getty.target that is attached to /dev/ttyAMA0

pi@raspberrypi:~ $ sudo systemctl stop getty.target

NOTE: This will release ttyAMA0 until you reboot/restart the raspberry pi. After you reboot you will have to give the command again.

Ghanima
  • 15,855
  • 15
  • 61
  • 119
Dale Noble
  • 61
  • 1
  • 1
  • Revised comment: It is currently possible to change back to sysV init and do without systemd on Raspbian "Jessie" - in the same way as the predecessor version "Wheezy" could be switched to systemd. However only archaeologists revisiting this question from the future are likely to want to got to that extreme and instead most will want to find-out how to set a permanent change that will survive a reboot... – SlySven Jan 24 '16 at 22:03
  • 1
    "Pins 8, GPIO14, TXD must be in mode ALT0" - and what do I do if it isn't? – mvmn May 24 '16 at 02:28
  • 1
    P.S. Nevermind - just did "gpio mode 15 ALT0" and "gpio mode 16 ALT0" and everything became fine. – mvmn May 24 '16 at 02:44
  • gpio readall => gpio: command not found on Raspian 4.14 can be solved: https://raspberrypi.stackexchange.com/questions/54116/gpio-command-not-found – AJP Jun 14 '19 at 17:48
6

You may use

stty -F /dev/ttyAMA0 9600

to set the baud rate to 9600 from the command line. Try man stty to see other options.

lenik
  • 11,541
  • 1
  • 30
  • 37