我使用HUAWEI USB调制解调器和下面的代码成功发送SMS,但长度小于140(请参见代码pls-double lenMes = textsms.Length / 2;
)。
但是如今,我看到了非常大的SMS消息。
所以我想知道AT指令到底出了什么问题,或者我的硬件很旧,所以我无法发送大型短信。
有什么线索吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace sendSMSPDU
{
class Program
{
static SerialPort port;
static void Main(string[] args)
{
port = new SerialPort();
Console.WriteLine("Sending SMS");
OpenPort();
bool result;
result = sendSMS("Some text that less 140 is gonna sending OK", " +75434355544");
if (result == true)
{
Console.WriteLine("OK");
}
else
{
Console.WriteLine("ERROR");
}
Console.ReadLine();
port.Close();
}
private static bool sendSMS(string textsms, string telnumber)
{
if (!port.IsOpen) return false;
try
{
System.Threading.Thread.Sleep(500);
port.WriteLine("AT\r\n");
System.Threading.Thread.Sleep(500);
port.Write("AT+CMGF=0\r\n");
System.Threading.Thread.Sleep(500);
}
catch
{
return false;
}
try
{
telnumber = telnumber.Replace("-", "").Replace(" ", "").Replace("+", "");
telnumber = "01" + "00" + telnumber.Length.ToString("X2") + "91" + EncodePhoneNumber(telnumber);
textsms = StringToUCS2(textsms);
string leninByte = (textsms.Length / 2).ToString("X2");
textsms = telnumber + "00" + "08" + leninByte + textsms;
double lenMes = textsms.Length / 2;
if (lenMes < 140) // It sends OK
{
port.Write("AT+CMGS=" + (Math.Ceiling(lenMes)).ToString() + "\r\n");
System.Threading.Thread.Sleep(500);
textsms = "00" + textsms;
port.Write(textsms + char.ConvertFromUtf32(26) + "\r\n");
System.Threading.Thread.Sleep(500);
}
else
{
return false;
}
}
catch
{
return false;
}
try
{
string recievedData;
recievedData = port.ReadExisting();
if (recievedData.Contains("ERROR"))
{
return false;
}
}
catch { }
return true;
}
private static void OpenPort()
{
port.BaudRate = 9600;
port.DataBits = 7;
port.StopBits = StopBits.One;
port.Parity = Parity.Odd;
port.ReadTimeout = 500;
port.WriteTimeout = 500;
//port.Handshake = Handshake.RequestToSend;
//port.DtrEnable = true;
//port.RtsEnable = true;
//port.NewLine = Environment.NewLine;
port.Encoding = Encoding.GetEncoding("windows-1252");
port.PortName = "COM7";
if (port.IsOpen)
port.Close();
try
{
port.Open();
}
catch { }
}
public static string EncodePhoneNumber(string PhoneNumber)
{
string result = "";
if ((PhoneNumber.Length % 2) > 0) PhoneNumber += "F";
int i = 0;
while (i < PhoneNumber.Length)
{
result += PhoneNumber[i + 1].ToString() + PhoneNumber[i].ToString();
i += 2;
}
return result.Trim();
}
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("-", "");
}
}
}
答案 0 :(得分:2)
单个SMS消息限于GSM-7
编码的 160 (或PDU模式下为 152 )个符号。除此之外,如果here中未列出任何符号,则需要使用UCS-2
编码,并且您的消息现在限制为 67 个符号。如果您需要发送更长的消息,欢迎来到SMS PDU mode的“光彩照人的世界”。
因此发送短信很简单:
UCS-2
或GSM-7
编码; PDU
条消息; AT+CMGF=0
)顺序发送它们修改
设计PDU格式的人是一个真正的恶人。真是令人不安的事情,对不起,我不想编写转换代码。但是,我可以用这个存根指向你:
protected void SendMessage(string phoneNumber, string message)
{
const char CR = '\r'; // "Carage Return"
const char CtrlZ = (char)26; // Ctrl+Z character
var header = GeneratePDUHeader(phoneNumber);
foreach (var messagePart in SplitSMSMessage(message))
{
SendToCOM("AT+CMGF=0" + CR);
SendToCOM("AT+CMGS=42" + CR);
SendToCOM($"{header}{messagePart}" + CtrlZ);
}
}
// should return something like "0041000B910000000000F000088C"
protected string GeneratePDUHeader(string phoneNumber) { }
// split long message to parts
protected IEnumerable<string> SplitSMSMessage(string message)
{
var useUCSEncoding = IsUCSEncodingNeeded(message);
var partLength = useUCSEncoding ? 67 : 152;
var messageParts = Enumerable.Range(0, message.Length / partLength)
.Select(i => message.Substring(i * partLength, partLength))
.ToArray();
var referenceNumber = $"{GenerateReferenceNumber():X2}"; // convert to HEX, i.e. "01"
var totalMessagesCount = $"{messageParts.Length:X2}"; // convert to HEX, i.e. "01"
var udhBase = $"050003{referenceNumber}{totalMessagesCount}";
var messageNumber = (char)0;
foreach (var messagePart in messageParts)
{
var udh = $"{udhBase}{++messageNumber}";
var messagePartText = useUCSEncoding ? StringToUCS(messagePart) : StringToGSM7(messagePart);
yield return $"{udh}{messagePartText}";
}
}
private void SendToCOM(string message) { } // writes message to COM port
private bool IsUCSEncodingNeeded(string message) { } // determine if UCS-2 convert is required
private char GenerateReferenceNumber() { } // random number 0-255
private string StringToUCS(string message) { } // convert string to UCS encoding
private string StringToGSM7(string message) { } // convert string to GSM7 encoding (don't forget about padding!)
您可能还会发现此链接很有用:
答案 1 :(得分:0)
我的问题的最佳答案是
How to concat long SMS in GSMComm Library?
https://github.com/welly87/GSMComm
https://www.nuget.org/packages/GSMComm/
与我的HUAWEI GSM调制解调器一起正常工作!哇!