我的最终目标是从 Adafruit Ultimate GPS(NMEA 0183 标准)接收纬度和经度 GPS 信息到我的 Java 应用程序。我正在使用 Java Marine API 来执行此操作。然后,当前位置将与时间戳一起写入数据库。
到目前为止,我已经成功(我认为)配置 RXTX 以通过 USB 端口启用 coms。 Java Marine API 附带了一些示例 .java 文件。下面的示例应该扫描所有现有的 COM 端口并查找 NMEA 数据 - 但当我运行它时,我只得到输出
Scanning port /dev/tty.Bluetooth-Incoming-Port
GPS 已连接,当我通过终端访问它时,我可以看到它流数据。
请原谅这个文本转储,我有点超出了我的深度,不确定哪些部分是相关的。
public class SerialPortExample implements SentenceListener {
public SerialPortExample() {
init();}
public void readingPaused() {
System.out.println("-- Paused --");}
public void readingStarted() {
System.out.println("-- Started --");}
public void readingStopped() {
System.out.println("-- Stopped --");}
public void sentenceRead(SentenceEvent event) {
// here we receive each sentence read from the port
System.out.println(event.getSentence());}
private SerialPort getSerialPort() {
try {
Enumeration<?> e = CommPortIdentifier.getPortIdentifiers();
while (e.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
SerialPort sp = (SerialPort) id.open("SerialExample", 30);
sp.setSerialPortParams(4800, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream is = sp.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buf = new BufferedReader(isr);
System.out.println("Scanning port " + sp.getName());
// try each port few times before giving up
for (int i = 0; i < 5; i++) {
try {
String data = buf.readLine();
if (SentenceValidator.isValid(data)) {
System.out.println("NMEA data found!");
return sp;}
} catch (Exception ex) {
ex.printStackTrace();}}
is.close();
isr.close();
buf.close();}
}
System.out.println("NMEA data was not found..");
} catch (Exception e) {
e.printStackTrace();}
return null;}
private void init() {
try {SerialPort sp = getSerialPort();
if (sp != null) {
InputStream is = sp.getInputStream();
SentenceReader sr = new SentenceReader(is);
sr.addSentenceListener(this);
sr.start(); }
} catch (IOException e) {
e.printStackTrace();}}
public static void main(String[] args) {
new SerialPortExample();}}
所以我认为它卡在了 tty.Bluetooth-Incoming-Port 上?我怎样才能让它查看 tty.usbserial 端口?
感谢
您即将建立联系了 ^^
当您扫描系统中的 com 端口时,只需将其打印出来即可!
while (e.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
if(serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(serialPortId.getName());//print the serial port name
}
}
你的输出应该是这样的
本机库版本 = RXTX-2.1-7
Java 库版本 = RXTX-2.1-7
/dev/ttyS1
/dev/ttyS0
现在你know串行端口的名称您可以使用此信息来打开该端口!假设您的 GPS 连接到串口 ttyS1...
再次进入您的代码并进行如下调整:
enumComm = CommPortIdentifier.getPortIdentifiers();
while(enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (serialPortId.getName().equalsIgnoreCase("/dev/ttyS1")) { //HERE you get your serial port
try {
SerialPort sp = (SerialPort) id.open("SerialExample", 30);
//continue with your code here
} catch (PortInUseException e) {
System.out.println("port in use");
}
}
}
this only works because your usb-device creates a virtual serial port!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)