我在树莓派上使用java进行编程,想通过读取python脚本执行输出到idea的控制台
py脚本
import RPi.GPIO as GPIO
from pn532 import *
if __name__ == '__main__':
pn532 = PN532_SPI(debug=False, reset=20, cs=4)
#pn532 = PN532_I2C(debug=False, reset=20, req=16)
#pn532 = PN532_UART(debug=False, reset=20)
ic, ver, rev, support = pn532.get_firmware_version()
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()
print('Waiting for RFID/NFC card...')
uid = pn532.read_passive_target(timeout=0.5)
print('Found card with UID:', [hex(i) for i in uid])
GPIO.cleanup();
java程序
package test;
import org.python.core.PyFunction;
import org.python.util.PythonInterpreter;
import java.io.*;
public class Read {
public static void main(String[] args) {
Process proc;
try {
proc = Runtime.getRuntime().exec("python /home/pi/Desktop/raspberrypi/python/a.py");// 执行py文件
// proc = Runtime.getRuntime().exec("python /home/pi/Desktop/raspberrypi/python/example_get_uid.py");// 执行py文件
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
执行py脚本之后是有结果的
但是执行java文件后没有输出
我发现时from pn532 import *的问题,但是不导入pn532这个脚本也用不了
有什么好的解决办法吗?