Go Down

Topic: HarwareSerial.h (Read 3162 times) previous topic - next topic

bhairu

#include<NewSoftSerial.h>
NewSoftSerial mySerial(2,3);

char c;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  Serial.Print("Key1");
  delay(1000);
  if (mySerial.available() > 0)
  {
    c=mySerial.Read();
  }
}

This is the code i compiled. But couldn't receive data "key1" to the variable c.
Could you check the output in your hardware, once shorting the pins i mentioned?

PaulS

Quote
But couldn't receive data "key1" to the variable c.

How do you know? What is connected to the software serial pins - 2 and 3?

bhairu

#include<NewSoftSerial.h>
NewSoftSerial mySerial(2,3);

char c;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  Serial.Print("Key1");
  delay(1000);
  if (mySerial.available() > 0)
  {
    c=mySerial.Read();
    delay(1000);
  }
Serial.print(c);
}

I could observe garbage values only. I tried with a single character, as well. That is, Serial.print('A'), instead of Serial.print("key1"). Still, i couldn't get the desired result.

bhairu

Software pin 'tx' (pin 3) is shorted to hardware 'rx' and Software pin 'rx' (pin 2) is shorted to hardware 'tx'

PaulS

The proper ratio of answers to questions is 1 to 1. Try again.

robtillaart

Quote
What i expect is to receive those 4 characters "key1" through the software 'rx' pin, to a variable c, once it has been transmitted over the hardware 'tx'.

and what did you get?
and how did you check that?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

bhairu

I printed the variable to verify. But i could obtain garbage values only.
Serial.print(c); Check this code below:

#include<NewSoftSerial.h>
NewSoftSerial mySerial(2,3);

char c;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  Serial.Print('A');
  delay(1000);
  if (mySerial.available() > 0)
  {
    c=mySerial.Read();
    delay(1000);
  }
Serial.print(c);
}

Here in the serial monitor i got output as shown below:
A <a rectangular symbol> A <a rectangular symbol> ........

PaulS

Why are you printing the value of c whether you received any data, or not? Move that print() statement inside the if() block.

And, answer the questions. WHAT IS CONNECTED TO PINS 2 AND 3?

bhairu

pin 3 (software 'tx') connected to hardware 'rx'
pin 2 (software 'rx') connected to hardware 'tx'

Go Up