串行通信中的inStream.read [英] inStream.read in serial communications

查看:171
本文介绍了串行通信中的inStream.read的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TC65调制解调器监视远程设备的温度.要请求,必须在最后加上回车符来发送"C".问题是,我只能在手机上收到此信息:这是测试短信.当前温度为",没有我要求的温度.我尝试使用HyperTeminal与恒温器通信没有问题.

I'm trying to monitor temperature from a remote device using a TC65 modem. To request, 'C' has to be sent with a carriage return at the end. The problem is, I only get this on my phone: "This is test sms. The current temperature is " without the temperature I requested. I tried communicating with the thermostat using HyperTeminal without a problem.

您能帮我instream.read吗?输出为两倍(精确到两位小数).

Could you help me with instream.read? The output is double (accurate by two decimals).

这是我的代码.谢谢.

Here's my code. Thanks.

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();
  }
}

推荐答案

问题在这里:

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

它将ch转换为对应的字符,而不转换为数字字符串.

It converts ch to corrensponding character, not to number string.

请尝试以下操作:

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

这篇关于串行通信中的inStream.read的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆