2014-01-07 127 views
1

我的Wiegand RFID閱讀器(26bit)有問題。我寫了簡單的Java應用程序,一切都很好。但是例如10次讀取後,它開始移位。 RPi Raspbian的Wiegand時間協議會減慢嗎?Raspberry PI + Wiegand RFID

下面是示例代碼和輸出

package classes; 


import com.pi4j.io.gpio.GpioController; 
import com.pi4j.io.gpio.GpioFactory; 
import com.pi4j.io.gpio.GpioPinDigitalInput; 
import com.pi4j.io.gpio.PinPullResistance; 
import com.pi4j.io.gpio.RaspiPin; 
public class Test { 


    public static char[] s = new char[10000]; 
    static int bits = 0; 

    public static void main(String[] args) { 
     // create gpio controller 
     final GpioController gpio = GpioFactory.getInstance(); 

     // provision gpio pin #02 as an input pin with its internal pull down 
     // resistor enabled 
     final GpioPinDigitalInput pin0 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_UP); 

     final GpioPinDigitalInput pin1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_UP); 

     System.out.println("PINs ready"); 
     Thread th = new Thread(new Runnable() { 

      @Override 
      public void run() { 

       while (true) { 

        if (pin0.isLow()) { // D1 on ground? 
         s[bits++] = '0'; 
         while (pin0.isLow()) { 

         } 

        } 

        if (pin1.isLow()) { // D1 on ground? 
         s[bits++] = '1'; 
         while (pin1.isLow()) { 
         } 

        } 

        if (bits == 26) { 
         bits=0; 

         Print(); 

        } 

       } 

      } 
     }); 
     th.setPriority(Thread.MAX_PRIORITY); 
     th.start(); 
     System.out.println("Thread start"); 

     for (;;) { 
      try { 
       Thread.sleep(500); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

    protected static void Print() { 

     for (int i = 0; i < 26; i++) { 
      System.out.write(s[i]); 

     } 
     System.out.println(); 
     bits = 0; 

    } 

} 

輸出:

10100100111111110110011011 
10100100111111110110011011 
10100100111111110110011011 
10100100111111110110011011 
10100100111111110001101110 
10010011111111011001101110 
10010011111111011001101110 
10010011111111011001101110 
10010011111111011001101110 
10010011111111011001101110 
10010011111111011001101110 

回答

1

你的printf語句可能會造成問題。嘗試存儲數據並在最後打印。 printf往往很慢(它涉及幾個上下文切換)。

此外,似乎你沒有辦法檢測你是否錯過了一點。我會說嘗試一個超時,所以如果你沒有及時得到26位重置你的櫃檯。這樣你就不會循環讀取任何內容,並最終得到錯位的數據。

+0

我現在已經改變了我的代碼爲Java。它正在做同樣的事情 –

+0

同樣的問題 - 我建議在你的while循環中添加一個超時(或其他一些同步機制)。這個代碼看起來幾乎是不確定的。 –

0

我已經完成了這個在C和python上的pi和arduino上。根據我的經驗,@woodrow douglas說您需要捕獲循環中的位或使用中斷(更好),並使用每次接收到一點時增加的超時,然後在確定擁有所有位後將其打印出來(時間到)。

這是我如何使用中斷在arduino上完成的。

void zero(){ 
    bit_count ++; 
    bit_holder = (bit_holder << 1) + 0; //shift left one and add a 0 
    timeout = t; 
} 

void one(){ 
    bit_count ++; 
    bit_holder = (bit_holder << 1) + 1; //shift left one and add a 1 
    timeout = t; 
} 

void loop() { 
    timeout --; 
    if (timeout == 0 && bit_count > 0){ 
    lcd.clear(); 
    lcd.print("Dec:"); 
    lcd.print(bit_holder); 
    lcd.setCursor(0,1); 
    lcd.print("Hex:"); 
    lcd.print(String(bit_holder,HEX)); 

    Serial.print("bit count= "); 
    Serial.println(bit_count); 
    Serial.print("bits= "); 
    Serial.println(bit_holder,BIN); 
    oldbit = bit_holder;    //store previous this value as previous 
    bit_count = 0;      //reset bit count 
    bit_holder = 0;  //reset badge number 

    } 

} 

我從來沒有用C皮上的任何問題,但我沒有得到使用Python,因爲它是不是真正的時間問題。它能夠在Python中工作的唯一方式是使用中斷,並且我管理的讀取率也降到了1比200,但從未完全刪除它。

我最後做的是使用一些C來收集位,然後調用我的Python腳本的位進行處理。

如果你有興趣,這是C代碼我使用:

#include <wiringPi.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <time.h> 

int r1data0 = 6; //pin 22 
int r1data1 = 7; // Pin 7 
// green goes to same as relay 
int r1beep = 0; //pin 11 
int r2data0 = 10; /* P1-24 */ 
int r2data1 = 11; /* P1-26 */ 
//green goes to same as relay 
int r2beep = 0; 
int i1 = 0; 
int i2 = 0; 
//generic variables 
int t = 5; 
int blank; //blank variable to reset bits 
int rel1time = 0; 
int rel2time = 0; 
int rel1 = 5; 
int previoust; 

//reader 1variables 
int r1bits; // Collected bits storage area 
int r1bit_count = 0; //to measure the size of bits 
int oldr1bit_count = 0; //somewhere to store bitcount to send to python 
int r1timeout; //timout to return correct value 
char r1command[40]; 
int r1ret; 

//reader 2 variables 
int r2bits; // Collected bits storage area 
int r2bit_count = 0; //to measure the size of bits 
int oldr2bit_count = 0; 
int r2timeout; //timout to return correct value 
char r2command[40]; 

pthread_t threads; 

void access_denied(int red){ 
    int pin; 
    if(red = 1){pin = r1beep;} 
    if(red = 2){pin = r2beep;} 
    pinMode(pin, OUTPUT); 
    digitalWrite(pin, LOW); 
    delay(300);; 
    digitalWrite(pin, HIGH); 
    delay(200); 
    digitalWrite(pin, LOW); 
    delay(300); 
    digitalWrite(pin, HIGH); 
    delay(200); 
    digitalWrite(pin, LOW); 
    delay(300); 
    digitalWrite(pin, HIGH); 
} 

void *r1python_thread(void *val){ 
    sprintf(r1command,"python access.py r1 %X %X", oldr1bit_count, val); //build python command 
    FILE* file = popen(r1command, "r"); //execute command using popen  
    char buffer[5]; 
    fscanf(file, "%100s", buffer); //read command output 
    pclose(file); 
    //printf("buffer is : %s\n", buffer); 
    rel1time = atoi(buffer); //convert returned string to int 
    if(rel1time == 0){access_denied(1);} 
    pthread_exit(NULL); 
} 

void *r2python_thread(void *val){ 
    sprintf(r2command,"python access.py r2 %X",val); //build python command 
    FILE* file = popen(r2command, "r"); //execute command using popen  
    char buffer[5]; 
    fscanf(file, "%100s", buffer); //read command output 
    pclose(file); 
    //printf("buffer is : %s\n", buffer); 
    rel2time = atoi(buffer); //convert returned string to int 
    pthread_exit(NULL); 
} 

//reader 1 bit functions 
void onebit0(){ //adds a 0 
     r1bit_count ++; //increase bit count 
     r1bits = (r1bits << 1) + 0; 
     r1timeout = t; //reset timeout 
} 

void onebit1(){ //adds a 1 
     r1bit_count ++; 
     r1bits = (r1bits << 1) + 1; 
     r1timeout = t; 
} 

//reader 2 bit functions 
void twobit0(){ //adds a 0 
     r2bit_count ++; //increase bit count 
     r2bits = (r2bits << 1) + 0; 
     r2timeout = t; //reset timeout 
} 

void twobit1(){ //adds a 1 
     r2bit_count ++; 
     r2bits = (r2bits << 1) + 1; 
     r2timeout = t; 
} 
int main(){ 
    wiringPiSetup(); //initialise wiringPi 
    pinMode (r1data0, INPUT); // set reader 1 data0 as input 
    pinMode (r1data1, INPUT); // set reader 1 data1 as input 

    pinMode (r2data0, INPUT); // set reader 2 data0 as input 
    pinMode (r2data1, INPUT); // set reader 2 data1 as input 



    //reader 1 
    wiringPiISR(r1data0, INT_EDGE_FALLING, onebit0); // set interrupt on data 0 if it falls call bit0 function 
    wiringPiISR(r1data1, INT_EDGE_FALLING, onebit1); // set interrupt on data 1 if it falls call bit1 function 
    //reader 2 
    wiringPiISR(r2data0, INT_EDGE_FALLING, twobit0); // set interrupt on data 0 if it falls call bit0 function 
    wiringPiISR(r2data1, INT_EDGE_FALLING, twobit1); // set interrupt on data 1 if it falls call bit1 function  

    while (1){ //loop 
     if (r1bit_count > 0){ //if bits is not empty 
      r1timeout--; // reduce timeout by 1 
      if(r1timeout == 0){ //and it has timed out ie no more bits coming 
       //printf("%X\n",r1bits); 
       pthread_create(&threads, NULL, r1python_thread,(void *) r1bits); //start new thread for python program 
       oldr1bit_count = r1bit_count; 
       r1bit_count = 0; //reset bit count 
       r1bits = blank; //clear bits cariable 
       r1timeout = t; //reset timeout 
       } 
      }  
     if (r2bit_count > 0){ //if bits is not empty 
      r2timeout--; // reduce timeout by 1 
      if(r2timeout == 0){ //and it has timed out ie no more bits coming 
       pthread_create(&threads, NULL, r2python_thread,(void *) r2bits); //start new thread for python program 
       r2bit_count = 0; //reset bit count 
       r2bits = blank; //clear bits cariable 
       r2timeout = t; //reset timeout 
       } 
      } 
     if (rel1time > 0){ 
      pinMode(rel1, OUTPUT); 
      int diff = time(NULL) - previoust; 
      if(diff >= 1){ 
      previoust = time(NULL); 
      rel1time--; 
      } 
     } 
     else{ 
      pinMode(rel1, INPUT); 
      previoust = time(NULL); 
     } 
    delay(1); 
    } 
    return 0; 
}