本次实验测试arduino uno 异机通过串口收发数据
选用两块arduino uno.
实验原型为一块uno作为主机,发送数据,另一块uno作为从机接收数据。
主机选用软串口发送数据(LIBRARY:softserial)测试程序:
复制代码
1
2
3#include <SoftwareSerial.h>
复制代码
从测试程序选用普通的硬件串口管脚:0和1.
1
2
3
4
5
6
7
8
9
10
11
12
13//定义管脚2/3分别为RX,TX. SoftwareSerial mySerial(2, 3); // RX, TX void setup() { mySerial.begin(9600); mySerial.println("Hello, world?"); } void loop() { mySerial.print("Hello, world?"); delay(1000); }
测试程序:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23String comdata = ""; //String 定义一个空的字符串 int STATE=1; void setup() { Serial.begin(9600); pinMode(13,OUTPUT); } void loop() { while (Serial.available() > 0) { comdata += char(Serial.read()); delay(2); } if (comdata.length() > 0) { Serial.println(comdata); STATE=!STATE; digitalWrite(13,STATE); //通过电平反转可直观看出程序的运行。 comdata = ""; } }
注意:连接两个串口时一定要交叉连、R---->T,,,,T----->R.
测试获得串口打印数据
最后
以上就是俊秀小丸子最近收集整理的关于arduino uno 多机串口通信的全部内容,更多相关arduino内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复