2012-03-17 26 views
0

TC65モデムを使用してリモートデバイスから温度を監視しようとしています。リクエストするには、最後にキャリッジリターンで 'C'を送信する必要があります。問題は、私は私の電話でこれを得るだけです: "これはテストSMSです。現在の温度は"私が要求した温度なしです。私は問題なくHyperTeminalを使ってサーモスタットと通信しようとしました。シリアル通信のinStream.read

instream.readを手伝ってもらえますか?出力は2倍(2小数で正確)です。

ここに私のコードです。ありがとう。

package example.rs232demo; 

import javax.microedition.midlet.*; 
import java.io.*; 
import javax.microedition.io.*; 
import com.siemens.icm.io.*; 


public class RS232Demo extends MIDlet { 

    CommConnection commConn; 
    InputStream  inStream; 
    OutputStream outStream; 
    private ATCommand ATC; 
    public static String phone = "+97455781868"; 
    public static String message = "This is test sms."; 

    /** 
    * RS232Demo - default constructor 
    */ 
    public RS232Demo() { 
    //System.out.println("RS232Demo: Constructor"); 
    //System.out.println("Available COM-Ports: " + System.getProperty("microedition.commports")); 
    try { 
     //String strCOM = "comm:com0;blocking=on;baudrate=115200"; 
     String strCOM = "comm:com0;blocking=on;baudrate=9600;bitsperchar=7;parity=even"; 
     commConn = (CommConnection)Connector.open(strCOM); 
    //System.out.println("CommConnection(" + strCOM + ") opened"); 
    //System.out.println("Real baud rate: " + commConn.getBaudRate()); 
     inStream = commConn.openInputStream(); 
     outStream = commConn.openOutputStream(); 
    //System.out.println("InputStream and OutputStream opened"); 
    } catch(IOException e) { 
    //System.out.println(e); 
     notifyDestroyed(); 
    } 
    } 

    /** 
    * startApp() 
    */ 
    public void startApp() throws MIDletStateChangeException { 

    int ch = 0; 
    //System.out.println("RS232Demo: startApp"); 
    //System.out.println("Looping back received data, leave with 'Q'..."); 
    try { 
     outStream.write('C'); 
     outStream.write('\r'); 

     ch = inStream.read(); 

    } catch(IOException e) { 
     //System.out.println(e); 
    } 


    try 
     { 
      this.ATC = new ATCommand(false); 
     } 
     catch (ATCommandFailedException ex) 
     { 
      ex.printStackTrace(); 
     } 

     send_Simple_SMS(phone, message, ch); 
     try 
     { 
     this.ATC.release(); 
     } 
     catch(ATCommandFailedException ex) 
     { 
      ex.printStackTrace(); 
     } 

    destroyApp(true); 
    } 


    public void pauseApp() { 
    //System.out.println("RS232Demo: pauseApp()"); 
    } 


public int send_Simple_SMS(String phone, String message, int ch) 
    { 
     int res = -1; 
     String AT = ""; 
     String response = ""; 
     synchronized (System.out) 
     { 
     } 
     if(ATC==null){return res;} 
     try 
     { 
      synchronized (ATC) 
      { 
       ATC.send("AT+CMGF=1\r"); 
       ATC.send("AT+IFC=1,1\r"); 
       response = ""; 
       response = ATC.send("AT+CMGS=?\r"); 
       if (response.trim().indexOf("OK") < 0) 
       { 
        return res; 
       } 
       response = ATC.send("AT+CMGS=" + phone + '\r' + '\n'); 
       //System.out.println("Sending."); 
       response = ATC.send(message + "The current temperature is " + (char)ch + '\032'); 
       //System.out.println("Sent."); 

        if (response.trim().indexOf("OK") >= 0) 
        { 
         res = 0; 
        } 

       ATC.notifyAll(); 

      } 
     } 
     catch (ATCommandFailedException ex) 
     { 
      ex.printStackTrace(); 
      res = -1; 
     } 
     return res; 
    } 



    public void destroyApp(boolean cond) { 
    //System.out.println("RS232Demo: destroyApp(" + cond + ")"); 
    try { 
     inStream.close(); 
     outStream.close(); 
     commConn.close(); 
    //System.out.println("Streams and connection closed"); 
    } catch(IOException e) { 
    //System.out.println(e); 
    } 

    notifyDestroyed(); 
    } 
} 

答えて

0

問題はここにある:

response = ATC.send(message + "The current temperature is " + (char)ch + '\032'); 

それはch文字をcorrenspondingに、ではない数の文字列に変換します。

次のことを試してみてください。

response = ATC.send(message + "The current temperature is " + ch + '\032'); 
関連する問題