Using the RDM630 RFID Tag Reader and NewSoftwareSerial library

See what I mean about attracting flak? :wink:

AWOL:
See what I mean about attracting flak? :wink:

Yep :wink: I didn't realise that Goto was such a taboo! It must be drilled into your heads at uni!

I'm changing their pinModes (even though its not really needed) because thats what it does in the SoftwareSerial tutorial:

It's on the internet. It must be right. Not in this case:

SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) : 
  _rx_delay_centering(0),
  _rx_delay_intrabit(0),
  _rx_delay_stopbit(0),
  _tx_delay(0),
  _buffer_overflow(false),
  _inverse_logic(inverse_logic)
{
  setTX(transmitPin);
  setRX(receivePin);
}

And, what does setTX do?

void SoftwareSerial::setTX(uint8_t tx)
{
  pinMode(tx, OUTPUT);
  digitalWrite(tx, HIGH);
  _transmitBitMask = digitalPinToBitMask(tx);
  uint8_t port = digitalPinToPort(tx);
  _transmitPortRegister = portOutputRegister(port);
}

And setRX?

void SoftwareSerial::setRX(uint8_t rx)
{
  pinMode(rx, INPUT);
  if (!_inverse_logic)
    digitalWrite(rx, HIGH);  // pullup for normal logic!
  _receivePin = rx;
  _receiveBitMask = digitalPinToBitMask(rx);
  uint8_t port = digitalPinToPort(rx);
  _receivePortRegister = portInputRegister(port);
}

So, the pin modes have already been set when you diddle with them.

PaulS:

I'm changing their pinModes (even though its not really needed) because thats what it does in the SoftwareSerial tutorial:

It's on the internet. It must be right. Not in this case:

SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) : 

_rx_delay_centering(0),
 _rx_delay_intrabit(0),
 _rx_delay_stopbit(0),
 _tx_delay(0),
 _buffer_overflow(false),
 _inverse_logic(inverse_logic)
{
 setTX(transmitPin);
 setRX(receivePin);
}




And, what does setTX do?


void SoftwareSerial::setTX(uint8_t tx)
{
 pinMode(tx, OUTPUT);
 digitalWrite(tx, HIGH);
 _transmitBitMask = digitalPinToBitMask(tx);
 uint8_t port = digitalPinToPort(tx);
 _transmitPortRegister = portOutputRegister(port);
}




And setRX?
void SoftwareSerial::setRX(uint8_t rx)
{
pinMode(rx, INPUT);
if (!_inverse_logic)
digitalWrite(rx, HIGH); // pullup for normal logic!
_receivePin = rx;
_receiveBitMask = digitalPinToBitMask(rx);
uint8_t port = digitalPinToPort(rx);
_receivePortRegister = portInputRegister(port);
}

So, the pin modes have already been set when you diddle with them.

Well it was on the official Arduino page, which is supposed to be moderated by experts like you and your counterparts, so it's not like I grabbed it off Wikipedia and stuck it in my code. Either way, nowhere have I read that you don't need to do that (At least anywhere on here), so obviously I'm not the only one, and no harm was done - the code works perfectly fine - so I don't think that any of this matters.

Well it was on the official Arduino page, which is supposed to be moderated by experts like you and your counterparts

The official Arduino page contains many, many errors. Not a one of us "experts" or moderators can change the official Arduino page.

I didn't mean to come across too hard on the issue. The pins belong to the instance (and it doesn't matter what the instance is an instance of), so they should not be diddled with by you (or any one else). Only the class that you give them to should diddle with them.

You can, of course, look at the code, like I did, to see what is necessary, and what isn't, in the future.

(That has to be a record for the number of commas I've ever put in one sentence.)

I didn't mean to come across too hard on the issue. The pins belong to the instance (and it doesn't matter what the instance is an instance of), so they should not be diddled with by you (or any one else). Only the class that you give them to should diddle with them.

You can, of course, look at the code, like I did, to see what is necessary, and what isn't, in the future.

Thanks for that. Its hard to 'read' a person's emotions with only text to go by, and I thought that you were being one of those annoying nit-pickers, but you weren't so thats good :slight_smile:

The official Arduino page contains many, many errors. Not a one of us "experts" or moderators can change the official Arduino page.

Well that's not good is it :confused: It should be checked for errors more frequently... and all examples I've ever seen do the same thing I did so it must be stemming from that one coding mistake. Thanks for all your help. I'll be sure to read the header files before I do anything with libraries again.

Thanks to you all,
Koop

It must be drilled into your heads at uni!

A bit condescending.
When I went to Uni there was no instruction about code. We had to learn it ourselves.

When I taught at Uni, I never introduced it as a valid instruction.

Some of us never attended a university.

Me neither.

At my first technical programming job I handed in my first piece of code to my supervisor. He said "rewrite it without the gotos". I did that, and haven't used them since.

I can think of one place where a goto might be useful, and that is in the regular expression library where an expert (Roberto Ierusalimschy) used it in one place in order to avoid consuming the stack when doing tail recursion. This is a guy who knows what he is doing. He is an associate professor of Informatics and a compiler-writer (Lua).

If you are trying to avoid using the stack, when doing tail recursion, fair enough.

For other cases, you don't need goto, and they obscure what the code is doing.

A bit condescending.

Some of us never attended a university.

Sorry, Grumpy_Mike, AWOL and dxw00d! In no means was that meant to be condescending, I just assumed that seeing as though you guys are so good at this, and you knew to never, EVER, use a goto (except in the circumstance said by Nick) you had some form of tertiary education on the matter. Being a schoolkid, I've never experienced uni or TAFE or community college or any tertiary education, so I have no idea of how or what they teach you there. It was just an (obviously uneducated) guess :stuck_out_tongue:

My problem was that I was taught BASIC at school - the old-fashioned version that required line numbers and pretty much only had for loops and GOTOs and GOSUBs as control structures.
Took me a while to get out of the habit when I learned ALGOL.
Glad I kicked it though.

AWOL:
My problem was that I was taught BASIC at school - the old-fashioned version that required line numbers and pretty much only had for loops and GOTOs and GOSUBs as control structures.
Took me a while to get out of the habit when I learned ALGOL.
Glad I kicked it though.

I just started a computer technology class, and lucky for me, BASIC has come a long way in the past couple of years, so I probably (more like hopefully) won't have that problem. And know that I know, I should be able to, even if I still 'need' to use gotos, I should be able to shake off the habit and keep it away :stuck_out_tongue:

My first language was Minitran, a variant of Fortran. Early versions of that used line numbers, which you did GOTO.

Can't say I miss it. Like AWOL, I moved onto a structured language (NEAT/3 rather than ALGOL) and haven't used them since (unless writing assembler perhaps, which doesn't happen very often).

Hello,
Here is the code (pro publico bono) which takes into account the official frame format from the sensor. We are synchronizing with the frame. We do not rely on the fact that every 13 characters there should be a new card number. Enjoy. Code should compile on Arduino Mega, for other Arduinos you have to replace Serial1 with instance of SoftwareSerial class (2 places in the code below). It works both with software and hardware UART.

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);  // you can use softwareserial instead
}
unsigned char card[20];

// let's use the data frame format from RDM630 specification to avoid mismatch
// [0x02] [ 10 ASCII CHARACTERS ] [CHECKSUM] [0x03]

void loop()
{
  static unsigned char i=0;

  while(Serial1.available())
  {
    char c= Serial1.read();
    if(i==0)
    {
      //we are waiting for the first character of the next RFID tag. It has to be byte of value 2.
      //we ignore everything before we get 2.
      if(c==2) 
      {
        card[i]=c;
        i++;
      }
    }
    else
    {
      //we are already into reading the ID so we do not ignore any more characters ...
      card[i]=c;
      i++;
      if((i>18) || (c==3))  // if we get a terminating byte (value=03 according to RDM630Reader specification)...
      {                            // ...or we are nearly buffer overflow for some reason
        decode();
        i=0;
      }
    }
  }
}

void decode()
{
  unsigned char p;
  //you  can add checksum calculation here to verify if the tag is correct
  // CHECKSUM: card 10byte DATA entire do XOR operation

  for(p=0;p<19;p++) // let's print the whole buffer however the tag ID itself is from card[1]  through[11]
  {
    Serial.print(card[p],HEX);
    Serial.print(" ");
  }
  Serial.println();
}

Moderator edit: quote tags removed, code tags added.

I'm guessing that what you posted doesn't look anything like what is stored on your computer, because you posted it wrong. Code tags (use the # icon), not quote tags.

Mangling code during posting doesn't help anyone.

I don't want to start a flame war here, but there are some rare occasions where using gotos is justified (OP's example was obviously not one of those). I have sometimes used gotos as a kind of a exception throwing mechanism where it makes handing error conditions simpler. Something like:

int foo()
{
    int ret;
    if (!(ret = do_something())) goto err;
    
    ... 

    if (!(ret = do_something_else())) goto err;
    if (!(ret = do_something_more())) goto err; 

    return OK;

  err:
    rollback();
    show_error_message();
    return ERR;
}

That was what try/throw/catch was added for in recent times. Not supported under the compile options used by the IDE however.

I tend to use goto exception throwing in languages that don't support actual try/catch/throw mechanism, such as in Perl and vanilla C. I have to say that try/catch/throw exception handling is not any of my favorite features. It is especially irritating in Java, which forces to catch exceptions is cases where I would rather just let the program crash.