简体   繁体   English

Pin 21/27不适用于Pi4j

[英]Pin 21 / 27 does not work with Pi4j

I have a Raspberry Pi rev. 我有一个Raspberry Pi版本。 2 (512mb ram version). 2(512mb内存版本)。 I have some LEDs that I am controlling over Pi4J. 我有一些可以控制Pi4J的LED。 Every LED works except the ones that i attach/connect to pin 21/27. 除了我连接/连接到引脚21/27的那些LED之外,每个LED都可以工作。 I have changed the LEDs to see if one is broken but it was not broken. 我已经更改了LED指示灯,以查看是否已损坏但未损坏。 I did some research on google and found this . 我在Google上做了一些研究,发现了这一点 Unfortunately I could not find a way to change Pin21 to 27 because Pi4j uses another numbering scheme. 不幸的是,我找不到将Pin21更改为27的方法,因为Pi4j使用了另一种编号方案。 (For numbering see here ) (有关编号,请参见此处

I couldn't find an error in my code but it looks like this: 我在代码中找不到错误,但看起来像这样:

package gpio; 包gpio;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class GPIOController {

    final long second = 1000L;

    final GpioController gpio = GpioFactory.getInstance();

    final GpioPinDigitalOutput red_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_03);
    final GpioPinDigitalOutput red_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_02);

    final GpioPinDigitalOutput yellow_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_00);
    final GpioPinDigitalOutput yellow_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_07);

    final GpioPinDigitalOutput green_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_06);
    final GpioPinDigitalOutput green_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_05);
    final GpioPinDigitalOutput green_3 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_04);
    final GpioPinDigitalOutput green_4 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_01);

    public GPIOController() {
        gpio.shutdown();
    }

    // level 1 = red_1, level 8 = green_4
    public void pulse(int level) {
        allLow();
        switch(level) {
        case 1:
            red_1.pulse(second);
            break;
        case 2:
            red_2.pulse(second);
            break;
        case 3:
            yellow_1.pulse(second);
            break;
        case 4:
            yellow_2.pulse(second);
            break;
        case 5:
            green_1.pulse(second);
            break;
        case 6:
            green_2.pulse(second);
            break;
        case 7:
            green_3.pulse(second);
            break;
        case 8:
            green_4.pulse(second);
            break;
        }
    }

    public void high(int level) {
        allLow();
        switch(level) {
        case 1:
            red_1.high();
            break;
        case 2:
            red_2.high();
            break;
        case 3:
            yellow_1.high();
            break;
        case 4:
            yellow_2.high();
            break;
        case 5:
            green_1.high();
            break;
        case 6:
            green_2.high();
            break;
        case 7:
            green_3.high();
            break;
        case 8:
            green_4.high();
            break;
        }
    }

    public void allLow() {
        gpio.shutdown();
    }

}

I call the methods through this class: 我通过此类来调用方法:

package connection;

import gpio.GPIOController;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Receiver {

    private ServerSocket server;
    private Socket client;
    private DataInputStream clientIn;
    private DataOutputStream clientOut;

    private GPIOController gpioController = new GPIOController();

    public Receiver(int port) throws IOException {
        server = new ServerSocket(port);
        listenForClient();
    }

    // client listener in a new thread
    public void listenForClient() {
        Runnable listener = new Runnable() {
            public void run() {
                while (true)
                    try {
                        // only the most recently connected client
                        // can control the LEDs
                        client = server.accept();
                    } catch (IOException e) {
                        e.printStackTrace(System.out);
                    }

            }
        };
        Thread listenerThread = new Thread(listener);
        listenerThread.setName("Client Listener");
        listenerThread.start();
    }

    // command listener in a new thread
    public void listenForCommands() {
        final Runnable listener = new Runnable() {
            public void run() {
                while (true) {
                    try {
                        if (client != null) {
                            DataInputStream clientIn = new DataInputStream(
                                client.getInputStream());
                            String msg = clientIn.readUTF();
                            handleMessage(msg);
                        }
                    } catch (IOException e) {
                        e.printStackTrace(System.out);
                    }
                }
            }
        };
        Thread listenerThread = new Thread(listener);
        listenerThread.setName("Command Listener");
        listenerThread.start();
    }

    public void handleMessage(String msg) {
        String[] command = msg.split(" ");
        String commandType = command[0]; // "pulse" or "permanent"
        for (int i = 1; i < command.length; i++) {
            try {
                if (commandType.equals("pulse"))
                    gpioController.pulse(Integer.parseInt(command[i]));
                else
                    gpioController.high(Integer.parseInt(command[i]));
            } catch (Exception e) {
                try {
                    clientOut = new DataOutputStream(client.getOutputStream());
                    clientOut.writeUTF(e.getMessage());
                } catch (IOException ioe) {
                    ioe.printStackTrace(System.out);
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            new Receiver(8374).listenForCommands();
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }

}

Is there anything I can do to get that Pin working (including quick & dirty solutions)? 我能做些什么来使Pin正常工作(包括快速和肮脏的解决方案)? By the way; 顺便说说; I do not want to use the P5 Pins because I use a PiCobbler. 我不想使用P5引脚,因为我使用的是PiCobbler。 I also want to use Java for this. 我也想为此使用Java。 Thanks in advance. 提前致谢。

Selim 塞利姆

So, It was a bug related to the distro I guess. 因此,我猜这是与发行版相关的错误。 sudo apt-get dist-upgrade fixed it. sudo apt-get dist-upgrade修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM