Go Down

Topic: Implementing Commands to control RFID reader (Read 1 time) previous topic - next topic

mmi698

Hi,

I am new to programming in general, and I have hit a bit of a road block.

I am trying to communicate with an RFID module via a command protocol. Please see page 12 on the attached PDF. This is something which I don not fully understand, despite my endless attempts to get to understand it.
My question is: What way should I send the data to the reader? Should it be something like mySerial.println("MOF") and wait for response. Or should I write each character individually? This then brings up the question of what <crn> is, and how to write to serial?


I am kindof struggling with this topic at the moment, so if someone could offer any help or some relative literature which I could go through and maybe come back with more intelligible questions.

Many thanks, Micheal.


Ralph_S_Bacon

#1
Feb 15, 2016, 08:42 pm Last Edit: Feb 15, 2016, 08:52 pm by Ralph_S_Bacon
I am trying to communicate with an RFID module via a command protocol...
Perhaps you could post your sketch here so others can see what you have done so far.

Normally, you use the standard 'Serial' library and transmit and receive using the functions built in, which you have already discovered (partly) anyway.

For example, to read a serial port stream of data you might use:
Code: [Select]
if (Serial.available() > 0) {
  char dataChar = Serial.read();
}

You can monitor and send data back to the Arduino using the serial monitor window. Have you done this?

Finally, <crn> is just Carriage Return and is decimal value 13 or 0x0D in hexadecimal (basically much the same as hitting the Enter key after typing something on your keyboard. You could do this with Serial.write(13), for example. Usually (and hugely simplified), Serial.print is used to send the actual (ascii) character, whereas Serial.write is used to send a value such as command codes.

But post your code (so far) and we may be able to move forward.
My YouTube Arduino Videos: https://www.youtube.com/RalphBacon
Arduino and electronics videos designed to get your project off the ground.

Grumpy_Mike

Note Serial.println adds a carriage return onto what you write automatically.

mmi698

Thanks for your input Ralph,

At the moment I am switching the RFID module on and off by connecting and disconnecting the power. When power is applied it automatically reads an ID tag and sends down the soft serial pin. This is how I have used the unit to date. Below is my code which I am using for reading the code to date.

Code: [Select]
char rfidData[BUFSIZE];  // Buffer for incoming data
    rfidData[0] = 0;         // Clear the buffer
    char offset = 0;         // Offset into buffer      

    while (mySerial.available()) {
        byte C = mySerial.read();
        rfidData[offset] = C;
        offset++;
        int Count = offset;
        Serial.print(C);
        Serial.println("LOOKING FOR TAG");
        }
        if(offset == 17){
          digitalWrite(YLED, HIGH);


As I have never needed to have a 2-way conversation with the RFID module, so I do not have any code written which transits data(only revives).

Your last post has joined a lot of dots in my head. From this, I think I might be able to attempt to Witt some sort of code.
Would something like this be the right way to go?

Code: [Select]
mySerial.write(0x4d);
mySerial.write(0x4f);
mySerial.write(0x46);
mySerial.write(13);
millis() = setime;
while(millis() - setime < 1000){
 while (mySerial.available()) {
  byte C = mySerial.read();
  Serial.print(C);
 }}


So (cr) from the ascii table is the same as <crn> from the FRID module data sheet (Serial.write(13))?

Many Thanks, Micheal.

Ralph_S_Bacon

Does the code you posted to READ the RFID actually work?

Without re-reading that pdf you posted  :o I can only guess at what the command codes you are transmitting in the first few lines of your next piece of code but yes, that's the way to send them.


I'm not sure what this line of code is supposed to do:

Code: [Select]
while(millis() - setime < 1000)
because when there is no more data to read the .available() while loop will end naturally.

And yes, from what I can work out from the pdf, the <crn> is what they mean by carriage return which is indeed Serial.write(13).

Just a little suggestion here, as it's clear you're fairly new to programming. Break this project into tiny little parts so you can get the reading / writing / sending commands or whatever sorted out independently as possible. Lumping all your code into one monolithic block will just obfuscate any errors and confuse the hell out of people on this forum!

Once each part is proved to be working then you can put it all back together.
My YouTube Arduino Videos: https://www.youtube.com/RalphBacon
Arduino and electronics videos designed to get your project off the ground.

Whandall

If you send the command as strings, it's more readable (even with the string in PROGMEM)
Code: [Select]
mySerial.print(F("MOF"));
is the same as
Code: [Select]
mySerial.write(0x4d);
mySerial.write(0x4f);
mySerial.write(0x46);
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up