1

I am using an Arduino Uno board with IDE 1.6.7.

I have tried to implement a tweaked version of the basic example (SoftwareSerial example) from the Software Serial library. The code is attached.

It should blink the light and print the message on the serial monitor. But I am not getting anything.

In fact, I also tried the example as it is. It did not work either.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9); // RX, TX

void setup() {
    pinMode(13,OUTPUT);
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
    }
    Serial.println("Goodnight moon!");
    // set the data rate for the SoftwareSerial port
    mySerial.begin(9600);
    delay(1000);
}

void loop() { // run over and over
    mySerial.println("hell");
    if (mySerial.available() > 0) {
        digitalWrite(13,HIGH);
        Serial.write("working..");
        delay(3000);
        digitalWrite(13,LOW);
        delay(2000);
    }
}
  • 1
    You are misunderstanding what "available" does. It doesn't tell you how much you sent, it tells you how many bytes are in the receive buffer. – Nick Gammon Oct 1 '16 at 6:21
  • I am checking if the buffer has some data not the quantum . If this condition is positive , the LED with pin 13 should blink. Simultaneously , a message should be sent to serial terminal . Please let me know if the logic is wrong. – rohit Oct 1 '16 at 6:37
  • 2
    You just say "It does not work", but you could at least provide traces of what you can see on the "normal" serial console and what you see on the other serial console, what you type as input, how you connected TX/RX to your terminal. Without this information, there could be a dozen reasons why your setup does not work. – jfpoilpret Oct 1 '16 at 8:38
  • 3
    Can we see a schematic of your wiring, if you have any wires running around? – Roberto Lo Giacco Oct 1 '16 at 9:26
  • 2
    It would be extremely helpful if you can describe your setup,like your wiring and how you're sending the messages.... – Mero55 Oct 1 '16 at 14:37
2

SoftwareSerial cannot transmit and receive at the same time. So even if you connected pin 8 to 9 with a wire, your sketch will not receive what was transmitted. That's a guess without your wiring diagram.

NeoSWSerial can simultaneously transmit and receive. It is a drop-in replacement for SoftwareSerial.

However, AltSoftSerial is even better, and it only works on pins 8 & 9. Use that instead!

This answer compares all 4 serial types, including HardwareSerial.

0
if(mySerial.available()>0)

mySerial.available() returns the number of bytes available in your serial receive buffer. Unless you are sending commands via serial in pins 8 and 9, this will remain 0 and you will never enter the if statement.

Also, you have two declarations for serial here:

  1. Serial.begin() initialized the built-in UART USB module. Here you will be able to send/receive commands with a program such as Putty.

  2. SoftwareSerial mySerial(8, 9); // RX, mySerial.begin() initializes serial communications with pins 8 and 9 on the Arduino board. This would be if you wanted to send/receive commands with digital I/O instead of, or in addition to, the UART USB module.

  • Thanks for the quick response . However , I am sending bytes to mySerial Port via the following command in the loop: mySerial.println("hell"); Should it not initialize mySerial ? – rohit Oct 1 '16 at 6:39
  • The fact you send something out to TX does not mean it wshall be echoed back to RX, it all depends on how you connected TX and RX and how you setup your serial terminal (if you use one). – jfpoilpret Oct 1 '16 at 8:40
  • The serial portia initialized but there is nothing in the receive buffer unless you are sending data to your arduino from your computer over putty or telnet etc – Anthony Meyer Oct 1 '16 at 13:43
  • Just remove the if statement and the code should make the LED blink at the very least – Anthony Meyer Oct 3 '16 at 2:09
0

I built two scetch for testing. One for Arduino Nano no1 and another for Arduino Nano no2. They are tested and working, not only written to this page.

No2 is sending "hello" and No1 is listening and sending it to PC via Serial. The speed (1200bps) is slow, but so it is easier to see what is happening.

Connect Pin10 of no1 to Pin11 of no2 and gnd to gnd. Connect the Nanos to the USB-ports of the PC and set a monitor program (1200, 8N1) to listen no1.

This seems to be a good way to test Serial.print, Serial.println and Serial.write commands. You will be surprised.

/******No1:*****/

#include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); // RX, TX void setup() { Serial.begin(1200); Serial.println("Testing"); mySerial.begin(1200); } void loop() { // run over and over if (mySerial.available()) //If this is missing, you get fast repeating carpage { Serial.write(mySerial.read()); Serial.print("."); } }

/******No2:*****/

#include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); // RX, TX void setup(){ mySerial.begin(1200); } void loop(){ mySerial.print("Hello"); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.