通过GSM调制解调器在短信中发送utf-8字符

时间:2016-01-17 18:03:02

标签: c# utf-8

我正在使用 Nokia 5228 通过COM端口发送短信,当我发送UTF-8字符时出现错误的符号。英语字符很好用。
我该如何解决这个问题?

        public static string SMSMessage = "Привет";
        public static string CellNumber = "number...";

        private void Form1_Load(object sender, EventArgs e)
        {
            sp = new SerialPort();
            sp.PortName = "COM12";
            sp.Encoding = UTF8Encoding.UTF8;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (!sp.IsOpen)
                {
                    sp.Open();
                    this.sp.WriteLine(@"AT" + (char)(13));
                    Thread.Sleep(200);
                    this.sp.WriteLine("AT+CMGF=1" + (char)(13));
                    Thread.Sleep(200);
                    this.sp.WriteLine(@"AT+CMGS=""" + CellNumber + @"""" + (char)(13));
                    Thread.Sleep(200);
                    this.sp.WriteLine(SMSMessage + (char)(26));
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Exception : {0}", ex.Message), "Port Error");
            }
        }

1 个答案:

答案 0 :(得分:1)

问题是,我必须使用UCS2。

this.sp.WriteLine(StringToUCS2("Привет, привіт !@#%") + char.ConvertFromUtf32(26));
public static string StringToUCS2(string str)
        {
            UnicodeEncoding ue = new UnicodeEncoding();
            byte[] ucs2 = ue.GetBytes(str);

            int i = 0;
            while (i < ucs2.Length)
            {
                byte b = ucs2[i + 1];
                ucs2[i + 1] = ucs2[i];
                ucs2[i] = b;
                i += 2;
            }
            return BitConverter.ToString(ucs2).Replace("-", "");
        }