树莓派(python)与arduino串口通信的详细步骤
作者:低调神龙 时间:2022-05-29 15:31:06
树莓派与arduino串口通信
第一步:先设置硬件串口分配给GPIO串口
输入sudo raspi-config命令进入树莓派系统配置界面,选择第三个Interfacing Options
进去选择Serial Port
然后选择关闭串口登录功能,打开硬件串口调试功能
修改配置文件
打开/boot/config.txt文件,输入 sudo vim /boot/config.txt,在最后添加俩行:1、dtoverlay=pi3-miniuart-bt 2、force_turbo=1
然后重启树莓派,ls /dev -al查看串口
串口0对应ttyAMA0,串口1对应ttyS0,意味着此时硬件串口可以通过GPIO跟别人进行串口通信了。
第二步:编写测试程序
树莓派:
import serial
import time
port = "/dev/ttyAMA0"
ser = serial.Serial(port,115200,timeout=1) # //打开串口,连接到Arduino上
ser.flushInput() # //清空输入缓冲区
while True:
#ser.write("7".encode())
ser.write("s".encode());
size = ser.inWaiting() # 获得缓冲区字符
if size != 0:
response = ser.read(size);
print(response)
time.sleep(3)
arduino:
void setup()
{
Serial.begin(115200); //定义波特率
}
void loop()
{
while(Serial.available()>0){
char teststring = Serial.read();
Serial.println(teststring);
if('s' == teststring )
Serial.println("Hello Raspberry,I am Arduino.");
}
}
测试结果截图:
接线是,树莓派的R对Arduino的T,树莓派的T对Arduino的R,然后接上GND就行了。
C语言的程序也有,懒得整理了
来源:https://blog.csdn.net/weixin_45506125/article/details/121256083
标签:树莓派,python,arduino,通信
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解PHP设计模式之桥接模式
2023-05-30 10:29:02
PHP模板引擎Smarty中变量的使用方法示例
2023-11-14 23:32:25
php简单生成一组与多组随机字符串的方法
2023-10-04 02:10:09
python多线程semaphore实现线程数控制的示例
2022-12-29 18:31:40
Pycharm添加虚拟解释器报错问题解决方案
2022-05-28 04:07:53
![](https://img.aspxhome.com/file/2023/0/107010_0s.png)
Python实现单例模式的5种方法
2021-07-13 19:40:08
简单理解Python中基于生成器的状态机
2023-04-14 17:35:55
Pygame实战之实现扎气球游戏
2023-06-17 10:17:43
![](https://img.aspxhome.com/file/2023/8/97208_0s.jpg)
分享几个字体设计
2007-10-10 13:53:00
![](https://img.aspxhome.com/file/UploadPic/200710/10/2007101014120666s.jpg)
numpy中的meshgrid函数的使用
2021-10-07 06:11:39
python多线程http下载实现示例
2023-12-03 00:15:34
关于Python函数对象的名称空间和作用域
2023-08-15 02:00:42
Python中用字符串调用函数或方法示例代码
2023-03-05 15:37:59
php使用pack处理二进制文件的方法
2023-11-21 04:26:11
Banner广告条中的字体设计
2010-08-05 20:57:00
![](https://img.aspxhome.com/file/UploadPic/20108/5/01-53s.jpg)
Python+OpenCV六种实时图像处理详细讲解
2022-06-14 10:47:27
![](https://img.aspxhome.com/file/2023/5/85005_0s.png)
python3 mmh3安装及使用方法
2021-08-02 00:31:05
![](https://img.aspxhome.com/file/2023/7/79567_0s.png)
Python一些基本的图像操作和处理总结
2022-02-24 05:46:33
![](https://img.aspxhome.com/file/2023/2/65562_0s.png)
Laravel框架实现定时发布任务的方法
2023-11-22 23:54:57
如何利用Python快速统计文本的行数
2021-11-15 19:48:12