why I get just numbers in UCS2 how can I fixed at commands and c#?

霸气de小男生 提交于 2019-12-19 11:49:15

问题


I am having a problem with reading my sms through putty, Its beacuse I type AT+CMGL="ALL" but the message(text) and number are just numbers, I read that my gms modem nokia s10 uses UCS2, but I dont know what to do here? how can I read my message intead of just seeing numbers?? help please

Also I am using this code from codeproject and I changed this line but It is the same result as putty just number in 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;    

    }

回答1:


Notice that AT+CSCS only affects string parameters to commands and responses. In the case of AT+CMGL the content of the message is not a string, but a <data> format. See the 27.005 specification for more details on that format, it is a bit complicated (only pay attention to the first In the case of SMS part, ignore the second In the case of CBS part).

But the short version of it is that for UCS-2 you will get the data hex encoded (e.g. two characters '2' and 'A' represents one byte with value 0x2A (ASCII/UTF-8 character '*')). So you should decode 4 and 4 received bytes as the hex encoding of the 16 bits in a UCS-2 character.

So decode into a byte array and then convert to string, see Appleman1234's answer for that (his answer does not address the core issue, namely the hex decoding).




回答2:


To convert from the UCS-2 encoding store the result (input) in a byte array instead of a string and then call

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

If the UCS-2 encoding is Big Endian then change System.Text.Encoding enc = Encoding.Unicode; to System.Text.Encoding enc = Encoding.BigEndianUnicode;.

Related resources include:

  • Unicode and .NET
  • C# big-endian UCS-2


来源:https://stackoverflow.com/questions/9587332/why-i-get-just-numbers-in-ucs2-how-can-i-fixed-at-commands-and-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!