🌎
GNSSレシーバー(BU-353N5)が取得した衛星データをJavaのシリアル通信で受信してみる
先日書いた記事ではAndoridスマホを使ってGNSS(Global Navigation Satellite System(全球測位衛星システム))のデータを受信しました。
今回は専用の(?)GNSSレシーバーを購入してシリアル通信のプログラムでデータを取得してみました。試したとても簡単でした。購入したのはGlobalSatのGU-353N5なるGNSSレシーバーです。
届いた箱はこれくらい。日本語のセットアップ手順もついていました。
10円玉と並べるとこれくらい。
USBで接続して、ドライバ(PL23XX_Prolific_DriverInstaller_v408.zip)をインストールするだけです。
プログラムはChatGPTに聞いたら書いてくれました(^^;
Javaの場合、jSerialCommというライブラリを使うと良いらしい。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>gnss</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>[2.0.0,3.0.0)</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
</project>
コードは恥ずかしながらChatGPTそのままです。
package com.example;
import com.fazecast.jSerialComm.SerialPort;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class ReadNmea {
public static void main(String[] args) throws Exception {
String portName = "COM7"; // Windows。macOS/Linuxなら /dev/tty.usbserial-xxxxx や /dev/ttyUSB0
int[] baudCandidates = {4800}; // まず9600で、ダメなら4800など
SerialPort port = SerialPort.getCommPort(portName);
port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 2000, 0);
boolean opened = false;
for (int baud : baudCandidates) {
port.setComPortParameters(baud, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
port.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
if (port.openPort()) { opened = true; break; }
}
if (!opened) throw new RuntimeException("ポートを開けません: " + portName);
try (BufferedReader br = new BufferedReader(
new InputStreamReader(port.getInputStream(), StandardCharsets.US_ASCII))) {
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("$")) {
System.out.println(line);
}
}
} finally {
port.closePort();
}
}
}
実行するとコンソールにNMEA形式のデータが取得できました。
$GNGGA,022846.000,3542.3754,N,13938.1186,E,2,25,0.60,62.4,M,39.3,M,,*48
$GNGSA,A,3,13,05,21,15,199,30,29,18,194,,,,0.89,0.60,0.66,1*0A
$GNGSA,A,3,83,68,82,69,,,,,,,,,0.89,0.60,0.66,2*05
$GNRMC,022846.000,A,3542.3754,N,13938.1186,E,0.06,350.74,310825,,,D,V*00
$GNGGA,022847.000,3542.3754,N,13938.1186,E,2,24,0.60,62.4,M,39.3,M,,*48
$GNGSA,A,3,13,05,21,15,199,30,29,18,194,,,,0.90,0.60,0.67,1*03
$GNGSA,A,3,83,82,69,,,,,,,,,,0.90,0.60,0.67,2*02
$GNRMC,022847.000,A,3542.3754,N,13938.1186,E,0.07,350.74,310825,,,D,V*00
$GNGGA,022848.000,3542.3754,N,13938.1185,E,2,24,0.60,62.4,M,39.3,M,,*44
$GNGSA,A,3,13,05,21,15,199,30,29,18,194,,,,0.90,0.60,0.67,1*03
$GNGSA,A,3,83,82,69,,,,,,,,,,0.90,0.60,0.67,2*02
$GNRMC,022848.000,A,3542.3754,N,13938.1185,E,0.06,350.74,310825,,,D,V*0D
$GNGGA,022849.000,3542.3754,N,13938.1184,E,2,24,0.60,62.4,M,39.3,M,,*44
$GNGSA,A,3,13,05,21,15,199,30,29,18,194,,,,0.90,0.60,0.67,1*03
$GNGSA,A,3,83,82,69,,,,,,,,,,0.90,0.60,0.67,2*02
$GPGSV,4,1,16,13,78,321,17,05,69,024,20,195,63,190,,21,47,084,16,1*58
$GPGSV,4,2,16,15,47,271,33,199,46,201,25,50,46,201,28,30,34,056,19,1*50
$GPGSV,4,3,16,11,30,160,,29,19,261,19,18,18,319,21,24,12,201,,1*60
$GPGSV,4,4,16,07,10,038,,14,05,101,,194,05,178,18,22,01,116,,1*57
$GNRMC,022849.000,A,3542.3754,N,13938.1184,E,0.12,350.74,310825,,,D,V*08
$GPGSV,4,4,16,07,10,038,,14,05,101,,194,05,178,18,22,01,116,,1*57
$GNRMC,022849.000,A,3542.3754,N,13938.1184,E,0.12,350.74,310825,,,D,V*08
$GNGGA,022850.000,3542.3754,N,13938.1185,E,2,24,0.60,62.4,M,39.3,M,,*4D
$GNGGA,022850.000,3542.3754,N,13938.1185,E,2,24,0.60,62.4,M,39.3,M,,*4D
$GNGSA,A,3,13,05,21,15,199,30,29,18,194,,,,0.90,0.60,0.67,1*03
Androidでええやん、って気持ちもありつつ...手軽にUSB接続するだけでGNSSが受信できるのはいいですね。
Discussion