我已将我的RFID阅读器连接到raspberry pi 3的GPIO引脚.RFID阅读器有4根线。 vcc,gnd,data0和data1。这里data0和data1设置为pull_up(HIGH),当读取0bit时,data0被边缘化为LOW,当读取1时,data1被边缘化为LOW。我写了python和java程序。它适用于python,但它对java不起作用。这是Python代码:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(True)
chan_list = [17,27]
GPIO.setup(chan_list, GPIO.IN, pull_up_down=GPIO.PUD_UP)
counter = 0
test_counter = 0
def my_callback(channel):
global counter
counter += 1
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback)
GPIO.add_event_detect(27, GPIO.FALLING, callback=my_callback)
while(True):
print "counter =",counter
当读取器读取位时,此处更新计数器。所以,这里的计数器值是26,当我尝试在java中使用pi4j时,没有调用监听器26次意味着计数器没有更新26次:这里是java中的代码。
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;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import com.pi4j.wiringpi.GpioInterrupt;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.*;
import com.pi4j.util.CommandArgumentParser;
public class ListenGpioExample {
public static void main(String args[]) throws InterruptedException {
ListenGpio listenGpio = new ListenGpio();
listenGpio.run(args);
}
}
class ListenGpio{
int counter ;
public void run(String[] args){
System.out.println("<--Pi4J--> GPIO Listen Example ... started.");
final GpioController gpio = GpioFactory.getInstance();
Pin pin = CommandArgumentParser.getPin(RaspiPin.class, RaspiPin.GPIO_00, args);
Pin pin2 = CommandArgumentParser.getPin(RaspiPin.class, RaspiPin.GPIO_02, args);
PinPullResistance pull = CommandArgumentParser.getPinPullResistance(PinPullResistance.PULL_UP, args);
final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(pin,pull);
final GpioPinDigitalInput myButton2 = gpio.provisionDigitalInputPin(pin2,pull);
myButton.setShutdownOptions(true);
myButton2.setShutdownOptions(true);
myButton.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
counter += 1;
}
});
myButton2.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
counter += 1;
}
});
for (;;) {
System.out.println("counter =="+counter);
}
}
}
在这个java代码中,Counter值随机为1或2。它必须是26,因为我发送26位数据。任何人都可以帮我解决这个问题。电路连接很好,因为它适用于python代码。
答案 0 :(得分:0)
我尝试用不同的方法使用wirespi来解决上面的问题,它提供了我正在寻找的输出。尽管如此,我还没有得到上述例程中的问题。这是我发布的解决方案:
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.GpioInterruptCallback;
public class RFIDAndroid {
public static void main(String args[]) throws InterruptedException{
RFIDReader rfidReader = new RFIDReader(0,2);
rfidReader.readRFID();
}
}
class RFIDReader{
int cardCode;
int facilityCode;
int data0;
int data1;
int counter =0;
int flagDone = 0;
int timer_count = 3000;
int[] databits = new int[100];
RFIDReader(int data0,int data1){
this.data0= data0;
this.data1=data1;
}
int getCardCode(){
return cardCode;
}
int facilityCode(){
return facilityCode;
}
void readRFID() throws InterruptedException{
if (Gpio.wiringPiSetup() == -1) {
System.out.println(" ==>> GPIO SETUP FAILED");
return;
}
Gpio.pinMode(data0, Gpio.INPUT) ;
Gpio.pinMode(data1, Gpio.INPUT) ;
Gpio.pullUpDnControl(data0, Gpio.PUD_UP);
Gpio.pullUpDnControl(data1, Gpio.PUD_UP);
Gpio.wiringPiISR(data0, Gpio.INT_EDGE_FALLING, new GpioInterruptCallback() {
@Override
public void callback(int pin) {
flagDone = 0;
databits[counter] = 1;
counter +=1;
timer_count = 3000;
}
});
Gpio.wiringPiISR(data1, Gpio.INT_EDGE_FALLING, new GpioInterruptCallback() {
@Override
public void callback(int pin) {
flagDone = 0;
databits[counter] = 0;
counter +=1;
timer_count = 3000;
}
});
while(true){
if (flagDone ==0 ) {
timer_count -= 1;
if (timer_count == 0){
flagDone = 1;
}
}
if (counter > 0 && flagDone ==1) {
int i;
if (counter == 35)
{
for (i=2; i<14; i++)
{
facilityCode <<=1;
facilityCode |= databits[i];
}
for (i=14; i<34; i++)
{
cardCode <<=1;
cardCode |= databits[i];
}
}
else if (counter == 26)
{
for (i=1; i<9; i++)
{
facilityCode <<=1;
facilityCode |= databits[i];
}
for (i=9; i<25; i++)
{
cardCode <<=1;
cardCode |= databits[i];
}
}
else {
facilityCode = -1;
cardCode = -1;
}
System.out.println("cardCode ="+cardCode);
System.out.println("facility Code="+facilityCode);
counter = 0;
facilityCode = 0;
cardCode = 0;
for (i=0; i< 100; i++)
{
databits[i] = 0;
}
}
}
}
}