Pretending multiple values

Hey guys i gave little problem. I work on Rfid reader system. Everytime i attach card to reader serial monitor start writing values as long as I remove the card. How do i prevent that?

#include <SoftwareSerial.h>

const int BUFFER_SIZE = 14; // RFID DATA FRAME FORMAT: 1byte head (value: 2), 10byte

const int DATA_SIZE = 10; // 10byte data (2byte version + 8byte tag)
const int DATA_VERSION_SIZE = 2; // 2byte version (actual meaning of these two bytes

const int DATA_TAG_SIZE = 8; // 8byte tag
const int CHECKSUM_SIZE = 2; // 2byte checksum

uint8_t buffer[BUFFER_SIZE]; // used to store an incoming data frame
int buffer_index = 0;

SoftwareSerial ssrfid = SoftwareSerial(2,8);

void setup() {
Serial.begin(9600);
ssrfid.begin(9600);
ssrfid.listen();
Serial.println("INIT DONE");
}

void loop() {

if (ssrfid.available() > 0){
bool call_extract_tag = false;

int ssvalue = ssrfid.read(); // read
if (ssvalue == -1) { // no data was read
return;
}
if (ssvalue == 2) { // RDM630/RDM6300 found a tag => tag incoming
buffer_index = 0;
} else if (ssvalue == 3) { // tag has been fully transmitted
call_extract_tag = true; // extract tag at the end of the function call
}
if (buffer_index >= BUFFER_SIZE) { // checking for a buffer overflow (It's very

Serial.println("Error: Buffer overflow detected!");
return;
}

buffer[buffer_index++] = ssvalue; // everything is alright => copy current value to buffer

if (call_extract_tag == true) {
if (buffer_index == BUFFER_SIZE) {

unsigned tag = extract_tag();

}
else { // something is wrong... start again looking for preamble (value: 2)
buffer_index = 0;
return;
}
}
}
}

unsigned extract_tag() {
uint8_t msg_head = buffer[0];
uint8_t *msg_data = buffer + 1;
uint8_t *msg_data_version = msg_data;
uint8_t *msg_data_tag = msg_data + 2;
uint8_t *msg_checksum = buffer + 11; // 2 byte
uint8_t msg_tail = buffer[13];
long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);

 Serial.println(tag);

return tag;
}
long hexstr_to_value(char str, unsigned int length) { // converts a hexadecimal value
char
copy = malloc((sizeof(char) * length) + 1);
memcpy(copy, str, sizeof(char) * length);
copy[length] = '\0';
// the variable "copy" is a copy of the parameter "str". "copy" has an additional
long value = strtol(copy, NULL, 16); // strtol converts a null-terminated string to
free(copy); // clean up
return value;

}

What do you mean? If English is not your first language try using...

"prevent that"...

I want to prevent multiple values print , if there is same card it will print olny once.

ouuu sorry prevent

Pcode

if last code != this code
{
  print the code
  last code = this code
}

I tried that but it doesn't write anything

What "that" did you try? We need to see actual code.

I can't read your code, the formatting makes my head spin. Please fix it. The forum guidelines have links to instructions for using code tags. Auto format your code using ctrl-T in the IDE before you upload.

#include <SoftwareSerial.h>

const int BUFFER_SIZE = 14; // RFID DATA FRAME FORMAT: 1byte head (value: 2), 10byte

const int DATA_SIZE = 10; // 10byte data (2byte version + 8byte tag)
const int DATA_VERSION_SIZE = 2; // 2byte version (actual meaning of these two bytes

const int DATA_TAG_SIZE = 8; // 8byte tag
const int CHECKSUM_SIZE = 2; // 2byte checksum

uint8_t buffer[BUFFER_SIZE]; // used to store an incoming data frame
int buffer_index = 0;

SoftwareSerial ssrfid = SoftwareSerial(2,8);

void setup() {
Serial.begin(9600);
ssrfid.begin(9600);
ssrfid.listen();
Serial.println("INIT DONE");
}

void loop() {

if (ssrfid.available() > 0){
bool call_extract_tag = false;

int ssvalue = ssrfid.read(); // read
if (ssvalue == -1) { // no data was read
return;
}
if (ssvalue == 2) { // RDM630/RDM6300 found a tag => tag incoming
buffer_index = 0;
} else if (ssvalue == 3) { // tag has been fully transmitted
call_extract_tag = true; // extract tag at the end of the function call
}
if (buffer_index >= BUFFER_SIZE) { // checking for a buffer overflow (It's very

Serial.println("Error: Buffer overflow detected!");
return;
}

buffer[buffer_index++] = ssvalue; // everything is alright => copy current value to buffer

if (call_extract_tag == true) {
if (buffer_index == BUFFER_SIZE) {

unsigned tag = extract_tag();

}
else { // something is wrong... start again looking for preamble (value: 2)
buffer_index = 0;
return;
}
}
}
}

unsigned extract_tag() {
uint8_t msg_head = buffer[0];
uint8_t *msg_data = buffer + 1;
uint8_t *msg_data_version = msg_data;
uint8_t *msg_data_tag = msg_data + 2;
uint8_t *msg_checksum = buffer + 11; // 2 byte
uint8_t msg_tail = buffer[13];
long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);
long old;

if(old!= tag){
Serial.println(tag);
old = tag;
}

return tag;
}
long hexstr_to_value(char str, unsigned int length) { // converts a hexadecimal value
char
copy = malloc((sizeof(char) * length) + 1);
memcpy(copy, str, sizeof(char) * length);
copy[length] = '\0';
// the variable "copy" is a copy of the parameter "str". "copy" has an additional
long value = strtol(copy, NULL, 16); // strtol converts a null-terminated string to
free(copy); // clean up
return value;

}

Auto format your code. Then upload it with code tags!

in these line
unsigned extract_tag() {
uint8_t msg_head = buffer[0];
uint8_t *msg_data = buffer + 1;
uint8_t *msg_data_version = msg_data;
uint8_t *msg_data_tag = msg_data + 2;
uint8_t *msg_checksum = buffer + 11; // 2 byte
uint8_t msg_tail = buffer[13];
long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);
long old;

if(old!= tag){
Serial.println(tag);
old = tag;
}

return tag;
}

Auto format your code. Then upload it with code tags!

#include <SoftwareSerial.h>



const int BUFFER_SIZE = 14;  // RFID DATA FRAME FORMAT: 1byte head (value: 2), 10byte

const int DATA_SIZE = 10;         // 10byte data (2byte version + 8byte tag)
const int DATA_VERSION_SIZE = 2;  // 2byte version (actual meaning of these two bytes

const int DATA_TAG_SIZE = 8;  // 8byte tag
const int CHECKSUM_SIZE = 2;  // 2byte checksum

uint8_t buffer[BUFFER_SIZE];  // used to store an incoming data frame
int buffer_index = 0;

SoftwareSerial ssrfid = SoftwareSerial(2, 8);

void setup() {
  Serial.begin(9600);
  ssrfid.begin(9600);
  ssrfid.listen();
  Serial.println("INIT DONE");
}


void loop() {

  if (ssrfid.available() > 0) {
    bool call_extract_tag = false;

    int ssvalue = ssrfid.read();  // read
    if (ssvalue == -1) {          // no data was read
      return;
    }
    if (ssvalue == 2) {  // RDM630/RDM6300 found a tag => tag incoming
      buffer_index = 0;
    } else if (ssvalue == 3) {  // tag has been fully transmitted
      call_extract_tag = true;  // extract tag at the end of the function call
    }
    if (buffer_index >= BUFFER_SIZE) {  // checking for a buffer overflow (It's very

      Serial.println("Error: Buffer overflow detected!");
      return;
    }

    buffer[buffer_index++] = ssvalue;  // everything is alright => copy current value to buffer

    if (call_extract_tag == true) {
      if (buffer_index == BUFFER_SIZE) {

        unsigned tag = extract_tag();


      } else {  // something is wrong... start again looking for preamble (value: 2)
        buffer_index = 0;
        return;
      }
    }
  }
}


unsigned extract_tag() {
  uint8_t msg_head = buffer[0];
  uint8_t *msg_data = buffer + 1;
  uint8_t *msg_data_version = msg_data;
  uint8_t *msg_data_tag = msg_data + 2;
  uint8_t *msg_checksum = buffer + 11;  // 2 byte
  uint8_t msg_tail = buffer[13];
  long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);
  long old;

  if (old != tag) {
    Serial.println(tag);
    old = tag;
  }




  return tag;
}
long hexstr_to_value(char *str, unsigned int length) {  // converts a hexadecimal value
  char *copy = malloc((sizeof(char) * length) + 1);
  memcpy(copy, str, sizeof(char) * length);
  copy[length] = '\0';
  // the variable "copy" is a copy of the parameter "str". "copy" has an additional
  long value = strtol(copy, NULL, 16);  // strtol converts a null-terminated string to
  free(copy);                           // clean up
  return value;
}
1 Like

variable 'old' is dynamically allocated because it's in a function. It's not remembered between calls. You need to declare it with the 'static' keyword.

Thank you, it works perfectly

1 Like

See also: R503 and RDM6300 readers