miham
Posts: 5
Joined: Tue Jan 07, 2014 1:14 pm

rPI + Wiegand + Shift problem

Tue Jan 07, 2014 1:58 pm

I have problem with my Wiegand RFID reader (26bit). I have write simple C app and everything seems fine. But after 10 reads for example, it starts to shift bits. Is RPi Raspbian to slow for Wiegand time protocol?

Here is sample code and output

Code: Select all

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;

    }

}
and output:

10100100111111110110011011
10100100111111110110011011
10100100111111110110011011
10100100111111110110011011
10100100111111110001101110
10010011111111011001101110
10010011111111011001101110
10010011111111011001101110
10010011111111011001101110
10010011111111011001101110
10010011111111011001101110



User avatar
Douglas6
Posts: 4519
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: rPI + Wiegand + Shift problem

Tue Jan 07, 2014 7:36 pm

That 'simple C app' looks suspiciously like Java to me. I think you're running into Java's preemptive garbage collection (GC), which periodically suspends all threads (including MAX_PRIORITY threads) to do it's thing. Java is just not suited for timing-critical applications like flight control, real-time trading, or, apparently, reading RFIDs (Real Time versions of Java exist, but not for the Pi, as far as I know).
I'm afraid I'm not familiar with JNI, but somehow you're going to need to pull out the time-critical parts of your code into threads that are not subject to Java's GC.
Last edited by Douglas6 on Tue Jan 07, 2014 7:44 pm, edited 1 time in total.

miham
Posts: 5
Joined: Tue Jan 07, 2014 1:14 pm

Re: rPI + Wiegand + Shift problem

Tue Jan 07, 2014 7:39 pm

Douglas6 wrote:That 'simple C app' looks suspiciously like Java to me. I think you're running into Java's preemptive garbage collection (GC), which periodically stops all threads (including MAX_PRIORITY threads) to do it's thing. Java is just not suited for timing-critical applications like flight control, real-time trading, or, apparently, reading RFIDs (Real Time versions of Java exist, but not for the Pi, as far as I know).
I'm afraid I'm not familiar with JNI, but somehow you're going to need to pull out the time-critical parts of your code into threads that are not subject to Java's GC.
It's ok then in c or c++, is there any way then to communicate with Java app ?

User avatar
Douglas6
Posts: 4519
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: rPI + Wiegand + Shift problem

Tue Jan 07, 2014 7:48 pm

Java Native Interface (JNI) is the standard way. Pi4J uses that. But I don't know enough about it to tell you how to do what you're attempting, sorry.

User avatar
joan
Posts: 13662
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: rPI + Wiegand + Shift problem

Tue Jan 07, 2014 7:50 pm

The Python example uses the socket interface to pigpio (the underlying C library). You (or I if I could be bothered) could use the same interface from Java.

JanJansen47
Posts: 1
Joined: Mon Feb 18, 2013 8:10 pm

Re: rPI + Wiegand + Shift problem

Fri Jan 10, 2014 9:08 am

You run at least two threads and in Java you are never sure about which comes first (despite your priority). In your "run" thread and in print you reset your bit counter but the reset in the print can come when the run has already started scanning. Several solutions to fix this.

Jan Jansen

chg91
Posts: 3
Joined: Sat Mar 10, 2018 10:03 pm

Re: rPI + Wiegand + Shift problem

Sun Mar 18, 2018 7:15 pm

hi me too i m using java and pi4j library have you fixed your problem if yes could you tell how you did it

Return to “Java”