0

I'm having a bit of trouble with the Arduino 1.0.5 SoftwareSerial library. I understand that ideally, I should be using a Mega here, but unfortunately I need to make do with an Uno R3.

I understand that it is possible to use multiple SoftSerial ports, provided that you switch between the two using Soft1.listen(); etc. but I seem to be having troubles with this.

If I run the below sketch for example:

#include <SoftwareSerial.h>

SoftwareSerial Soft1(3,2);
SoftwareSerial Soft2(5,4);

void setup()
{
    Soft2.begin(9600);
}

void loop()
{
    Soft2.print("Test");
    delay(1000);
}

I get exactly what I would expect - The word 'Test' printed every second. Now, if I replace Soft2.begin with Soft1.begin I get nothing. Adding Soft1.listen(); either before the 'print', or even before the 'begin' makes no difference. I need to read/write from one port, and write only to the second port, but it would appear that the SoftwareSerial library make up its mind on which port to use when they are declared rather during the program.

Has anybody ever used multiple SoftwareSerial ports successfully?

EDIT:

Just some more information for folks, if I swap the order of the software serial declarations, the live/dead ports reverse, so it really does appear to be something around that point. Just for testings sake, I've tried not starting either port until it is needed and doing the following:

void loop() {
    Soft2.begin(9600);
    Soft2.print("test2");
    Soft2.end();
    delay(1000); // For stability/to alleviate timing issues, whatever.
    Soft1.begin(9600);
    Soft1.print("test1");
    Soft1.end();
}

But to no avail. I'm just seeing the 'test2' output.

Cheers,
Alex.

4

1 回答 1

0

对不起,这个答案在最初的帖子之后很久了。我以前确实遇到过这个问题。我已将屏幕和 RFBee 连接到一个 arduino。然后,我将串行通信从我的计算机发送到第一个 arduino,并连接了第一个 RFBee。然后这个 arduino 通过 RFBee 将信息发送到第二个 arduino。然后第二个 arduino 将数据发送到屏幕,并通过 RFBee 向第一个 arduino 返回一个语句,第一个 arduino 返回到计算机,说它已经发送。由于某种原因,该消息不会发送回第一个 arduino。这是关于在读取之前必须选择当前打开的串行端口的问题。如果你放一个 SoftX.listen(); 在您使用它之前,它应该可以正常工作。如果您需要更好的解释,这里有http://arduino.cc/en/Tutorial/TwoPortReceive希望这对您有所帮助。另外,没有 SoftX.begin(); 循环中的语句。可能会导致问题。

于 2014-12-11T20:30:49.147 回答