NewSoftSerial : Unexpected reading values

I’m trying to verify the correct retrieving of serial information with NewSoftSerial library .
I wire the physical UART from Arduino (pins 0 and 1) to two other digital pins (2,3) that are setup as with newsoftserial library as a new software Serial in the following way:

0 RX – 3 TX
1 TX – 2 RX

Then , with the following code, I try to print what has been read by the created software Serial

#include <NewSoftSerial.h>

const byte rxPin = 2; 
const byte txPin = 3; 

NewSoftSerial bikeSerial(2, 3); 

void setup()  
{
  Serial.begin(9600);
  bikeSerial.begin(9600); 

  Serial.print(0x2D,HEX);
  delay(50);
  if (bikeSerial.available()) 
     {      
      Serial.print(bikeSerial.read(),HEX);
     }
  }

void loop()
{
}

The problem is that information read doesn’t correspond to the information sent. And I don’t know if there’s a problem with the printing or really in the communication.

Nearly right!

I’m assuming that your signal INTO the Arduino is connected to pin 2.

Try this…

#include <NewSoftSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

NewSoftSerial bikeSerial(rxPin,txPin);

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

void loop()
{
Serial.print(0x2D,HEX);
  delay(150);
  if (bikeSerial.available()>0)
     {
      Serial.print(bikeSerial.read(),HEX);
     }
}

That’s not tested… but should be closer.

Be sure that whatever’s sending to your Arduino over bikeSerial is sending at 9600.

There’s more at…

http://sheepdogguides.com/dt4t.htm

You may need in setup…

digitalWrite(rxPin,HIGH);

… to connect the internal pull up resistor.

Thanks tkbyd ,

I just tested the code and I added also activated the pull-up for Rx unfortunately the result is the same with or without the pull-up: I send “2E” and in return I always receive (at least it’s what is displayed) “32”

#include <NewSoftSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

NewSoftSerial bikeSerial(2,3);


void setup()
{
  Serial.begin(9600);
  bikeSerial.begin(9600);
  digitalWrite(2,HIGH);
  Serial.print(0x2D,HEX);
  delay(150);
  if (bikeSerial.available()>0)
     {
      Serial.print(bikeSerial.read(),HEX);
     }
 }

void loop()
{

}

PS: I couldn’t use the constants for the calling of the constructor of NewSoftSerial , there’s a compilation error when I try to do it.

I couldn't use the constants for the calling of the constructor of NewSoftSerial , there's a compilation error when I try to do it.

Try....

#define rxPin 2//no ; here
#define txPin 3//no ; here

Again... not tested. Tested code is in the webpage I sent you to.

How are you sending serial data to the Arduino? Typically code that reads serial data goes in loop, not setup.

In this code, you are only reading one byte of serial data, one time, regardless of how much is sent. If the 2E you are sending is sent as "2E", the HEX value for the "2" is 32.

Thanks Paul ,

You’re right . I was sending a chain and reading only the first byte . I fixed the code and now is working well.
I added the code at the setup section to be executed once (and prevent the continuous feeding of the hardware serial to reprint)

#include <NewSoftSerial.h>

const byte rxPin = 2; 
const byte txPin = 3;
int countOffset =0;
int bytesRead=0;

NewSoftSerial nsSerial(2,3); 

void setup()  
{
  Serial.begin(19200);
  nsSerial.begin(19200); 

  Serial.print(0x2A,BYTE); //
  delay(50); //  
  countOffset = nsSerial.available(); 
     if ( countOffset > 0 ) 
     {   
       while(bytesRead < countOffset)
        
      {
        Serial.print(nsSerial.read(),HEX);
        bytesRead ++;
      }
     }
     Serial.println();
     Serial.print(countOffset);Serial.println(" bytes read");
  }

void loop()
{
}

Regards

The setup function executes once. The amount of serial data that is present when nsSerial.available() is called is all that will [u]ever[/u] be read. Is this really what you want?

@PaulS Yes , I cross wired Tx/Rx between the hardware uart on Arduino and the created NewSoftSerial one. The idea was to send a byte to the hardware UART and then retrieve it on the software one and print it (as the fact of printing is sending again new bytes I wanted to stop there). The only purpose was to verify that data are well sent and read at 19200 to discard this issue in other development. Probably there're better ways but I thought of this one.

Rolo,

When you write

Serial.print(0x2D, HEX);

you are sending the string "2D" out the serial port. That's the two characters ASCII '2' followed by ASCII 'D'.

Your read code is only reading one or these characters -- the '2'. The ASCII code for '2' is 50 or hex 0x32. You chose to print it out in HEX, and that's why you're getting a "32".

Mikal