[Linux] 树莓派(二)串口通信
序
1树莓派4b,用的本身自带串口
一、设置树莓派
11. 输入命令 sudo raspi-config
22. 选择 5 Interfacing Options Configure connections to peripherals -->P6 Serial -->No -->Yes 最后显示如图 -->ok
13. 树莓派4b物理引脚连接对 8号引脚 Tx 10号引脚 Rx
24. 命令行输入 ls /dev -al 找到显示如图
15. 命令行输入 sudo nano /boot/config.txt 打开文件后,在最后一行加入 `dtoverlay=pi3-miniuart-bt force_turbo=1` 保存并退出
26. 命令行 sudo reboot 重启
37. 重复4步骤,发现位置互换,说明设置成功
二、串口连接测试(Python)
1python 代码如下
1#!/usr/bin/python
2# -*- coding:utf-8 -*-
3
4import serial
5import time
6
7ser = serial.Serial('/dev/ttyAMA0',115200)
8if ser.isOpen ==False:
9 ser.open()
10 print("open success")
11for i in range(6):
12 ser.write(b'i')
13 time.sleep(0.5)
14
15try:
16 while True:
17 size = ser.inWaiting()
18 if size !=0:
19 response = ser.read(size)
20 print (response)
21 ser.flushInput()
22 time.sleep(0.1)
23except KeyboardInterrupt:
24 ser.close()
1串口另一端连接电脑,正常情况下即可正常发送数据
三、结
1同时调试了usb转串口,弄明白就好,还有Python 需要配置环境


吾心信其可行,
则移山填海之难,
终有成功之日!
——孙文
则移山填海之难,
终有成功之日!
——孙文

0 评论