J2ME, TC45 og GPRS
Hejsa allesammenEr der nogen som kan hjælpe mig her. Vi er igang med hovedopgave på datamatiker udd, og har problemer med at programmere en TC45 til at bruge GPRS.
Her kommer koden.
package testgprs;
import javax.microedition.midlet.*;
import com.siemens.icm.io.*;
/**
* <p>Title: </p>
* <p>Description: Midlet to control the overall customer application</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author Rasmus Trenskow
* @version 1.0
*/
public class Main
extends MIDlet {
private SyncAtCommand ata;
private SyncAtCommand atb;
private GprsThread gprsThread = null;
public Main() {
String response;
try {
ata = new SyncAtCommand(false);
atb = new SyncAtCommand(false);
// Register an ATCommand class with ModuleStatus
ModuleStatus ms = ModuleStatus.getInstance();
ms.registerAtCommand(atb);
ms = null;
// Setup Gprs
response = ata.send("AT^SJNET=GPRS," +
ModuleResources.getInstance().getGprsPoint() + "," +
ModuleResources.getInstance().getGprsUser() + "," +
ModuleResources.getInstance().getGprsPass() + "\r");
if (response.indexOf("OK") >= 0) {
System.out.println("GPRS Setup : OK ");
}
}
catch (ATCommandFailedException e) {
System.out.println("CustDemo Constructor: " + e);
destroyApp(false);
notifyDestroyed();
}
}
public void startApp() throws MIDletStateChangeException {
System.out.println("startApp");
startGprsThread();
System.out.println("Module started");
}
public void pauseApp() {
System.out.println("pause");
}
protected void startGprsThread() {
synchronized (this) {
if (gprsThread == null) {
gprsThread = new GprsThread();
}
gprsThread.start();
}
}
protected void killGprsThread() {
synchronized (this) {
if (gprsThread != null) {
gprsThread.stopRunning();
gprsThread = null;
}
}
}
public void destroyApp(boolean cond) {
System.out.println("destroy");
if (gprsThread != null) {
gprsThread = null;
}
// Close down atcommands using the release method.
try {
if (ata != null) {
false);
ata.release();
ata = null;
}
if (atb != null) {
atb.release();
atb = null;
}
}
catch (ATCommandFailedException e) {
e.printStackTrace();
}
// Finally die.
notifyDestroyed();
}
}
------------------------------------------------------------------
package testgprs;
import java.io.*;
import javax.microedition.io.*;
public class GprsThread
extends Thread {
boolean running = true;
public GprsThread() {
}
public void run() {
System.out.println("GPRS Data thread run");
while (running) {
HttpConnection c = null;
InputStream is = null;
System.out.println("Connecting to www.minix.de");
// String url = "http://193.118.193.73:16000/ModuleServlet?status=" + ModuleStatus.getInstance().getHtmlStatus();
String url = "http://www.minimx.de:8080/WMServer/ModuleServlet?status=" +
ModuleStatus.getInstance().getHtmlStatus();
// String url = "http://www.minimx.de/";
try {
c = (HttpConnection) Connector.open(url);
// Getting the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
is = c.openInputStream();
// Get the ContentType
String type = c.getType();
// Get the length and process the data
System.out.println("Get the length and process the data");
int ch;
while ( (ch = is.read()) != -1) {
System.out.print( (char) ch);
}
System.out.println("");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally {
try {
if (is != null) {
is.close();
}
if (c != null) {
c.close();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
// Done for now... Sleep until we need to send the data again...
// 1 minute here...
try {
sleep(60000);
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Gprs Thread waking up");
}
System.out.println("GPRS thread terminate");
}
/**
* Used to kill the thread
*/
public void stopRunning() {
running = false;
}
}
------------------------------------------------------------------
package testgprs;
import java.io.*;
import com.siemens.icm.io.*;
public final class ModuleResources {
// Hold the singleton instance of this variable:
private static ModuleResources instance = null;
private static String gprsPoint = "sonofon.dk";
private static String gprsUser = "sonofon";
private static String gprsPass = "sonofon";
private ModuleResources() {
// Load the resources when constructed..
// This needs to talk to the resources file on the flash file system
String resources = "";
try {
File f = new File();
f.open("resources.txt");
byte[] file = new byte[f.length()];
f.read(file, 0, file.length);
f.close();
f = null;
char[] res = new char[file.length];
for (int i = 0; i < file.length; i++) {
res[i] = (char) file[i];
}
resources = String.valueOf(res);
res = null;
file = null;
}
catch (IOException ioe) {
ioe.printStackTrace();
}
// Now we have the resources...
// Parse out the correct information
try {
String resPoint = resources.substring(
resources.indexOf("<gprspoint>") + 11,
resources.indexOf("</gprspoint>"));
gprsPoint = resPoint;
resPoint = null;
}
catch (StringIndexOutOfBoundsException sioobe) {
sioobe.printStackTrace();
}
try {
String resUser = resources.substring(
resources.indexOf("<gprsuser>") + 10,
resources.indexOf("</gprsuser>"));
gprsUser = resUser;
resUser = null;
}
catch (StringIndexOutOfBoundsException sioobe) {
sioobe.printStackTrace();
}
try {
String resPass = resources.substring(
resources.indexOf("<gprspass>") + 10,
resources.indexOf("</gprspass>"));
gprsPass = resPass;
resPass = null;
}
catch (StringIndexOutOfBoundsException sioobe) {
sioobe.printStackTrace();
}
}
/**
* Get the static instance of this class and return it.
* @return Instance of this class.
*/
public static ModuleResources getInstance() {
System.out.println("getInstance()");
if (instance == null) {
instance = new ModuleResources();
}
return instance;
}
public String getGprsUser() {
System.out.println("getGprsUser");
return gprsUser;
}
/**
* Return the GPRS access point.
* @return gprs access point.
*/
public String getGprsPoint() {
System.out.println("getGprsPoint()");
return gprsPoint;
}
/**
* Return the password for GPRS access.
* @return password.
*/
public String getGprsPass() {
System.out.println("getGprsPass");
return gprsPass;
}
}
------------------------------------------------------------------
package testgprs;
import com.siemens.icm.io.*;
public final class ModuleStatus {
private static int counter = 0;
private static SyncAtCommand atc = null;
private static ModuleStatus instance = null;
private static int previousCounter = 0;
/**
* Public constructor.
*/
private ModuleStatus() {
}
/**
* Static method to return the single instance of the class.
* @return current instance.
*/
public static ModuleStatus getInstance() {
System.out.println("getInstance()");
if (instance == null) {
instance = new ModuleStatus();
}
return instance;
}
/**
* Provide the singleton class with a ATCommand to use.
* @param atc The ATCommand to use.
*/
public void registerAtCommand(SyncAtCommand atc) {
System.out.println("registerAtCommand()");
this.atc = atc;
}
/**
* Get the status of the module and counter in a suitable format for
* SMS text messages.
* @return SMS formatted status.
*/
public synchronized String getStatus() {
System.out.println("getStatus()");
String signal = "?";
try {
signal = atc.send("AT+CSQ\r");
if (signal.indexOf("+CSQ:") >= 0) {
int signalStart = signal.indexOf(":") + 2;
int signalEnd = signal.indexOf(",");
signal = signal.substring(signalStart, signalEnd);
}
}
catch (ATCommandFailedException atcfe) {
atcfe.printStackTrace();
}
return "Counter : " + counter + " -- Signal : " + signal;
}
/**
* A More complicated module status in HTML format that is encoded for use
* over a URL to a server.
* @return URL Encoded
*/
public synchronized String getHtmlStatus() {
System.out.println("getHtmlStatus()");
String signal = "Not known";
try {
System.out.println("Sending AT+CSQ");
signal = atc.send("AT+CSQ\r");
if (signal.indexOf("+CSQ:") >= 0) {
System.out.println("Signal is greater than 0");
int signalStart = signal.indexOf(":") + 2;
int signalEnd = signal.indexOf(",");
signal = signal.substring(signalStart, signalEnd);
}
}
catch (ATCommandFailedException atcfe) {
atcfe.printStackTrace();
}
StringBuffer sb = new StringBuffer();
int currentCounter = counter;
sb.append("<p>Module%201<br/>");
sb.append("<b>Module%20is%20functioning%20correctly</b><br/></p>");
sb.append("<p>Previous%20Counter%20=%20" + previousCounter + "<br/>");
sb.append("Current%20Counter%20=%20" + currentCounter + "<p>");
sb.append("<p>Make:%20Siemens<br/>");
sb.append("Model:%20TC45</p>");
sb.append("<p>Signal:%20" + signal + "</p>");
previousCounter = currentCounter;
String rtn = sb.toString();
sb = null;
return rtn;
}
}
------------------------------------------------------------------
package testgprs;
import com.siemens.icm.io.*;
/**
* <p>Title: SyncAtCommand</p>
* <p>Description: Extends ATCommand and presents user with a synchronized send method
* to make ATCommand use Thread Safe.</p>
* <p>Company: Siemens</p>
*/
public class SyncAtCommand
extends ATCommand {
SyncAtCommand(boolean csdSupport) throws ATCommandFailedException {
super(csdSupport);
}
public synchronized String send(String ATCmd) throws ATCommandFailedException {
return super.send(ATCmd);
}
}
------------------------------------------------------------------
Her er udskriften fra konosollen når jeg kører JAD filen på TC45
OK
getInstance()
registerAtCommand()
getInstance()
getGprsPoint()
getInstance()
getGprsUser
getInstance()
getGprsPass
GPRS Setup : OK
startApp
GPRS Data thread run
Connecting to www.minix.de
Module started
java.io.IOException
at com.sun.cldc.io.j2me.socket.Protocol.openPrim(+19)
at com.sun.midp.io.InternalConnector.openPrim(+124)
at com.sun.midp.io.InternalConnector.openInternal(+39)
at com.sun.midp.io.j2me.http.Protocol.connect(+79)
at com.sun.midp.io.j2me.http.Protocol.openInputStream(+64)
at testgprs.GprsThread.run(+40)
Nogen der kan se hvad problemet er.
