GPRS 编程通信

如何通过串口的编程实现GPRS模块在网路上传输数据的系列问题  (讨论)

通过GPRS模块发送MMS 
开放 mm1接口发送彩信(也就是GPRS modem发送彩信) 代码 c#版

mm1接口也就是手机终端和mmsc(中国移动就是http://mmsc.monternet.com)用来发送mms message的接口, GPRS modem当然也是可以使用这一接口发送的.

using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Text;

namespace MMSLib
{
/// <summary>
/// MMSender 的摘要说明。
///
/// </summary>
public class MMSender
{
// 设置参数
string sMmscUrl="http://mmsc.monternet.com";
string sProxyUrl="10.0.0.172:80";

public MMSender()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void SetMMSC(string szUrl)
{
sMmscUrl =szUrl;
}
public void SetProxy(string szUrl)
{
sProxyUrl = szUrl;
}


/* 发送MMS的过程
1> 创建消息发送接口
  MMSender ms = new MMSender();
2> 设置参数属性
  默认属性已经是中国移动参数,因此如果是中国移动用户,以下两个操作可以不需要
  ms.SetMMSC("http://mmsc.monternet.com");
  ms.SetProxy("10.0.0.172:80");
3> 创建消息
  MMessage mm= new MMessage();
4> 设置消息内容
  mm.SetSubject("标题");      // 设置标题
  mm.AddTo("13825271511");    // 添加接收号码,调用一次添加一个接收号码
  mm.AddFile("FileName");     // 添加发送文件,包含文件路径,调用一次添加一个发送文件
5> 发送消息
   string szReult =ms.Send(mm);
  
6> 继续发送其他号码
  mm.ClearTo();
  mm.AddTo("13812345678");
  ms.Send(mm);    
*/


/* 避免协议冲突的设置
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net>
</configuration>
    */
public string Send(MMessage mm)
{
try
{
// 验证参数有效性
WebRequest wReq = WebRequest.Create(sMmscUrl);
HttpWebRequest hReq = (HttpWebRequest)wReq;
wReq.Headers.Clear();
if( sProxyUrl.Length >0)
wReq.Proxy = new WebProxy(sProxyUrl);

wReq.ContentType ="application/vnd.wap.mms-message";
hReq.Accept ="application/vnd.wap.mms-message,text/plain,*/*";
wReq.Method ="POST";
hReq.KeepAlive = false;
hReq.UserAgent = "Nokia6681/2.0 (4.00.15) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1";
// Write Post Dat
byte[] byMM = mm.GetContent();
hReq.ContentLength = byMM.Length;
Stream sReq = wReq.GetRequestStream();
sReq.Write(byMM,0,byMM.Length);
sReq.Close();
// Http Request
WebResponse wRes= wReq.GetResponse();
HttpWebResponse hRes = (HttpWebResponse)wRes;
if ( hRes.StatusCode == HttpStatusCode.OK)
{
Stream sRes = wRes.GetResponseStream();
StreamReader sr = new StreamReader(sRes);
string szResult =sr.ReadToEnd();  // 发送结果
// Parse result sring
return szResult;
}
}
catch(Exception e)
{
throw new Exception(e.Message);
}
return string.Empty;
}
}
}

public class MMessage
{
string Subject ="";
int nSeconds =0; // 设置送达的时间,当前相对时间,以秒为单位
ArrayList lFile = new ArrayList();   // 彩信文件列表
ArrayList lDest = new ArrayList();   // 发送号码集合

static long nSeq =0;

public MMessage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public void SetSubject(string szSubject)
{
Subject =szSubject;
}
public void SetDeliverTime(int nSec)
{
nSeconds = nSec;
}
//
public void AddTo(string Dest)
{
lDest.Add(Dest);
}

public void AddFile(string File)
{
lFile.Add(File);
}

public void ClearTo()
{
lDest.Clear();
}

// 得到二进制编码字节
public byte[] GetContent()
{

byte[] byMms = new byte[0];
// 消息头开始
//X-Mms-Message-Type
byMms = AppendOct(new byte[]{0x8C,0x80},byMms);
//X-Mms-Transaction-ID
byMms = AppendOct(new byte[]{0x98},byMms);
byMms= AppendOct(nSeq.ToString(),byMms);
nSeq++; // 序列号加1

byMms= AppendOct(new byte[]{0x0},byMms);

//X-Mms-MMS-Version
byMms = AppendOct(new byte[]{0x8D,0x90},byMms);
//Date

//From,设置为 Insert-address-token
byMms = AppendOct(new byte[]{0x89,0x01,0x81},byMms);

//To
for(int i=0;i<lDest.Count;i++)
{
byMms= AppendOct(new byte[]{0x97},byMms);
byMms= AppendOct("+86"+ (string)lDest[i] +"/TYPE=PLMN",byMms);
byMms= AppendOct(new byte[]{0x0},byMms);
}
//Subject
if(Subject.Length >0)  // 使用Utf8编码
{
byMms= AppendOct(new byte[]{0x96},byMms);
// Value-length Char-set Text -string
byte[] byLen= new byte[1];
byLen[0] = (byte) (Encoding.UTF8.GetByteCount(Subject) +2);
byMms= AppendOct(byLen,byMms);
// Char-set 为utf-8
byMms= AppendOct(new byte[]{0xEA},byMms);
byMms= AppendOct(Encoding.UTF8.GetBytes(Subject),byMms);
byMms= AppendOct(new byte[]{0x0},byMms);
}
// X-Mms-Delivery-Time,递送时间 = Relative-token Delta-seconds-value
// Relative-token = 0x81
// Delta-seconds-value =Long-integer
// Long-integer = Short-length Multi-octet-integer
if( nSeconds >0)
{
byMms= AppendOct(new byte[]{0x87},byMms);
byte[] bfTime = BitConverter.GetBytes(nSeconds);  // 默认使用Big-endian,需用改为Little-endian
// bfTime改为Little-endian
Array.Reverse(bfTime);
byte[] bfTimeLen = new byte[3];
bfTimeLen[0] = (byte) (bfTime.Length + 2);
bfTimeLen[1] = 0x81;  // 相对时间格式
bfTimeLen[2] = (byte)bfTime.Length;
byMms= AppendOct(bfTimeLen,byMms);
byMms= AppendOct(bfTime,byMms);
}

//Content-Type:application/vnd.wap.multipart.mixed
byMms = AppendOct(new byte[]{0x84,0xA3},byMms);
// 消息体开始(MIME multipart)
// 8.5.2 Multipart Header
// nEntries Uintvar The number of entries in the multipart entity
byte[] byFileCount = new byte[1];
byFileCount[0] = (byte)lFile.Count;
byMms = AppendOct(byFileCount,byMms);
// 8.5.3 Multipart Entry,逐个加入媒体文件
for(int j=0;j<lFile.Count;j++)
{
byMms = AppendOct(GetMmsContent(lFile[j].ToString()),byMms);
}
return byMms;
}

// Tools
// 加入媒体文件到彩信内容中去
private byte[] GetMmsContent(string FileName)
{
// 每一个Multipart Entry由5个部分组成
/* HeadersLen
 * DataLen
 * ContentType
 * Headers
 * Data
 * */
byte[] byHeaders = new byte[0];   // ContentType和Headers组合
byte[] byData = ReadFromFile(FileName);

string FileID = getContentId(FileName);
// Set content-type
if( FileName.EndsWith(".txt"))
{
byHeaders = new byte[1];
byHeaders[0] = (byte) (Encoding.ASCII.GetByteCount(FileID)+5);
byHeaders = AppendOct(new byte[]{0x83,0x85},byHeaders);   // Utf-8
byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID),byHeaders);
byHeaders = AppendOct(new byte[]{0x00},byHeaders);
byHeaders = AppendOct(new byte[]{0x81,0xEA},byHeaders);
}
else if( FileName.EndsWith(".gif"))
{
byHeaders = new byte[]{0x9D};
}
else if( FileName.EndsWith(".mid") || FileName.EndsWith(".midi"))
{
byHeaders = Encoding.ASCII.GetBytes("audio/midi");
byHeaders = AppendOct(new byte[]{0x00},byHeaders);  // 文本需要以0x00结尾
}

// 加入Content-ID和Content-Location
byHeaders = AppendOct(new byte[]{0xC0,0x22,0x3C},byHeaders);
byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID),byHeaders);
byHeaders = AppendOct(new byte[]{0x3E,0x00},byHeaders);
//加入Content-Location
byHeaders = AppendOct(new byte[]{0x8E},byHeaders);
byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID),byHeaders);
byHeaders = AppendOct(new byte[]{0x00},byHeaders);

byte[] byHeaderLen = encodeUintvar( byHeaders.Length);
byte[] byDataLen = encodeUintvar(byData.Length);

byte[] byMmc = new byte[ byHeaderLen.Length + byDataLen.Length + byHeaders.Length +  byData.Length ];
Array.Copy( byHeaderLen,byMmc,byHeaderLen.Length);
Array.Copy( byDataLen,0,byMmc,byHeaderLen.Length,byDataLen.Length);
Array.Copy( byHeaders,0,byMmc,byHeaderLen.Length+byDataLen.Length,byHeaders.Length);
Array.Copy( byData,0,byMmc,byHeaderLen.Length +byDataLen.Length +byHeaders.Length,byData.Length);

return byMmc;
}

private byte[]  encodeUintvar(int n)
{
byte[] buf = new byte[8];
int l=0;
while(n >=128)
{
byte b = (byte)(n &0x7F);
n = n >> 7;
buf[l++] =b;
}
buf[l++]= (byte)n;

byte[] retBys = new byte[l];
for(int i=0;i<l;++i)
{
retBys[i] = (byte)(buf[l-i-1]|0x80);
}
retBys[l-1] &= 0x7F;
return retBys;

}
// 从文件中读取字节
private byte[] ReadFromFile(string FileName)
{
byte[] bf = new byte[0];
FileStream fs = null;
try
{
fs= new FileStream(FileName,FileMode.Open,FileAccess.ReadWrite,FileShare.None);  // 没有设定Buffsize
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
if( fs!=null)
{
bf = new byte[fs.Length];
fs.Read(bf,0,(int)fs.Length);
fs.Close();
}
return bf;
}
// 得到文件名(不包含文件夹部分)
private string getContentId(string FileName)
{
int at =FileName.LastIndexOf("\\");
if( at <0)
return FileName;
else
return FileName.Substring(at+1);
}
// 增加字节
private byte[] AppendOct(byte[] bys,byte[] byDest)
{
byte[] bysNew =new byte[byDest.Length +bys.Length];
try
{
Array.Copy(byDest,bysNew,byDest.Length);
Array.Copy(bys,0,bysNew,byDest.Length,bys.Length);
}
catch(Exception e)
{
Console.WriteLine(e);
}
return bysNew;
}
// 增加字符串
private byte[] AppendOct(string sz,byte[] byDest)
{
return AppendOct(Encoding.Default.GetBytes(sz),byDest);
}
}

你可能感兴趣的:(编程)