Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GPIO不高,运行PI4J时忽略代码_Java_Raspberry Pi2 - Fatal编程技术网

Java GPIO不高,运行PI4J时忽略代码

Java GPIO不高,运行PI4J时忽略代码,java,raspberry-pi2,Java,Raspberry Pi2,我试图控制一个GPIO引脚在我的程序中打开一个简单的继电器所有的代码工程除了一个问题;发送高信号的GPIO引脚代码不起作用,也不会导致任何错误,我正在使用pi4j库控制RaspberryPi板上的引脚 这是我的密码: import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFacto

我试图控制一个GPIO引脚在我的程序中打开一个简单的继电器所有的代码工程除了一个问题;发送高信号的GPIO引脚代码不起作用,也不会导致任何错误,我正在使用pi4j库控制RaspberryPi板上的引脚

这是我的密码:

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;


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


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.sql.Timestamp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;


public class FileWatch {

    static String clkID;
    static String clkID2;
    static String ts;
    static String ts1;
    static boolean done = false;
    static boolean REdone = false;
    static boolean finished = false;
    static boolean ready;

    static String host ="jdbc:mysql://localhost/dancers";
    static String username ="root";
    static String password ="beaker19";


    public static void main(String[] args) throws IOException,
            InterruptedException {

       // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // provision gpio pin #01 as an output pin and turn on
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, "MyLED", PinState.LOW);


        while (true) {
            done = false;
            REdone=false;
            checkFile();
            System.out.println("worked");
            pin.high();
            Thread.sleep(3000);
            REcheckFile();
            Thread.sleep(500);
            //Thread.
            if (clkID.equals(clkID2)) {

            uploadTimes();

            }

            else {
                System.out.println("Wrong matching ID's");
            }
            Thread.sleep(1000);
            pin.low();
         }
    }   

    // Thread th = new Thread(new FileWatch());
    // th.start();
    // checkFile();
    // REcheckFile();
    // if (clkID2.equals(clkID2)){
    // System.out.println("worked");
    // }
    // else {
    // System.out.println("not worked");
    // }
    //
    public static void check() throws InterruptedException{

        checkFile();
        Thread.sleep(3000);
        REcheckFile();
        Thread.sleep(500);

    }


    public String getClkId() {

        return clkID;

    }

    public static void connection() {

//      while(!finished){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("worked");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      }
    }


    public static void checkFile() {
        while (!done) {
            try {
                WatchService watcher = FileSystems.getDefault()
                        .newWatchService();
                Path dir = Paths.get("/home/pi/rpi2ardu");
                dir.register(watcher, ENTRY_MODIFY);

                System.out.println("Watch Service registered for dir: "
                        + dir.getFileName());

                WatchKey key;
                try {
                    key = watcher.take();
                } catch (InterruptedException ex) {
                    return;
                }

                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent.Kind<?> kind = event.kind();

                    @SuppressWarnings("unchecked")
                    WatchEvent<Path> ev = (WatchEvent<Path>) event;
                    Path fileName = ev.context();

                    System.out.println(kind.name() + ": " + fileName);

                    if (kind == ENTRY_MODIFY
                            && fileName.toString().equals("example.txt")) {
                        System.out.println("My source file has changed!!!");
                        String sCurrentLine = null;
                        try (BufferedReader br = new BufferedReader(
                                new FileReader("/home/pi/rpi2ardu/example.txt"))) {
                            while ((sCurrentLine = br.readLine()) != null) {
                                System.out.println(sCurrentLine);
                                clkID = sCurrentLine;
                                System.out.println(clkID);
                                java.util.Date date = new java.util.Date();
                                date = new Timestamp(date.getTime());
                                // System.out.println(new
                                // Timestamp(date.getTime()));
                                ts = date.toString();
                                System.out.println(ts);
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        File inputFile = new File("/home/pi/rpi2ardu/example.txt"); // Your
                                                                            // file
                        File tempFile = new File("/home/pi/rpi2ardu/temp.txt");// temp
                                                                                // file
                        BufferedReader reader = new BufferedReader(
                                new FileReader(inputFile));
                        BufferedWriter writer = new BufferedWriter(
                                new FileWriter(tempFile));
                        String currentLine;
                        while ((currentLine = reader.readLine()) != null) {
                            currentLine = ("");
                            writer.write(currentLine);
                        }
                        writer.close();
                        reader.close();
                        done = true;
                        boolean successful = tempFile.renameTo(inputFile);
                        System.out.println(successful);
                    }
                }

                boolean valid = key.reset();
                if (!valid) {
                    break;
                }
            }

            catch (IOException ex) {
                System.err.println(ex);
            }

        }
    }

    public static void REcheckFile() {
        while (!REdone) {
            try {
                WatchService watcher = FileSystems.getDefault()
                        .newWatchService();
                Path dir = Paths.get("/home/pi/rpi2ardu");
                dir.register(watcher, ENTRY_MODIFY);

                System.out.println("Watch Service registered for dir: "
                        + dir.getFileName());

                WatchKey key;
                try {
                    key = watcher.take();
                } catch (InterruptedException ex) {
                    return;
                }

                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent.Kind<?> kind = event.kind();

                    @SuppressWarnings("unchecked")
                    WatchEvent<Path> ev = (WatchEvent<Path>) event;
                    Path fileName = ev.context();

                    System.out.println(kind.name() + ": " + fileName);

                    if (kind == ENTRY_MODIFY
                            && fileName.toString().equals("example.txt")) {
                        System.out.println("My source file has changed!!!");
                        String sCurrentLine = null;
                        try (BufferedReader br = new BufferedReader(
                                new FileReader("/home/pi/rpi2ardu/example.txt"))) {
                            while ((sCurrentLine = br.readLine()) != null) {
                                System.out.println(sCurrentLine);
                                clkID2 = sCurrentLine;
                                System.out.println(clkID2);
                                java.util.Date date1 = new java.util.Date();
                                date1 = new Timestamp(date1.getTime());
                                // System.out.println(new
                                // Timestamp(date.getTime()));
                                String ts1 = date1.toString();
                                System.out.println(ts1);
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        File inputFile = new File("/home/pi/rpi2ardu/example.txt"); // Your
                                                                            // file
                        File tempFile = new File("/home/pi/rpi2ardu/temp.txt");// temp
                                                                                // file
                        BufferedReader reader = new BufferedReader(
                                new FileReader(inputFile));
                        BufferedWriter writer = new BufferedWriter(
                                new FileWriter(tempFile));
                        String currentLine;
                        while ((currentLine = reader.readLine()) != null) {
                            currentLine = ("");
                            writer.write(currentLine);
                        }
                        writer.close();
                        reader.close();
                        REdone = true;
                        boolean successful = tempFile.renameTo(inputFile);
                        System.out.println(successful);
                    }
                }

                boolean valid = key.reset();
                if (!valid) {
                    break;
                }
            }

            catch (IOException ex) {
                System.err.println(ex);
            }

        }
    }

    public static void uploadTimes(){
          try
            {
              String host ="jdbc:mysql://localhost/dancers";
                String username ="root";
                String password ="beaker19";
                connection();
               Connection conn = DriverManager.getConnection(host, username, password);
                System.out.println("Connected:");

                String t1= ts;
                String t2 =ts1;
                String id =clkID;

                // the mysql insert statement
                String query = " insert into test (id, ts, ts1)"
                  + " values (?, ?, ?)";     
              // create the mysql insert preparedstatement
              java.sql.PreparedStatement preparedStmt = conn.prepareStatement(query);
              preparedStmt.setString (1, id);
              preparedStmt.setString (2, t1);
              preparedStmt.setString (3, t2);

              // execute the preparedstatement
              preparedStmt.execute();
              System.out.println("worked"); 
              conn.close();
            }
            catch (Exception e)
            {
              System.err.println("Got an exception!");
              System.err.println(e.getMessage());
            }

    }


}
import static java.nio.file.StandardWatchEventTypes.ENTRY\u MODIFY;
导入com.pi4j.io.gpio.GpioController;
导入com.pi4j.io.gpio.gpio工厂;
导入com.pi4j.io.gpio.gpiopingitaloutput;
导入com.pi4j.io.gpio.PinState;
导入com.pi4j.io.gpio.RaspiPin;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.nio.file.FileSystems;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.nio.file.WatchEvent;
导入java.nio.file.WatchKey;
导入java.nio.file.WatchService;
导入java.sql.Timestamp;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.sql.Timestamp;
导入java.util.Calendar;
公共类文件监视{
静态字符串clkID;
静态字符串clkID2;
静态字符串ts;
静态字符串ts1;
静态布尔值done=false;
静态布尔重做=假;
静态布尔值finished=false;
静态布尔就绪;
静态字符串host=“jdbc:mysql://localhost/dancers";
静态字符串username=“root”;
静态字符串密码=“烧杯19”;
公共静态void main(字符串[]args)引发IOException,
中断异常{
//创建gpio控制器
最终GpioController gpio=GpioFactory.getInstance();
//提供gpio引脚#01作为输出引脚并打开
最终gpiopingitaloutput pin=gpio.provisionDigitalOutputPin(RaspiPin.gpio_02,“MyLED”,PinState.LOW);
while(true){
完成=错误;
重做=假;
检查文件();
系统输出打印项次(“工作”);
pin.high();
睡眠(3000);
重新检查文件();
睡眠(500);
//线。
if(clkID等于(clkID2)){
上传次数();
}
否则{
System.out.println(“错误的匹配ID”);
}
睡眠(1000);
pin.low();
}
}   
//Thread th=新线程(new FileWatch());
//th.start();
//检查文件();
//重新检查文件();
//if(clkID2.等于(clkID2)){
//系统输出打印项次(“工作”);
// }
//否则{
//系统输出打印项次(“未工作”);
// }
//
public static void check()引发InterruptedException{
检查文件();
睡眠(3000);
重新检查文件();
睡眠(500);
}
公共字符串getClkId(){
返回clkID;
}
公共静态无效连接(){
//当(!完成){
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
系统输出打印项次(“工作”);
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//      }
}
公共静态无效检查文件(){
而(!完成){
试一试{
WatchService watcher=FileSystems.getDefault()
.newWatchService();
Path dir=Path.get(“/home/pi/rpi2ardu”);
方向寄存器(观察者、条目和修改);
System.out.println(“为目录注册的监视服务:”
+dir.getFileName());
监视键;
试一试{
key=watcher.take();
}捕获(中断异常例外){
返回;
}
for(WatchEvent事件:key.pollEvents()){
WatchEvent.Kind-Kind=event.Kind();
@抑制警告(“未选中”)
WatchEvent ev=(WatchEvent)事件;
路径文件名=ev.context();
System.out.println(kind.name()+“:”+文件名);
如果(种类==条目\u修改
&&fileName.toString().equals(“example.txt”)){
System.out.println(“我的源文件已更改!!!”;
字符串sCurrentLine=null;
try(BufferedReader br=新的BufferedReader(
新的文件读取器(“/home/pi/rpi2ardu/example.txt”)){
而((sCurrentLine=br.readLine())!=null){
System.out.println(sCurrentLine);
clkID=电流线;
系统输出打印项次(clkID);
java.util.Date Date=新建java.util.Date();
日期=新的时间戳(date.getTime());
//System.out.println(新
//时间戳(date.getTime());
ts=date.toString();
系统输出打印项次(ts);
}
}捕获(IOE异常){
e、 printStackTrace();
}
File inputFile=new File(“/home/pi/rpi2ardu/example.txt”);//您的
//文件
File tempFile=新文件(“/home/pi/rpi2ardu/temp.txt”);//temp
//文件
BufferedReader reader=新的BufferedReader(
新文件读取器(inputFile));
BufferedWriter writer=新的BufferedWrite