-
Notifications
You must be signed in to change notification settings - Fork 42
/
5.2.4-Serial2Serial.ino
54 lines (50 loc) · 1.28 KB
/
5.2.4-Serial2Serial.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Arduino Mega端程序
串口使用情况:
Serial ------ computer
Serial1 ------ UNO SoftwareSerial
*/
void setup()
{
// 初始化Serial,该串口用于与计算机连接通信
Serial.begin(9600);
// 初始化Serial1,该串口用于与设备B进行连接通信
Serial1.begin(9600);
}
// 两个字符串分别用于存储AB两端传输来的数据
String device_A_String = "";
String device_B_String = "";
void loop()
{
// 读取从计算机传入的数据,并通过Serial1发送给设备B
if (Serial.available() > 0)
{
if (Serial.peek() != '\n')
{
device_A_String += (char)Serial.read();
}
else
{
Serial.read();
Serial.print("you said: ");
Serial.println(device_A_String);
Serial1.println(device_A_String);
device_A_String = "";
}
}
//读取从设备B传入的数据,并在串口监视器中显示
if (Serial1.available() > 0)
{
if (Serial1.peek() != '\n')
{
device_B_String += (char)Serial1.read();
}
else
{
Serial1.read();
Serial.print("device B said: ");
Serial.println(device_B_String);
device_B_String = "";
}
}
}