我正在使用AT命令在C#.Net中开发GSM调制解调器(D-Link DWM-156)的应用程序.我在发送Unicode消息时遇到问题(例如用波斯语或阿拉伯语写的消息).这是我的计划的核心:
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\r");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGF=1\r");
Thread.Sleep(1000);
GSMPort.Write("AT+CMGS=\"" + destinationNumber + "\"\r\n");
Thread.Sleep(1000);
GSMPort.Write(shortMessage+ "\x1A");
Run Code Online (Sandbox Code Playgroud)
它适用于英文和ASCII字母.我已阅读本文,可以使用AT命令在Hyperterminal中发送Unicode消息:
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 (???? ?????) …Run Code Online (Sandbox Code Playgroud) 我在我的C#代码中调用了一个非托管函数.
该功能的声明如下:
int myFun(unsigned char* inputBuffer, unsigned char* &outputBuffer);
Run Code Online (Sandbox Code Playgroud)
我使用这个函数如下:
[DllImport("myDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int myFun([In] byte[] inputBuffer, out IntPtr outputBuffer);
byte[] a = Encoding.ASCII.GetBytes("sampletext!");
IntPtr b;
res = myFun(a, out b);
Run Code Online (Sandbox Code Playgroud)
虽然我提到[InAttribute]过byte[] inputBuffer,但该功能仍然改变了它的值a.似乎CLR正在使用其自己的默认编组行为.我用过,byte[]因为它是C#的等价物unsigned char*.
当我byte[]用char[]CLR 替代时,将遵循我的In-Out属性.
我非常感谢你的帮助.有关更多信息,请阅读此msdn页面.