Go Down

Topic: Problems with the SoftwareSerial Library (Read 1 time) previous topic - next topic

Koop

Hi all,
I have been attempting to use an RFID Tag reader to read an RFID tag and send the data (i.e. The tag's serial number) to the Serial Port of my computer.  To receive data from the reader, you need to use serial, and I have been using the SoftwareSerial library to communicate with it for obvious reasons.  However, it has not been working as planned.  What was supposed to happen in the Serial Monitor was something like this:

Code: [Select]

Serial Ready
RFID Ready
Tag: [(the tag's serial number)]


However what is really happening is this:
Code: [Select]

Serial Ready
RFID Ready
Tag: [ÿÿÿÿÿÿÿÿÿÿÿÿ]
Tag: [ÿÿÿÿÿÿÿÿÿÿÿÿ]
Tag: [ÿÿÿÿÿÿÿÿÿÿÿÿ]
Tag: [ÿÿÿÿÿÿÿÿÿÿÿÿ]
... and so on


The 'tag' just repeats itself again and again.  I have a suspicion that my SoftwareSerial library is not working somehow because when you add the library, the 'SoftwareSerial.h' text does not turn orange as it normally does with libraries, and all functions to do with SoftwareSerial (that are solely to do with SoftwareSerial) are not turned orange either.  I even tried with the example code in the IDE and the same thing happened.  Does anyone know what is going on?  I think it may have something to do with the way I installed the IDE or maybe its just 1.0.1.

Thanks,
Koop

dxw00d

#1
Jul 11, 2012, 02:05 pm Last Edit: Jul 11, 2012, 02:12 pm by dxw00d Reason: 1
The 'ÿ' represents -1, the value returned when you try to read from (Software)Serial and there is nothing available to read. Are you checking SoftwareSerial.available() before reading?

Quote
the 'SoftwareSerial.h' text does not turn orange

The keywords.txt file for SoftwareSerial still refers to NewSoftSerial, so I wouldn't worry about that.

AWOL

Posting code usually helps clear things up.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Koop

Here's the code.  Its the sample code provided with the datasheet:

Code: [Select]
/*------------------------------------------------------------------
This is a sample code for RDM630 RFID reader by Spekel(Spekel.se)
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
http://creativecommons.org/licenses/by-nc-sa/3.0/
-------------------------------------------------------------------*/
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
char code[20];
int val = 0;
int bytesread = 0;
//------------------------------------
//create a Serial object RFID
SoftwareSerial RFID= SoftwareSerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  Serial.println("Serial Ready");
  RFID.begin(9600);
  Serial.println("RFID Ready");
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
}
void loop()
{
  val = 0;
  bytesread = 0;

  while(bytesread < 12)
  {
    // read 12 digit code
    val = RFID.read();
    if(val == 3)
    { // if header or stop bytes before the 10 digit reading
      break; // stop reading
    }

    if(val != 2)
    {
      code[bytesread] = val; // add the digit
      bytesread++; // ready to read next digit
      code[bytesread] = '\0'; // add the NULL
    }
  }

  if(bytesread >= 12)
  { // if 12 digit read is complete
    Serial.print("Tag: [");
    for(int i=0; code[i]!='\0' ; i++)
    {
      Serial.print(code[i]);
    }
    Serial.println("]"); //print the whole 13 bytes
  }
}
[code]

Oh, and here is the datasheet:
http://australianrobotics.com.au/sites/default/files/RDM630-Spec..pdf
[/code]

Koop

Oh, thanks dxW00d, I'll try that tomorrow (its getting kinda late here).

Koop

AWOL

Yes, dxw00d nailed it - reading data that isn't there.
The "read" method just gets the next character from the read buffer, or returns -1 if there's nothing there.
You need either to check that you have at least as many characters as you expect before reading them all, or check before each character.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
SoftwareSerial RFID= SoftwareSerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  Serial.println("Serial Ready");
  RFID.begin(9600);
  Serial.println("RFID Ready");
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

You told the SoftwareSerial instance that the rxPin and txPin pins were its to diddle with as needed. Why are you then diddling with them?

There is no call to RFID.available() to determine whether there is anything to read, or not. You just assume, incorrectly, that there is.

Koop

Hi!  Thanks to all of you for your help, dxw00d in particular.  I included a check to ensure my RFID Reader was actually sending me data, and all works fine.

Thanks again!
Koop

jeperohe

Hello,
I have the same problem as Koop and not fix it. As you say we should add the check serial port in the loop ()? Koop, could you post the area you modified the original code. Thank you!

Go Up