我尝试使用 jdk.dio 在 java 中读取 gaz 传感器,但我总是使用以下代码在我的 SPI 总线上读取 0:
import java.nio.ByteBuffer;
import org.slf4j.Logger;
import jdk.dio.Device;
import jdk.dio.DeviceConfig;
import jdk.dio.DeviceManager;
import jdk.dio.spibus.SPIDevice;
import jdk.dio.spibus.SPIDeviceConfig;
public class SPIDACDemo {
public static void startApp(Logger sLogger, int spiDeviceId) {
try {
SPIDeviceConfig config = new SPIDeviceConfig(0, 1, 300000, 0, DeviceConfig.DEFAULT, Device.BIG_ENDIAN);
SPIDevice spi = (SPIDevice) DeviceManager.open(config);
while (true) {
ByteBuffer rcvMsg = ByteBuffer.wrap(new byte[2]);
int resp = spi.read(rcvMsg);
System.out.println(resp);
for (byte b : rcvMsg.array()) {
System.out.println(b);
}
String message = "Response: " + resp + ", Received: " + new String(rcvMsg.array(),"ASCII");
System.out.println(message);
Thread.sleep(1000);
}
spi.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage());
sLogger.error(SPIDACDemo.class.getName() + ", error:" + ex);
}
}
}
我的连接正常,我可以在 python 中读取值,但在 Java 中我只读取 0...