如何配置/ dev / ttyAMA0的串行端口设置?


9

我想通过/ dev / ttyAMA0通过一些BASH脚本文件访问串行端口。

如何配置串行端口设置,例如速度,奇偶校验/奇校验?

Answers:


6

您可以使用

stty -F /dev/ttyAMA0 9600

从命令行将波特率设置为9600。尝试man stty查看其他选项。


6

为您的应用程序(如minicom)配置UART ttyAMA0

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-+---+------+---------+-----+-----+

引脚8,GPIO14,TXD必须处于模式ALT0(此引脚是UART串行数据输入)

引脚10,GPIO15和RXD必须处于模式ALT0(此引脚是UART串行数据输出)

您的启动命令行应如下所示:

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

重新启动PI

停止附加到/ dev / ttyAMA0的进程

由于操作系统版本jessie已更改为使用systemd,因此文件/ etc / initab不再存在。您必须使用systemd命令systemctl来停止附加到/ dev / ttyAMA0的getty.target

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

注意:这将释放ttyAMA0,直到您重新启动/重新启动树莓派。重新启动后,您将不得不再次输入命令。


修改后的注释:当前可以改回sysV init,而无需systemd在Raspbian“ Jessie”上进行操作-与先前版本“ Wheezy”可以切换到相同 systemd。但是,只有考古学家从未来重新审视这个问题,才有可能
走到

1
“引脚8,GPIO14,TXD必须处于模式ALT0”-如果不是,该怎么办?
mvmn

1
PS Nevermind-刚刚执行了“ gpio模式15 ALT0”和“ gpio模式16 ALT0”,一切都变好了。
mvmn

gpio readall=> gpio: command not found上Raspian 4.14可以解决:raspberrypi.stackexchange.com/questions/54116/...
AJP
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.