111,129
社区成员
发帖
与我相关
我的任务
分享string strTemp = "";
SerialPort sp = new SerialPort();
serialPort.BaudRate = baudRate;
serialPort.PortName = "COM" + id.ToString();
serialPort.DataBits = 8;
serialPort.Open();
strTemp = "AT+CMGF=0\r";
serialPort.Write(Encoding.ASCII.GetBytes(strTemp), 0, Encoding.ASCII.GetBytes(strTemp).Length);
Thread.Sleep(500);
byte[] buffer1 = new byte[serialPort.BytesToRead];
serialPort.Read(buffer1, 0, buffer1.Length);
string str = Encoding.ASCII.GetString(buffer1);
if (str.Contains("OK"))
{
de.smsDecodedsms("8613800200500", 接收短信的号码, "abc你好!");
strTemp = string.Format("AT+CMGS={0}\r", de.nLength);//
serialPort.Write(Encoding.ASCII.GetBytes(strTemp), 0, Encoding.ASCII.GetBytes(strTemp).Length);
Thread.Sleep(500);
byte[] buffer2 = new byte[serialPort.BytesToRead];
serialPort.Read(buffer2, 0, buffer2.Length);
string str = Encoding.ASCII.GetString(buffer2);
if (str.Contains(">"))
{
strTemp = de.smsDecodedsms("8613800200500", 接收短信的号码, "abc你好!") + "\x01a";
serialPort.Write(Encoding.ASCII.GetBytes(strTemp), 0, Encoding.ASCII.GetBytes(strTemp).Length);
}
}
serialPort.Close();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sms
{
class PDUDecoding
{
public string Phone = "",
SendContent = "",
SCA = "";
public DateTime sendTime;
GSMCode code;
public PDUDecoding() { }
///
public enum GSMCode
{
Bit7 = 0,
Bit8 = 1,
UCS2 = 2
}
///
public bool DecodingMsg(string SMS)
{
try
{
string s = SMS;
int iLength = int.Parse(s.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
if (iLength > 0)
{
SCA = "";
if (s.Substring(2, 2) == "91")
{
SCA = "+";
iLength--;
}
for (int i = 0; i < iLength * 2; i += 2)
{
SCA += s.Substring(5 + i, 1);
SCA += s.Substring(4 + i, 1);
}
if (SCA.EndsWith("F")) SCA = SCA.Remove(SCA.Length - 1, 1);
} s = s.Remove(0, iLength * 2 + 6);
Phone = "";
iLength = int.Parse(s.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
if (s.Substring(2, 2) == "91")
{
Phone = "+";
}
if (iLength % 2 == 1) iLength++;
for (int i = 0; i < iLength; i += 2)
{
Phone += s.Substring(5 + i, 1);
Phone += s.Substring(4 + i, 1);
}
if (Phone.EndsWith("F")) Phone = Phone.Remove(Phone.Length - 1, 1); s = s.Remove(0, iLength + 6);
if (s.Substring(0, 2) == "08")
code = GSMCode.UCS2;
else if (s.Substring(0, 2) == "00")
code = GSMCode.Bit7;
else
code = GSMCode.Bit8; s = s.Remove(0, 2);
sendTime = new DateTime(int.Parse("20" + s.Substring(1, 1) + s.Substring(0, 1)),
int.Parse(s.Substring(3, 1) + s.Substring(2, 1)),
int.Parse(s.Substring(5, 1) + s.Substring(4, 1)),
int.Parse(s.Substring(7, 1) + s.Substring(6, 1)),
int.Parse(s.Substring(9, 1) + s.Substring(8, 1)),
int.Parse(s.Substring(11, 1) + s.Substring(10, 1)));
s = s.Remove(0, 16);
if (code == GSMCode.Bit7)
{
SendContent = DecodingBit7(s);
}
else if (code == GSMCode.UCS2)
{
SendContent = DecodingUCS2(s);
//SendContent = DecodingBit7(s);
}
else
{
SendContent = DecodingBit8(s);
}
return true;
}
catch
{
return false;
}
}///
string DecodingBit8(string s)
{
byte[] buf = new byte[s.Length / 2];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i += 2)
{
buf[i / 2] = byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return Encoding.ASCII.GetString(buf).Replace("\0", "");
}///
string DecodingBit7(string s)
{
int iByte = 0;
int iLeft = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < s.Length; i += 2)
{
byte bSrc = byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
sb.Append((((bSrc << iByte) | iLeft) & 0x7f).ToString("X2"));
iLeft = bSrc >> (7 - iByte);
iByte++;
if (iByte == 7)
{
sb.Append(iLeft.ToString("X2"));
iByte = 0;
iLeft = 0;
}
}
string sReturn = sb.ToString();
byte[] buf = new byte[sReturn.Length / 2];
for (int i = 0; i < sReturn.Length; i += 2)
{
buf[i / 2] = byte.Parse(sReturn.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return System.Text.Encoding.ASCII.GetString(buf).Replace("\0", "");
}///
string DecodingUCS2(string s)
{
byte[] buf = new byte[s.Length];
for (int i = 0; i < s.Length; i += 4)
{
buf[i / 2] = byte.Parse(s.Substring(2 + i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
buf[i / 2 + 1] = byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return Encoding.Unicode.GetString(buf).Replace("\0", "");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class PDUEncoding
{
public string nLength;
public PDUEncoding() { }
public string smsPDUEncoded(string srvContent)
{
Encoding encodingUTF = System.Text.Encoding.BigEndianUnicode;
string s = null;
byte[] encodedBytes = encodingUTF.GetBytes(srvContent);
for (int i = 0; i < encodedBytes.Length; i++)
{
s += BitConverter.ToString(encodedBytes, i,1);
}
s = String.Format("{0:X2}{1}", s.Length / 2, s);
return s;
}
public string smsEncodedCenterNumber(string srvCenterNumber)
{
string s = null;
int nLength = srvCenterNumber.Length;
for (int i = 1; i < nLength; i += 2)
{
s += srvCenterNumber[i];
s += srvCenterNumber[i - 1];
}
if (!(nLength % 2 == 0))
{
s += 'F';
s += srvCenterNumber[nLength - 1];
}
s = String.Format("91{0}", s);
s = String.Format("{0:X2}{1}", s.Length / 2, s);
return s;
}
///
///
public string smsEncodedNumber(string srvNumber)
{
string s = null;
if (!(srvNumber.Substring(0, 2) == "86"))
{
srvNumber = String.Format("86{0}", srvNumber);
}
int nLength = srvNumber.Length;
for (int i = 1; i < nLength; i += 2)
{
s += srvNumber[i];
s += srvNumber[i - 1];
}
if (!(nLength % 2 == 0))
{
s += 'F';
s += srvNumber[nLength - 1];
}
return s;
}
public string smsEncodedsms(string strCenterNumber, string strNumber, string strSMScontent) //smsDecodedsms
{
string s = String.Format("{0}11000D91{1}000800{2}", smsEncodedCenterNumber(strCenterNumber), smsEncodedNumber(strNumber), smsPDUEncoded(strSMScontent));
nLength = String.Format("{0:D2}", (s.Length - smsEncodedCenterNumber(strCenterNumber).Length) / 2);
return s;
}
public bool IsPDU(string SMS)
{
if (SMS.Substring(40, 2) != "08")
return false;
return true;
}
///
public string[] GetEverySMS(string SMS)
{
char[] str = "\n".ToCharArray();
string[] temp = SMS.Split(str);
return temp;
}
///
public string GetTelphone(string SMS)
{
string tel = SMS.Substring(24, 14);
string s = "";
for (int i = 0; i < 11; i += 2)
{
s += tel[i + 1];
s += tel[i];
}
s += tel[tel.Length - 1];
return s;
}
///
public string GetDataTime(string SMS)
{
string time = SMS.Substring(42, 12);
string s = "";
for (int i = 0; i < 11; i += 2)
{
s += time[i + 1];
s += time[i];
}
string t = s.Substring(0, 2) + "" + s.Substring(2, 2) + "" + s.Substring(4, 2) + "" + s.Substring(6, 2) + ":" + s.Substring(8, 2) + ":" + s.Substring(10, 2);
return t;
}
///
public string GetContent(string SMS)
{
string c = "";
string len = SMS.Substring(56, 2);
int length = System.Convert.ToInt16(len, 16);
length *= 2;
string content = SMS.Substring(58, length);
for (int i = 0; i < length; i += 4)
{
string temp = content.Substring(i, 4);
int by = System.Convert.ToInt16(temp, 16);
char ascii = (char)by;
c += ascii.ToString();
}
return c;
}
public string GetTextContent(string SMS)
{
string str = "";
string c = "";
byte by;
char ascii;
int i;
SMS = SMS.Replace("\r", "");
SMS = SMS.Replace("\n", "");
//"+CMGR: 1,,290891683108501305F0040D91683106469011F70000701062112250230A42A1F01993CD6C351AOK"
//+CMGR: 1,,29 OK
string content = SMS.Substring(70,SMS.Length - 72);
for (i = content.Length - 2; i >= 0; i -= 2)
{
by = Convert.ToByte(content.Substring(i, 2), 16);
str += Convert.ToString(by, 2).PadLeft(8, '0');
}
for (i = str.Length - 7; i >= 0; i -= 7)
{
by = Convert.ToByte(str.Substring(i, 7), 2);
ascii = (char)by;
c += ascii.ToString();
}
return c;
}
/// <summary>
/// 使用7-bit进行编码
/// </summary>
/// <param name="s">要编码的英文字符串</param>
/// <returns>信息长度及编码后的字符串</returns>
static public string EncodingBit7(string s)
{
int iLeft = 0;
string sReturn = "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
// 取源字符串的计数值的最低3位
int iChar = i & 7;
byte bSrc = (byte)char.Parse(s.Substring(i, 1));
// 处理源串的每个字节
if (iChar == 0)
{
// 组内第一个字节,只是保存起来,待处理下一个字节时使用
iLeft = (int)char.Parse(s.Substring(i, 1));
}
else
{
// 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节
sReturn = (bSrc << (8 - iChar) | iLeft).ToString("X4");
// 将该字节剩下的左边部分,作为残余数据保存起来
iLeft = bSrc >> iChar;
// 修改目标串的指针和计数值 pDst++;
sb.Append(sReturn.Substring(2, 2));
}
}
sb.Append(sReturn.Substring(0, 2));
return (sb.Length / 2).ToString("X2") + sb.ToString();
}
}
serialPort.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"), 0, Encoding.ASCII.GetBytes("AT+CSCA?\r").Length);短信中心号码的话可以先获取存下来string strTemp = "";
SerialPort sp = new SerialPort();
serialPort.BaudRate = baudRate;
serialPort.PortName = "COM" + id.ToString();
serialPort.DataBits = 8;
serialPort.Open();
strTemp = "AT+CMGF=0\r";
serialPort.Write(Encoding.ASCII.GetBytes(strTemp), 0, Encoding.ASCII.GetBytes(strTemp).Length);
Thread.Sleep(500);
byte[] buffer1 = new byte[serialPort.BytesToRead];
serialPort.Read(buffer1, 0, buffer1.Length);
string str = Encoding.ASCII.GetString(buffer1);
if (str.Contains("OK"))
{
strTemp = string.Format("AT+CMGS={0}\r", de.smsDecodedsms("8613800200500", 接收短信的号码, "abc你好!").Length);
serialPort.Write(Encoding.ASCII.GetBytes(strTemp), 0, Encoding.ASCII.GetBytes(strTemp).Length);
Thread.Sleep(500);
byte[] buffer2 = new byte[serialPort.BytesToRead];
serialPort.Read(buffer2, 0, buffer2.Length);
string str = Encoding.ASCII.GetString(buffer2);
if (str.Contains(">"))
{
strTemp = de.smsDecodedsms("8613800200500", 接收短信的号码, "abc你好!") + "\x01a";
serialPort.Write(Encoding.ASCII.GetBytes(strTemp), 0, Encoding.ASCII.GetBytes(strTemp).Length);
}
}
serialPort.Close();
楼主,改了下你的,试试看