如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2007年7月25日星期三

Dump data from Sun SPOT

Free Range Sun SPOT

/*
* StartApplication.java
*
* Created on October 8, 2006, 12:15 AM
*/


package org.sunspotworld;

import com.sun.spot.peripheral.NoAckException;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.ITemperatureInput;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import com.sun.spot.sensorboard.peripheral.ILightSensor;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.*;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* The startApp method of this class is called by the VM to start the
* application.
*
* The manifest specifies this class as MIDlet-1, which means it will
* be selected for execution.
*/

public class StartApplication extends MIDlet {
private IAccelerometer3D accel = EDemoBoard.getInstance().getAccelerometer();
private ITemperatureInput tempSensor = EDemoBoard.getInstance().getADCTemperature();
private ILightSensor lightSensor= EDemoBoard.getInstance().getLightSensor();

protected void startApp() throws MIDletStateChangeException {
new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host

IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
System.out.println("Our radio address = " + ourAddr.asDottedHex());

while (true) {
getReading();
}
}

protected void pauseApp() {
// This will never be called by the Squawk VM
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// Only called if startApp throws any exception other than MIDletStateChangeException
}

private void getReading() {
try {
String msg = String.valueOf(accel.getAccelX())+";"+
String.valueOf(accel.getAccelY())+";"+
String.valueOf(accel.getAccelZ())+";"+
String.valueOf(tempSensor.getCelsius())+";"+
String.valueOf(lightSensor.getValue());
sendOut(msg);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void sendOut(String msg) {
System.out.println(msg);
try{
RadiogramConnection conn =(RadiogramConnection)Connector.open("radiogram://0014.4F01.0000.1455:100"); //This is the address of base station
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try{
dg.writeUTF(msg);
conn.send(dg);
}finally{
conn.close();
}
}catch(IOException e){
}
}
}

Base Station and Host PC Application

 
/*
* SunSpotHostApplication.java
*
* Created on December 21, 2006, 16:45 PM
*/


package org.sunspotworld;

import com.sun.spot.peripheral.NoAckException;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.IEEEAddress;

import java.io.*;
import javax.microedition.io.*;


/**
* Sample Sun SPOT host application
*/

public class SunSpotHostApplication {

/**
* Print out basestation address.
*/

public void run() {
IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
System.out.println("Our radio address = " + ourAddr.asDottedHex());
FileWriter fw=null;
PrintWriter pw=null;
//FileWriter fw=null;
try {
fw = new java.io.FileWriter("C:\\value.dat", true);
} catch (IOException ex) {
ex.printStackTrace();
}
pw=new java.io.PrintWriter(fw);
//PrintWriter pw=new java.io.PrintWriter(fw);
pw.println("##;##");
pw.println("@Sun SPOT Data file");
pw.println("X;Y;Z;Temperature;Light");
pw.close();
try {
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}

while(true){
try{
RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:100");
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try {
conn.receive(dg);
String rawData = dg.readUTF();
System.out.println(rawData);

FileWriter fc=null;
PrintWriter pc=null;
//FileWriter fw=null;
try {
fc = new java.io.FileWriter("C:\\value.dat", true);
} catch (IOException ex) {
ex.printStackTrace();
}
pc=new java.io.PrintWriter(fc);
//PrintWriter pw=new java.io.PrintWriter(fw);
pc.println(rawData);
pc.close();
try {
fc.close();
} catch (IOException ex) {
ex.printStackTrace();
}

/*
pw.println(rawData);
pw.close();
try {
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
*/

//streamWriter(rawData);
} catch (NoAckException e) {
e.printStackTrace();
} finally {
conn.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}

/**
* Start up the host application.
*
* @param args any command line arguments
*/

public static void main(String[] args) {
SunSpotHostApplication app = new SunSpotHostApplication();
app.run();
}
}

没有评论: