最近在电脑城上买了一根NOKIA3210的数据线,玩了几天改LOGO、改铃声后也将数据线扔在一边。直到前几天在Http://oxyge oftware.com上看到有发手机短信息的二次开发控件,才想起多日不用的数据线,而且最近在学C#,觉得用C#做个发短信息的程序也不错,经过多天的测试,终于实现用电脑+数据线+手机的模式,实现在单位的局域网平台上发送短信息了。
由于在单位使用到发手机短信息的地方有很多,可能是从网页、可能是OUTLOOK中的窗体、也可能是某台非Windows操作系统的主机的某个系统,所以经过思考探讨,觉得最好的解决方案是采用Windows的“服务”,定时从一个目录中固定格式的文本文件中读取出相应的信息,发送出去。而其它客户端只需往该目录写入文本信息即可。思路定下来后就让我们开始吧!
先交待一下开发平台:Windows 2000 Advance Server操作系统、Visual Studio .Net 、Oxygen Sms ActiveX Control V2.3 (Share Ware)、 Nokia 3210手机通过数据线接在COM1上。运行Visual Studio .Net,新建一个C#的项目,选择“Windows Server”类型的项目,命名为“Sm erver”。在Server1的设计画面,将“ServerName”命名为“Sm erver”。点击“视图设计器按钮”切换到设计画面,在“Windows Forms”工具箱中拖一时钟控件,命名为“SmsTimer”,在“Components”工具箱中拖一“EventLog”控件。命名为“eventLog1”。在“项目”菜单中点击“添加引用”,选择“COM”页,浏览到安装Oxygen Sms ActiveX Control V2.3程序的目录,找到SMSControl.ocx添加到“选定的组件”中。 < an cla ='Bgf809'>
将Server1.cs代码替换为
using System;
using System.Collectio
using System.ComponentModel;
using System.Data;
using System.Diagnostic
using System.ServiceProce
using System.IO;
using System.Text ;
name ace Sm erver
{
public cla Sm erver : System.ServiceProce .ServiceBase
{
private System.Timers.Timer SmsTimer;
private System.Diagnostics.EventLog eventLog1;
public O2SMSXControl.O2SMSX SmsX1;//定义手机短信对象
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Sm erver()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the proce
static void Main()
{
System.ServiceProce .ServiceBase[] ServicesToRu
// More than one user Service may run within the same proce . To add
// another service to this proce , change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProce .ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProce .ServiceBase[] { new Sm erver() };
System.ServiceProce .ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer su ort - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SmsTimer = new System.Timers.Timer();
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISu ortInitialize)(this.SmsTimer)).BeginInit();
((System.ComponentModel.ISu ortInitialize)(this.eventLog1)).BeginInit();
//
// SmsTimer
//
this.SmsTimer.Enabled = true;
this.SmsTimer.Ela ed += new System.Timers.Ela edEventHandler(this.SmsTimer_Ela ed);
//
// Sm erver
//
this.ServiceName = "Sm erver";
((System.ComponentModel.ISu ortInitialize)(this.SmsTimer)).EndInit();
((System.ComponentModel.ISu ortInitialize)(this.eventLog1)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Di ose( bool di osing )
{
if( di osing )
{
if (components != null)
{
components.Di ose();
}
}
base.Di ose( di osing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void O tart(string[] args)
{
// TODO: Add code here to start your service.
//开始服务时初始化手机.
SmsX1 = new O2SMSXControl.O2SMSXCla ();
SmsX1.Co ectionMode = 0; //联线类型cable
SmsX1.ComNumber = 1; //联接端口为com 1
SmsX1.Model = 0; //手机类型3210
SmsX1.Open (); //联接手机
SmsX1.SetSMSCNumber ("+8613800754500");//信息中心号码
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void O top()
{
// TODO: Add code here to perform any tear-down nece ary to stop your service.
SmsX1.Close ();
}
private void SmsTimer_Ela ed(object sender, System.Timers.Ela edEventArgs e)
{
//当f:smsdatafiletosend有文件时,先关闭时钟,将其发送出去,并删除掉文件再启动时钟
this.SmsTimer.Enabled =false;
//目录对象
DirectoryInfo cd = new System.IO.DirectoryInfo("F:\Sms\Data\FileToSend");
//数据库记录变量
string rsId;
string r honeNum;
string r msText;
string StrSql;
//首先,在当前目录中列举当前的所有SMS文件
foreach(FileInfo FileSend in cd.GetFiles ())
{
try
{
//依次打开每个文件读取文件内容
FileStream fs = new FileStream (cd.FullName + "\" + FileSend.Name ,FileMode.Open,FileAcce .Read );
StreamReader sr;
sr = new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312"));
rsId = FileSend.Name .ToString ();
rsId = rsId.Replace (".sms","");
rsId = rsId.Trim ();
r honeNum = sr.ReadLine ();
r honeNum = r honeNum.Trim ();
if (r honeNum.Length >11)
r honeNum = r honeNum.Su tring (0,10);
r msText = sr.ReadToEnd();
r msText = r msText.Trim ();
if (r msText.Length >50)
r msText.Su tring (0,49);
fs.Close ();
sr.Close ();
//发送短信息
SmsX1.SendUnicodeSMSMe age (r honeNum.ToString (),r msText.ToString (),6,false,"");
//备份并删除文件
FileSend.CopyTo ("F:\Sms\Data\HadBee end\" + FileSend.Name ,true);
FileSend.Delete ();
}
catch(System.Exception E)
{
//出错写LOG文件
eventLog1.WriteEntry (E.Me age.ToString ());
}
}
//重新启动时钟
this.SmsTimer.Enabled =true;
}
}
}
在 Server1.cs切换设计画面,在属性窗口下点击“Add I taller”,系统自动增加ProjectI taller.cs文件,点击serviceI taller1,设置“Server Name”设置为“Sm erver”,点击“serviceProce I taller1”,设置Account为“LocalSystem”。
选择菜单“生成”中的“生成Sm erver”,改正可能有的错误。进行DOS命令行,进行项目目录的indebug目录下,执行“i tallutil Sm erver”,如果找不到i tallutil程序,就先Path一下。这时,在管理工具的“服务”下可以找到“Sm erver”服务了。启动该服务。这里默认源为目录F:SmsDataFileToSend,如果这个目录有.SMS文件,就读取其第一行为发送的手机号码,第二行到文本结束为短信息内容,然后发送短信息,再将文本备份到F:SmsDataHadBee end。
让我们再回头看一下Server1.cs中的代码。首先在命令空间要增加“using System.IO; using System.Text ; ”方便处理文件及文本对象,在命名类时
public cla Sm erver : System.ServiceProce .ServiceBase
{
private System.Timers.Timer SmsTimer;
private System.Diagnostics.EventLog eventLog1;
public O2SMSXControl.O2SMSX SmsX1;//定义手机短信对象
......
引用Oxygen控件中的定义SmsX1对象,然后在启动服务时初始化手机对象
protected override void O tart(string[] args)
{
// TODO: Add code here to start your service.
//开始服务时初始化手机.
SmsX1 = new O2SMSXControl.O2SMSXCla ();
SmsX1.Co ectionMode = 0; //联线类型cable
SmsX1.ComNumber = 1; //联接端口为com 1
SmsX1.Model = 0; //手机类型3210
SmsX1.Open (); //联接手机
SmsX1.SetSMSCNumber ("+8613800754500");//信息中心号码
}
其中要注意的是要初始化信息中心号码,如果不初始化,经常有发不去的情况。然后当时钟触发时要注意先将时钟关掉,再列举当前目录中的.SMS文件,逐一发送出去,再将时钟打开,同时在读文件时,要注意文件的编码 “sr=new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312"));”采用GB2312编码读取才不会读出乱码出来,最后发送信息即可,“SmsX1.SendUnicodeSMSMe age (r honeNum.ToString (),r msText.ToString (),6,false,""); ”其中各个参数的含义可以参照Oxygen的帮助。最后在服务停止时释放短信息对象“SmsX1.Close ();” 如果出错,则写出错服务LOG文件“eventLog1.WriteEntry (E.Me age.ToString ());”这样,在Windows的“事件查看器”就可以看到出错的信息了。


