0

I wrote an Arduino program that requires two ports for serial communication. I intended to use both hardware serial and software serial, but the problem is receiving data from receiving buffer of the software serial: it always returns zero byte from myserial.available().

For clarity I used the software serial example in Arduino IDE ver. 1.8.4. I tried to change the baud rate but still the problem persists. I read somewhere that they made some code changes in the SoftwareSerial library's .cpp and .h files. I tried to find the site without success. And my PC where I installed those libraries was stolen. Before that it was working properly.

The mistake I made is that I just copied the code and pasted in the library without concentrating on it. Here is the example of program with two serial ports.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() {
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}
  • What devices are your serial ports connected to? Can you add a clear photo of your wiring and devices. – Squats Sep 12 '17 at 22:55
  • Currently I'm using proteus 8 professional for simulation of the project, where I used arduino UNO and virtual terminal for serial transmitting and receiving. hardware serial port is connected to pins 0 and 1 and software serial port is connected to pins 2 and 3, and also I tried pins 10 and 11 as in example. hardware serial port has no problem and software it transmits without problem, but the problem is on receiving the bytes, myserial.available() returns zero bytes always. that means nothing detected in the receive buffer – kassim Sep 14 '17 at 4:29
1

The SoftwareSerial example code https://www.arduino.cc/en/Reference/SoftwareSerialAvailable shows that the pins used as tx and rx for SoftwareSerial need to be defined using pinMode() as digital input and output.

Example

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available()>0){
    mySerial.read();
  }
}
  • The constructor does set the pinMode(): github.com/arduino/Arduino/blob/1.8.4/hardware/arduino/avr/…. The example sketches included with the library as well as most of the other example code in the reference don't set the pin mode. However, it has been suggested (github.com/arduino/Arduino/issues/3041) that doing this so early in the constructor is bad so whether setting the pin mode in setup() is necessary would depend on the implementation of init(). It shouldn't hurt (other than wasting memory) and it's worth a try. – per1234 Sep 13 '17 at 5:30
  • On struggling to solve my problem I met that example I also tried to copy and make a trial but the problem continued. On the past days I read the solution of the same problem on Github and it solved my problem, but by that time I was a beginner I actually didn't catch up the solution they made in the library files, there were some amendments on some codes and it solved the problem, but right now the computer I installed the files was stolen and the sites I had seen the solution is not opening it end-ups with error that the site is not found. – kassim Sep 14 '17 at 4:24

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.