Pages: [1]   Go Down
Author Topic: Problems with the SoftwareSerial Library  (Read 841 times)
0 Members and 1 Guest are viewing this topic.
Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
char CoolThings[] = "Vegemite", "Swimming", "Arduino";
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Serial Ready
RFID Ready
Tag: [(the tag's serial number)]

However what is really happening is this:
Code:
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
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 11, 2012, 07:12:03 am by dxw00d » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posting code usually helps clear things up.
Logged

"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.

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
char CoolThings[] = "Vegemite", "Swimming", "Arduino";
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
/*------------------------------------------------------------------
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]
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
char CoolThings[] = "Vegemite", "Swimming", "Arduino";
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Koop
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46110
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
char CoolThings[] = "Vegemite", "Swimming", "Arduino";
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Palencia (Spain)
Offline Offline
Newbie
*
Karma: 1
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1]   Go Up
Jump to: