I have connected my RFID reader to GPIO pins of raspberry pi 3. The RFID reader has 4 wires. vcc,gnd,data0 and data1. Here data0 and data1 is set pull_up (HIGH) and when 0bit is read then data0 is edged to LOW and when 1 is read then data1 is edged to LOW. I wrote the python and java program. It worked fine for python, but it didn't work for java. Here is Python code:
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
Here counter is updated when a bit is read by reader. So, here counter value is 26 and When I tried to do in java using pi4j then listener is not called 26 time means counter is not updated 26 times: here is code in 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);
}
}
}
In this java code, Counter value is 1 or 2 randomly. it must be 26 as I am sending 26bit of data. Can anyone help me how to resolve the this problem. The circuit connection is fine as it works fine for python code.
I tried with different way using wiringpi for above problem which gives output I am looking for. Still, I did not get what was problem in above routine. Here's solution I am posting:
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;
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.