通过 GSM 调制解调器使用 AT 命令在 C# 中发送 Unicode 消息(例如波斯语和阿拉伯语) [英] Sending Unicode Messages (such as in Persian and Arabic) in C# using AT Commands through GSM Modem

查看:27
本文介绍了通过 GSM 调制解调器使用 AT 命令在 C# 中发送 Unicode 消息(例如波斯语和阿拉伯语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AT 命令在 C#.Net 中为 GSM 调制解调器(D-Link DWM-156)开发应用程序.我在发送 Unicode 消息(例如用波斯语或阿拉伯语编写的消息)时遇到问题.这是我程序的核心:

I am developing an application for GSM Modems (D-Link DWM-156) in C#.Net using AT commands. I have a problem sending Unicode messages (Such as messages written in Persian or Arabic). Here it is the core of my program:

SerialPort GSMPort = new SerialPort();

GSMPort.PortName = "COM6";
GSMPort.BaudRate = 9600;
GSMPort.Parity = Parity.None;
GSMPort.DataBits = 8;
GSMPort.StopBits = StopBits.One;
GSMPort.Handshake = HandShake.RequestToSend;
GSMPort.DtrEnable = true;
GSMPort.RtsEnable = true;

GSMPort.Open();

GSMPort.Write("AT
");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGF=1
");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGS="" + destinationNumber + ""
");
Thread.Sleep(1000);
GSMPort.Write(shortMessage+ "x1A");

它适用于英文和 ASCII 字母.我已阅读这篇文章 并且可以使用 AT 命令在超级终端中发送 Unicode 消息:

It works fine for English and ASCII letters. I have read this article and can send Unicode messages in Hyperterminal using AT commands:

AT [Enter]
OK
AT+CSCS="UCS2" or AT+CSCS="HEX" [Enter] ---> We have to convert our message to hex 
OK
AT+CMGF=1 [Enter]
OK
AT+CMGS="destinationNumber" [Enter]
> 0633064406270645002006450631062D06280627 ---> The hex format of our message (سلام مرحبا)
+CMGS: 139
OK

由于此命令正常工作,我已将 GSMPort.Write("AT+CSCS="UCS2" "); 添加到我的应用程序中,并尝试发送 06450631 062D 0628 0627(مرحبا)或0633 0644 0627 0645(سلام).

Since this commands worked correctly, I have added GSMPort.Write("AT+CSCS="UCS2" "); to my application, and tried to send 0645 0631 062D 0628 0627 (مرحبا) or 0633 0644 0627 0645 (سلام).

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

但与超级终端中的 AT 命令不同,目的地不会收到正确的字母.C# 代码和超级终端代码中的这些 AT 命令有什么区别?如果您能帮我解决这个问题,我将不胜感激.

But unlike AT commands in Hyperterminal, destination does not receive correct letters. What is the difference between these AT commands in C# code and Hyperterminal code? I will appreciate if you help me to resolve this problem.

其他信息:如您所知,通过超级终端和使用 AT 命令发送 Unicode 消息(例如波斯字母)没有问题.在下面的代码中,我尝试发送 سلام,0633064406270645"是它的十六进制形式.但是我在手机中收到 □□□□.

Additional information: As you know I have no problem for sending Unicode messages (such as Persian letters) by hyperterminal and using AT commands. In below code I try to send سلام, and "0633064406270645" is the hexadecimal form of it. But I receive □□□□ in my phone.

GSMPort.Encoding = UnicodeEncoding.GetEncoding(1256);
GSMPort.Write("AT
");
Thread.Sleep(1000);
GSMPort.Write("AT+CSCS="UCS2"
");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGF=1
");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGS="" + destinationNumber + ""
");
Thread.Sleep(1000);
GSMPort.Write("0633064406270645" + "x1A");

另一方面,在超级终端环境中,一切顺利,我收到了 سلام.

In the hyperterminal environment, On the other hand, everything goes fine and I receive سلام.

AT [Enter]
OK
AT+CSCS="UCS2"
OK
AT+CMGF=1 [Enter]
OK
AT+CMGS="destinationNumber" [Enter]
> 0633064406270645 [Ctrl+Z]
+CMGS: 139
OK

我注意到在超级终端属性中,如果我检查发送行以换行结束"(它在属性->设置-> ASCII设置中,请检查下图),我将遇到与以下相同的问题我有 C# 代码.我认为在 GSMPort.Write 中默认启用以换行符发送行结束"?如果是,我该如何禁用它?

I have noticed that in the hyperterminal properties if I check the "send line ends with line feeds" (it is in properties->Settings-> ASCII Setups, please check the picture below), I will have the same problem as I had in C# code. I think "send line ends with line feeds" is enabled in GSMPort.Write by default? If yes, how can I disable it?

推荐答案

我终于找到了解决这个问题的方法.正如我在我的问题的附加信息"部分中所说的那样,以换行符发送行结束会导致 C# 中的 SerialPort 与超级终端中用于发送 Unicode 消息的 AT 命令之间的这种不匹配.我刚刚用 换行符替换了 .修改后的代码如下:

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 with line feeds. The modified code is as follow:

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

保持 SerialPort.EncodingSerialPort.NewLine 属性不变.无需更改它们的默认值,只需设置AT+CSCS="UCS2" 即可以Unicode 格式发送消息.

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.

这篇关于通过 GSM 调制解调器使用 AT 命令在 C# 中发送 Unicode 消息(例如波斯语和阿拉伯语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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