dvd1478 发表于 2015-2-2 22:17:09

新手求助 SoftwareSerial 问题,串口只能发送,不能接

本帖最后由 dvd1478 于 2015-2-2 22:20 编辑

#include <SoftwareSerial.h>
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
Serial.println("Goodnight moon!");
}

void loop() // run over and over
{   
    if (Serial.available())
    Serial.write(Serial.read());
}

这代码是没有问题,但神奇的是换成以下代码,即不可以

========================================
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX

void setup()
{
// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
mySerial.println("Goodnight moon!");
}

void loop() // run over and over
{
    if (mySerial.available())
    mySerial.write(mySerial.read());
}


以上的代码,能收到Goodnight moon! 但不能把电脑上发送到Arduino的信息返回,可是在第一段的即默认Serial,就没有问题
SoftwareSerial mySerial(0, 1); // RX, TX这是RX0TX1
我换成
SoftwareSerial mySerial(15, 14); // RX3, TX3
SoftwareSerial mySerial(17, 16); // RX2, TX2
SoftwareSerial mySerial(19, 18); // RX1, TX1
都是不成功,只能收到“Goodnight moon!” 感觉的是Arduino 只能发送,不能接收。

这个问题不知道怎么解决呢?我用的是MEGA 2560

引脚上我复查了很多错,没有发现问题呢

Super169 发表于 2015-2-2 23:58:28

你在做什麼? D0, D1 本來就是 hardware serial, 你把它設定成 SoftwareSerial 有什麼好處?

Mega 的 D14-D19, 是用 Serial1, Serial2, Serial3 的.

dvd1478 发表于 2015-2-3 12:01:35

Super169 发表于 2015-2-2 23:58 static/image/common/back.gif
你在做什麼? D0, D1 本來就是 hardware serial, 你把它設定成 SoftwareSerial 有什麼好處?

Mega 的 D14- ...

我知道,我本来的应该是一个串口(TXD2RXT2 )与外围通讯,然后把数据传到电脑上(D0 D1)。尝试很久,发现TXD2 RXT2 或者 TXD1 RXT1或者TXD3 RXT3都是一样,只能发送不能接收,但是用默认的Serial就没有问题。可是换成SoftwareSerial 后同样只能发送不能接收。不知道是什么问题

Super169 发表于 2015-2-3 15:52:44

請說清楚你做過了什麼, 如何判斷是 只是發送, 不能接收?
有嘗試用其他 非 UART 的接腳作SoftwareSerial 測試嗎?
使用庫的時候, 請先看看人家怎麼說 (http://arduino.cc/en/Reference/SoftwareSerial) :

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

如果你堅持要用 硬串口的 I/O 作 軟串口, 也只有 14,15 有可能成功, 其他不成功也是正常的, 人家早說明了.

dvd1478 发表于 2015-2-5 15:42:43

Super169 发表于 2015-2-3 15:52 static/image/common/back.gif
請說清楚你做過了什麼, 如何判斷是 只是發送, 不能接收?
有嘗試用其他 非 UART 的接腳作SoftwareSerial...

哦,明白,我马上试一下,原是分软软件 与硬件之分,以前一值认为是相同的呢!谢谢啦

Super169 发表于 2015-2-5 16:15:19

dvd1478 发表于 2015-2-5 15:42 static/image/common/back.gif
哦,明白,我马上试一下,原是分软软件 与硬件之分,以前一值认为是相同的呢!谢谢啦

如果你是用 mega 的話, 直接用 硬串口 (Serial, Serial1, Serial2, Serial3) 更快更安全.

軟串口一般在只有一個串口的板子才會用 (例如 UNO, nano, ...), 而且每次只可以有一個軟串口作接收, 速度上也有很大的限制.一般 9600 已經差不多了, 再快就很大機會會掉包.但硬串口的話, 用上 115200 也沒大問題.速度及資源運用上有明顯的的分別.

页: [1]
查看完整版本: 新手求助 SoftwareSerial 问题,串口只能发送,不能接