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

2008年7月16日星期三

Identify the COM port number and device address of Sun SPOT

Basically, I am using the spotfinder.exe which is a tool provided with Sun SPOT SDK to detect the COM port in use and device adddress of the connected Sun SPOT.
NOTE: Only one SPOT can be detected in this, I would like to change it later.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package simpleexec;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author pdeng
*/

public class Main {

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
// TODO code application logic here
int portNumber = spotCOMPort();
System.out.println(portNumber);
String deviceMACAddress = getDeviceMACAddress();
System.out.println(deviceMACAddress);
}

private static String getDeviceMACAddress() {
try {
String rawString = "";
String line;
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Sun\\SunSPOT\\sdk\\bin\\spotfinder.exe -vv");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
rawString = rawString + line;
}
input.close();
String rawAddress = String.valueOf(rawString.subSequence(rawString.indexOf("00144F01") + 8, rawString.indexOf("00144F01") + 16));
String MACAddress = "0014.4F01." + rawAddress.substring(0, 4) + "." + rawAddress.substring(4, 8);
return MACAddress;
} catch (Exception err) {
err.printStackTrace();
return "-1";
}
}

private static int spotCOMPort() {
try {
String rawString = "";
String line;
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Sun\\SunSPOT\\sdk\\bin\\spotfinder.exe -vv");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
rawString = rawString + line;
//System.out.println(line);
}
input.close();
String comPort = String.valueOf(rawString.subSequence(rawString.indexOf("(COM") + 4, rawString.indexOf(")parsed")));
return Integer.parseInt(comPort);
} catch (Exception err) {
err.printStackTrace();
return -1;
}
}
}

没有评论: