我目前正计划在Arduino Uno上使用SoftwareSerial 2对(tx,rx)。
我有以下代码:
#include <SoftwareSerial.h>
#define EC_RX 8 // Make sure RX<->TX!!!!
#define EC_TX 9
#define PH_RX 10
#define PH_TX 11
SoftwareSerial ecSerial(EC_RX, EC_TX);
SoftwareSerial phSerial(PH_RX, PH_TX);
String ecSensorString = "";
bool ecSensorComplete = false;
String phSensorString = "";
bool phSensorComplete = false;
void setup() {
Serial.begin(38400);
ecSerial.begin(38400);
phSerial.begin(38400);
ecSensorString.reserve(30);
phSensorString.reserve(30);
delay(5000);
Serial.println("Setup Complete");
}
void getEC() {
Serial.println("getEC");
ecSerial.listen();
while (ecSerial.available()) {
char inchar = (char)ecSerial.read();
ecSensorString += inchar;
if (inchar == '\r') {
ecSensorComplete = true;
}
}
if (ecSensorComplete) {
Serial.println(ecSensorString);
ecSensorString = "";
ecSensorComplete = false;
}
}
void getPH() {
Serial.println("getPH");
phSerial.listen();
while (phSerial.available()) {
char inchar = (char)phSerial.read();
phSensorString += inchar;
if (inchar == '\r') {
phSensorComplete = true;
}
}
if (phSensorComplete) {
Serial.println(phSensorString);
phSensorString = "";
phSensorComplete = false;
}
}
void loop() {
getEC();
getPH();
delay(5000);
}
如果我在getEC()
内发表评论loop()
,我会得到以下数据:
Setup Complete
getPH
3.22
如果我在getPH()
内发表评论loop()
,我会:
Setup Complete
getEC
0.00,0,0.00,1.000
但是,如果我将这两个取消注释,我得到了:
Setup Complete
getEC
getPH
即使使用Serial.listen()
,我的串口也无法按预期运行。我的引脚遵循http://arduino.cc/en/Tutorial/TwoPortReceive找到的布线。
是什么给出的?