[Help] Wiegand Sending Data to controller

hello,

I am using brad-anton source code for sending binary card data to controller.
input is through serial console.
when I type 26 bit card data in binary, output from below code is adding leading four zeros
Need help in fixing.

I need to send actual 26bit binary data to controller

Link to source (original author)

brad-anton VertX

void writeCard(volatile unsigned long sendValue) {
   Serial.println("[-] Sending Data:");
   Serial.print("\t");
   for (short x=26; x>=0; x--) {
     if ( bitRead(sendValue,x) == 1 ) {
       Serial.print("1"); 
       //digitalWrite(VX_D1, LOW);
       delayMicroseconds(34);
       //digitalWrite(VX_D1, HIGH);
     } else if ( bitRead(sendValue,x) == 0 ) {
       Serial.print("0");
       //digitalWrite(VX_D0, LOW);
       delayMicroseconds(34);
       //digitalWrite(VX_D0, HIGH);
     }
     delay(2);
   }
   Serial.println();
}

Thanks
Sudheer

Hi, can you give some sample output. I don’t see anything in that code that would send more than 26 digits.

Also please note that you should always post your code in code tags so it looks like this. Use the </> icon.

Paul

I think this will work and is shorter

 for (short x=26; x>=0; x--) {
     if ( bitRead(sendValue,x) == 1 ) {
       Serial.print('1'); 
     } 
     else  {
       Serial.print('0);
     }
     delayMicroseconds(34);

     // etc

I wonder do you need single quotes around the 1 and the 0

This might also work

 for (short x=26; x>=0; x--) {
     Serial.write(bitRead(sendValue,x) );
     delayMicroseconds(34);
     // etc

If you want to send 26 bits perhaps the FOR should run from 25 to 0 ?

...R

re-written same code to troubleshoot, changed CARD_LEN = 4

here is entire code.

#define CARD_LEN 4
unsigned long cardValue = 0;

unsigned  char incomingByte = 0; 
int sInputCount = 0;

void setup()
{
  Serial.begin(9600);
   resetState();
}

void loop()
{  
/* Start Serial In
  
    All this stuff does is accept input via the serial interface
*/
  if (Serial.available() > 0 ) {
    
    incomingByte = Serial.read();
    Serial.print(".");
    delay(10);

   processBin(incomingByte);
    if ( ( sInputCount > 0 ) && ( sInputCount != CARD_LEN ) && ( Serial.peek() == -1 ) ) {
          Serial.println("\n[!] Invalid Input Length!");
          resetState();
        }
     }
  if (sInputCount == CARD_LEN) {
      Serial.println("");
          Serial.println("[-] Sending current Serial Buffer");
          Serial.println("cardValue is "); // for checking
          Serial.print(cardValue);  // for checking
          Serial.println("");
          writeCard(cardValue);
          resetState();
     } else if (sInputCount > CARD_LEN) {
          Serial.println("[!] Sorry, looks like your input was too long.. ");
          resetState();
      }
      
// End Serial In


}
//
void processBin(int byte) {
   if (byte == 49) { // DECIMAL 1 
     sInputCount++; 
     bitSet(cardValue, CARD_LEN - sInputCount);
  } else if (byte == 48) { // DECIMAL 0
     sInputCount++; 
     bitClear(cardValue, CARD_LEN - sInputCount);
    
  } else 

{
      Serial.print("[!] Recieved invalid Value:");
      Serial.println(byte);
      resetState();
     }
}
//
void writeCard(volatile unsigned long sendValue) {
    Serial.println("[-] Sending Data:");
    Serial.print("\t");
    for (short x=CARD_LEN; x>=0; x--) {
      if ( bitRead(sendValue,x) == 1 ) {
        Serial.print("1"); 
        
               
      } else if ( bitRead(sendValue,x) == 0 ) {
        Serial.print("0");
       
              
      }
      delay(2);
    }
   Serial.println("");
}
//
void resetState() {
  Serial.println("[+] Reseting State...");
  delay(10);
  Serial.flush();
  sInputCount=0;
}

attached is screen shot of output.

Decimal value of entered 0011 shows correct, as 3, but in binary one zero is extra !

Robin2: I think this will work and is shorter

 for (short x=26; x>=0; x--) {
     if ( bitRead(sendValue,x) == 1 ) {
       Serial.print('1'); 
     } 
     else  {
       Serial.print('0);
     }
     delayMicroseconds(34);

    // etc




I wonder do you need single quotes around the 1 and the 0

This might also work


for (short x=26; x>=0; x--) {     Serial.write(bitRead(sendValue,x) );     delayMicroseconds(34);     // etc ```

If you want to send 26 bits perhaps the FOR should run from 25 to 0 ?

...R

Tried this, but its reducing digits from right, not zero on last left.

eg (for 4 bit) input 0011 becomes 0001 with out this chane output is 00011

I have posted full code

Following change fixed the extra zero issue ..

 for (short x=CARD_LEN ;x-- ; x>=0) {

Hi,

Same problem as before!

for (short x=CARD_LEN; x>=0; x--) {

should be:

for (short x=CARD_LEN; x>0; x--) {

Paul

technofreek: Following change fixed the extra zero issue ..

 for (short x=CARD_LEN ;x-- ; x>=0) {

That's wrong. Well, ok, it may work, but only by accident. That's the wrong way to write a for-loop, you were just lucky there.

PaulRB: Hi,

Same problem as before!

should be:

for (short x=CARD_LEN; x>0; x--) {

Paul

Input is : 0011

Output is : 0001

PaulRB: That's wrong. Well, ok, it may work, but only by accident. That's the wrong way to write a for-loop, you were just lucky there.

for loop is counting backwards: 4,3,2,1,0 4 is creating this problem

Thanks Paul for pointing at right direction.

working fine with

for (short x=3 ; x>=0 ;x--)