【问题标题】:why I get just numbers in UCS2 how can I fixed at commands and c#?为什么我在 UCS2 中只得到数字我怎样才能修复命令和 c#?
【发布时间】:2012-03-06 16:02:37
【问题描述】:

我在通过 putty 读取短信时遇到问题,因为我输入 AT+CMGL="ALL" 但消息(文本)和数字只是数字,我读到我的 gms 调制解调器 nokia s10 使用 UCS2,但是我不知道在这里做什么?我怎样才能阅读我的信息而不是只看到数字?请帮忙

我也在使用 codeproject 中的这段代码,我改变了这一行,但结果与 putty 相同,只是 ucs2 中的数字

 public ShortMessageCollection ReadSMS(SerialPort port, string p_strCommand)
    {

        // Set up the phone and read the messages
        ShortMessageCollection messages = null;
        try
        {

            #region Execute Command
            // Check connection
            ExecCommand(port,"AT", 300, "No phone connected");
            // Use message format "Text mode"
            ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
            // Use character set "PCCP437"
            **ExecCommand(port, "AT+CSCS=\"UCS2\"", 300, "Failed to set character set.")**;
            // Select SIM storage
            ExecCommand(port,"AT+CPMS=\"SM\"", 300, "Failed to select message storage.");
            // Read the messages
            string input = ExecCommand(port, p_strCommand, 5000, "Failed to read the messages.");
            #endregion

            #region Parse messages
            messages = ParseMessages(input);
            #endregion

        }
        catch (Exception ex)
        {
            throw ex;
        }

        if (messages != null)
            return messages;
        else
            return null;    

    }

【问题讨论】:

    标签: c# sms nokia at-command


    【解决方案1】:

    注意AT+CSCS 只影响命令和响应的字符串 参数。在AT+CMGL 的情况下,消息的内容不是字符串,而是<data> 格式。有关该格式的更多详细信息,请参阅27.005 规范,它有点复杂(只注意第一个In the case of SMS 部分,忽略第二个In the case of CBS 部分)。

    但它的简短版本是,对于 UCS-2,您将获得十六进制编码的数据(例如,两个字符 '2''A' 代表一个字节,其值为 0x2A(ASCII/UTF-8 字符 @987654331 @))。因此,您应该将接收到的 4 和 4 个字节解码为 UCS-2 字符中 16 位的十六进制编码。

    所以解码成字节数组,然后转换成字符串,见 Appleman1234 的answer (他的回答没有解决核心问题,即十六进制解码)。

    【讨论】:

      【解决方案2】:

      要从 UCS-2 编码转换,将结果(输入)存储在字节数组而不是字符串中,然后调用

      System.Text.Encoding enc =  Encoding.Unicode;
      string myString = enc.GetString(myByteArray);
      

      如果 UCS-2 编码为 Big Endian,则将 System.Text.Encoding enc = Encoding.Unicode; 更改为 System.Text.Encoding enc = Encoding.BigEndianUnicode;.

      相关资源包括:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-06
        • 2016-01-22
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 2020-11-22
        • 1970-01-01
        相关资源
        最近更新 更多