2012-03-17 3 views
0

TC65 모뎀을 사용하여 원격 장치의 온도를 모니터링하려고합니다. 요청하려면 'C'를 마지막에 캐리지 리턴과 함께 보내야합니다. 문제는, 나는 단지 내 전화로 이것을 얻는다 : "이것은 테스트 SMS입니다. 현재 온도는"내가 요청한 온도없이 "입니다. HyperTeminal을 사용하여 온도 조절기와 문제없이 통신하려고했습니다.직렬 통신의 inStream.read

instream.read를 도와 주시겠습니까? 출력은 두 배입니다 (두 자리 소수로 정확함).

여기 내 코드가 있습니다. 감사.

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'); 

이없는 숫자 문자열에, corrensponding 문자로 ch 변환합니다.

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

는 다음 시도

관련 문제