0

I am trying to write the ID found over RFID to a file - but when I send the bytes to the file, it gives the wrong ID.

I am adding the write file function to this example - where the ID comes from an RFID tag, and when I open the file, the format is completely different to what I have output to my terminal.

I am working on raspberry pi with 125KHz RFID sheild.

Here is the example code with my add-ons:

   /*  
     *  RFID 125 kHz Module
     *  
     *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
     *  http://www.libelium.com 
     *  
     *  This program is free software: you can redistribute it and/or modify 
     *  it under the terms of the GNU General Public License as published by 
     *  the Free Software Foundation, either version 3 of the License, or 
     *  (at your option) any later version. 
     *  a
     *  This program is distributed in the hope that it will be useful, 
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     *  GNU General Public License for more details.
     *  
     *  You should have received a copy of the GNU General Public License 
     *  along with this program.  If not, see http://www.gnu.org/licenses/. 
     *  
     *  Version:           2.0
     *  Design:            David Gascón 
     *  Implementation:    Marcos Yarza & Luis Martin
     */

    //Include ArduPi library
    #include "arduPi.h"


    int led = 13;
    byte data_1 = 0x00;
    byte data_2 = 0x00;
    byte data_3 = 0x00;
    byte data_4 = 0x00;
    byte data_5 = 0x00;
    int val = 0;

    void setup(){
        // Start serial port 19200 bps
        Serial.begin(19200);
        pinMode(led, OUTPUT);

        delay(500);

        // Setting Auto Read Mode - EM4102 Decoded Mode - No password
        // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
        Serial.print(0xFF,BYTE);
        Serial.print(0x01,BYTE);
        Serial.print(0x09,BYTE);
        Serial.print(0x87,BYTE);
        Serial.print(0x01,BYTE);
        Serial.print(0x03,BYTE);
        Serial.print(0x02,BYTE);
        Serial.print(0x00,BYTE);
        Serial.print(0x10,BYTE);
        Serial.print(0x20,BYTE);
        Serial.print(0x30,BYTE);
        Serial.print(0x40,BYTE);
        Serial.print(0x37,BYTE);

        delay(500);
        Serial.flush();

        printf("\n");

        printf("RFID module started in Auto Read Mode\n");

    }

    void loop(){

        printf("Waiting card...\n");
        val = Serial.read();
        while (val != 0xff){
            val = Serial.read();
            delay(1000);
        }

        // Serial.read();    // we read ff
        Serial.read();    // we read 01
        Serial.read();    // we read 06
        Serial.read();    // we read 10
        data_1 = Serial.read();    // we read data 1
        data_2 = Serial.read();    // we read data 2
        data_3 = Serial.read();    // we read data 3
        data_4 = Serial.read();    // we read data 4
        data_5 = Serial.read();    // we read data 5
        Serial.read();    // we read checksum


        // LED blink
        for(int i = 0;i < 4;i++){
            digitalWrite(led,HIGH);
            delay(500);
            digitalWrite(led,LOW);
            delay(500);
        }

        // Printing the code of the card
        printf("\n");
        printf("EM4100 card found - Code: ");
        printf("%x",data_1);
        printf("%x",data_2);
        printf("%x",data_3);    
        printf("%x",data_4);
        printf("%x",data_5);

        printf("\n\n");
    }

int writeFile(){
    ofstream myfile;
    osstringstream mystream;
    myfile.open(example);
    mystream << data_1;
    mystream << data_2;
    mystream << data_3;
    mystream << data_4;
    mystream << data_5;
    myfile << mystream.str();
    myfile.close();
    return 0;
}

int main (){
    setup();
    while(1){
        loop();
        writefile();
    }
    return (0);
}

It compiles and runs, but when I check my example file, it gives a load of rubbish. I have tried a few other tweaks on the write file, but nothing seems to work. I am a bit lost so any help would be amazing!

Gabriel
  • 763
  • 1
  • 10
  • 28
jonny maguire
  • 13
  • 2
  • 6
  • What is the expected output? `FF 01 09 87 01 03 02 00 10 20 30 40 37`? – ichramm Aug 11 '15 at 19:11
  • What about opening the file with an hex editor? – ichramm Aug 11 '15 at 19:13
  • possible duplicate of [Getting a buffer into a stringstream in hex representation:](http://stackoverflow.com/questions/7639656/getting-a-buffer-into-a-stringstream-in-hex-representation) – Michael Roland Aug 11 '15 at 20:27
  • the output is a 8 varchar RFID from one of the tags, i believe the that @ichramm is just the initialising of the reader. something like i.e 5E 77 O4 DE etc – jonny maguire Aug 11 '15 at 20:48
  • and i had a look at the duplicate it is similar @Michael but im new to c++ thanks – jonny maguire Aug 11 '15 at 20:48
  • Well, you seem to ask why you are not getting the hexadecimal string representation of data_1 to data_5 in the file. You currently write those values to the string in binary representation (so the byte/unsigned char values are treated as ASCII(?) characters), hence you need to apply formatting modifies to the string stream in order to print the hexadecimal string representation of the value and that's exactly what I linked as duplicate. Being new to C++ is not really an excuse for not doing proper research before posting questions. – Michael Roland Aug 12 '15 at 00:59

0 Answers0