今、GPSのGPGGAのデータをモードセレクターを使って、
SW2を押すとSunSPOT本体にGPSデータを書き込み続けるプログラムを作ろうとしていますが
SW2を押しても一つのデータしか取れません。
何が悪いのでしょうか?
配線は資料のそのままです。
GPS は秋月の GT-720F です。
以下プロジェクトの内訳です。
ModeSelection.java
- コード: 全て選択
package org.sunspotworld;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import com.sun.squawk.io.BufferedReader;
import java.io.InputStreamReader;
public class ModeSelection extends SunSpotApplicationAdapter {
RMS rms;
BufferedReader in;
static final String DB = "GPSRecordStore";
static final int RECORD = 0;
static final int PRINT = 1;
static final int DELETE = 2;
ModeSelector modeSelector;
protected void setup() {
System.out.println("setup");
modeSelector = new ModeSelector(LEDColor.GREEN, LEDColor.CYAN, LEDColor.RED);
while (!modeSelector.selected()) {
sleep(100);
}
switch (modeSelector.getMode()) {
case RECORD:
rms = RMS.getInstance(DB, true);
in = new BufferedReader(new InputStreamReader(new UARTInputStream(9600)));
break;
case PRINT:
rms = RMS.getInstance(DB, false);
break;
}
}
protected void loop() throws Exception {
switch (modeSelector.getMode()) {
case RECORD:
for(int w = 0 ;w <= 100;w++){
String sentence = in.readLine();
System.out.println("??Get??");
if (sentence.startsWith("$GPGGA")) {
System.out.println("GetGet");
rms.writeString(sentence).
flush();
System.out.println("Keep!");}
sleep(99);
}
case PRINT:
if (rms.hasMoreRecords()) {
System.out.println(rms.readString());
rms.skip();
} else {
exitLoop();
}
break;
case DELETE:
RMS.delete(DB);
exitLoop();
}
}
protected void cleanup() throws Exception {
System.out.println("Cleanup");
in.close();
}
}
ModeSelector.java
資料そのままです。
RMS.java
- コード: 全て選択
package org.sunspotworld;
/**
*
* @author yamaguchi
*/
import com.sun.squawk.util.ByteArrayInputStreamWithSetBytes;
import com.sun.squawk.util.ByteArrayOutputStreamWithGetBytes;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class RMS {
private RecordStore rs;
private DataInputStream in;
private ByteArrayInputStreamWithSetBytes baIn;
private DataOutputStream out;
private ByteArrayOutputStreamWithGetBytes baOut;
private int index;
public RMS(String name, boolean createIfNecessary) {
try {
rs = RecordStore.openRecordStore(name, createIfNecessary);
} catch (Exception e) {
rs = null;
}
}
public static RMS getInstance(String name, boolean createIfNecessary) {
try {
return new RMS(name, createIfNecessary);
} catch (Exception e) {
return null;
}
}
public static void delete(String name) {
try {
RecordStore.deleteRecordStore(name);
} catch (RecordStoreException e) {
}
}
public boolean hasMoreRecords() {
try {
return rs.getNumRecords() > 0 && index <= rs.getNumRecords();
} catch (RecordStoreNotOpenException e) {
return false;
}
}
void prepareInput() throws Exception {
if (index == 0 && rs.getNumRecords() > 0)
index = 1;
baIn = new
ByteArrayInputStreamWithSetBytes(rs.getRecord(index));
in = new DataInputStream(baIn);
}
void prepareOutput() {
if (out == null) {
baOut = new ByteArrayOutputStreamWithGetBytes();
out = new DataOutputStream(baOut);
}
}
public String readString() throws Exception {
if (in == null)
prepareInput();
try {
return in.readUTF();
} catch (EOFException e) {
if (index < rs.getNumRecords()) {
index ++;
in = null;
return readString();
}
throw e;
}
}
public long readLong() throws Exception {
if (in == null)
prepareInput();
try {
return in.readLong();
} catch (EOFException e) {
if (index < rs.getNumRecords()) {
index ++;
in = null;
return readLong();
}
throw e;
}
}
public java.lang.String readBun() throws Exception {
if (in == null)
prepareInput();
try {
return in.readUTF();
} catch (EOFException e) {
if (index < rs.getNumRecords()) {
index ++;
in = null;
return readBun();
}
throw e;
}
}
public double readDouble() throws Exception {
if (in == null)
prepareInput();
try {
return in.readDouble();
} catch (EOFException e) {
if (index < rs.getNumRecords()) {
index ++;
in = null;
return readDouble();
}
throw e;
}
}
public void skip() {
if (in != null) {
try {
in.close();
} catch (IOException e) {
} finally {
index ++;
in = null;
}
}
}
public RMS writeString(String s) throws Exception {
if (out == null)
prepareOutput();
out.writeUTF(s);
return this;
}
public RMS writeLong(long n) throws Exception {
if (out == null)
prepareOutput();
out.writeLong(n);
return this;
}
public RMS writeBun(java.lang.String a) throws Exception {
if (out == null)
prepareOutput();
out.writeUTF(a);
return this;
}
public RMS writeDouble(double v) throws Exception {
if (out == null)
prepareOutput();
out.writeDouble(v);
return this;
}
public RMS flush() throws Exception {
byte[] data = baOut.getBytes();
rs.addRecord(data, 0, data.length);
try {
out.close();
}
catch (IOException e) {}
finally {
out = null;
}
return this;
}
}
UARTInputStream.java
資料そのままです。
SunSpotAplicationAdapter.java
資料そのままです。