Sending Unicode Messages (such as in Persian and Arabic) in C# using AT Commands through GSM Modem

*爱你&永不变心* 提交于 2019-11-27 23:50:43
Yasser Mohseni

I have finally found out how to resolve this problem. As I said in in the "Additional Information" section of my question, sending line ends with line feeds caused this mismatching between SerialPort in C# and AT commands in hyperterminal for sending Unicode messages. I have just replaced \r with \n line feeds. The modified code is as follow:

GSMPort.Write("AT\n");
Thread.Sleep(1000);
GSMPort.Write("AT+CSCS=\"UCS2\"\n");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGF=1\n");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGS=\"" + destinationNumber + "\"\n");
Thread.Sleep(1000);
GSMPort.Write("0633064406270645" + "\x1A"); 

Leave SerialPort.Encoding and SerialPort.NewLine properties unchanged. It is not necessary to change their default values, just set AT+CSCS="UCS2" to send messages in Unicode format.

The default encoding for SerialPort is Encoding.ASCII. Either set SerialPort.Encoding to an encoding to supports the character set you're using (like Encoding.UTF32) or use SerialPort.Write(char[], int, int) and convert your Unicode string to bytes in whatever way'd prefer.

First of all check your modem support unicode then change your code to this
we have to specify the correct DCS (Data Coding Scheme) for Unicode messages, which is 0x08.

We can set this value by changing the fourth parameter of the AT+CSMP command to '8':

AT+CSMP=1,167,0,8

    GSMPort.Write("AT\r");
    Thread.Sleep(1000);
    GSMPort.Write("AT+CSCS=\"UCS2\"\r");
    Thread.Sleep(1000);
    GSMPort.Write("AT+CMGF=1\r");
    Thread.Sleep(1000);
    GSMPort.Write("AT+CSMP=1,167,0,8\r"); //AT+CSMP=1,167,0,8
    Thread.Sleep(1000);
    GSMPort.WriteLine("AT+CMGS=\"" + destinationNumber + "\"\r\n");
    Thread.Sleep(1000);
    GSMPort.WriteLine("0633064406270645" + "\x1A");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!