window| Chinese
Recently in the computer city to buy a NOKIA3210 data line, played a few days to change the logo, change the bell after the data line will be thrown aside. Until a few days ago on the Http://oxygensoftware.com to see a mobile phone message two development control, just think of the data cable, and recently learning C #, think using C # to do a message of the program is also good, after many days of testing, finally realized with computer + data line + Mobile phone mode, the implementation of the Unit's LAN platform to send short messages.
Because the unit used to send mobile phone messages in a lot of places, may be from the Web page, may be the form in Outlook, but also may be a non-Windows operating system host a system, so after thinking about the best solution is to adopt windows "services", Periodically read the appropriate information from a fixed-format text file in a directory and send it out. Other clients simply write text messages to the directory. Let's start with the idea.
Let's start with the development platform: Windows Advance server operating system, Visual Studio. Net, Oxygen Sms ActiveX control V2.3 (Share Ware), Nokia 3210 Phone Pass The data line is connected to the COM1. Run visual Studio. Net, create a new C # project, select the Windows Server type of project, named "Smsserver". In the Server1 design screen, "ServerName" is named "Smsserver". Click the View Designer button to switch to the design screen, drag the 1: control in the Windows Forms Toolbox, name "Smstimer", and drag a "EventLog" control in the Components Toolbox. Named "EventLog1". Click Add Reference on the Project menu, select COM page, browse to the directory where the Oxygen Sms ActiveX Control V2.3 program is installed, and locate Smscontrol.ocx to add to selected components.
Replace the Server1.cs code with the
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.ServiceProcess;
Using System.IO;
Using System.Text;
Namespace Smsserver
{
public class SmsServer:System.ServiceProcess.ServiceBase
{
Private System.Timers.Timer Smstimer;
Private System.Diagnostics.EventLog EventLog1;
Public O2SMSXCONTROL.O2SMSX smsx1;//defines mobile SMS objects
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Smsserver ()
{
This are required by the windows.forms Component Designer.
InitializeComponent ();
Todo:add any initialization on the initcomponent call
}
The main entry point for the process
static void Main ()
{
System.serviceprocess.servicebase[] ServicesToRun;
More than one user Service may run within the same process. To add
Another service to this process, change the "following line" to
Create a second service object. For example,
//
ServicesToRun = new system.serviceprocess.servicebase[] {new Service1 (), New MySecondUserService ()};
//
ServicesToRun = new system.serviceprocess.servicebase[] {new Smsserver ()};
System.ServiceProcess.ServiceBase.Run (ServicesToRun);
}
<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This. Smstimer = new System.Timers.Timer ();
This.eventlog1 = new System.Diagnostics.EventLog ();
((System.ComponentModel.ISupportInitialize) (this. Smstimer)). BeginInit ();
((System.ComponentModel.ISupportInitialize) (THIS.EVENTLOG1)). BeginInit ();
//
Smstimer
//
This. Smstimer.enabled = true;
This. smstimer.elapsed + = new System.Timers.ElapsedEventHandler (this. smstimer_elapsed);
//
Smsserver
//
This. ServiceName = "Smsserver";
((System.ComponentModel.ISupportInitialize) (this. Smstimer)). EndInit ();
((System.ComponentModel.ISupportInitialize) (THIS.EVENTLOG1)). EndInit ();
}
<summary>
Clean up any being used.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
<summary>
The Set things in motion so your service can does its work.
</summary>
protected override void OnStart (string[) args)
{
Todo:add code here to start your service.
Initializes the phone when the service is started.
SmsX1 = new O2smsxcontrol.o2smsxclass ();
Smsx1.connectionmode = 0; Line type Cable
Smsx1.comnumber = 1; Join port is COM 1
Smsx1.model = 0; Mobile phone Type 3210
Smsx1.open (); Connect mobile phone
Smsx1.setsmscnumber ("+8613800754500");//Information Center number
}
<summary>
Stop this service.
</summary>
protected override void OnStop ()
{
Todo:add code here to perform any tear-down necessary to stop your service.
Smsx1.close ();
}
private void Smstimer_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
When F:\sms\data\filetosend has a file, first close the clock, send it out, and delete the file and then start the clock
This. Smstimer.enabled =false;
directory objects
DirectoryInfo cd = new System.IO.DirectoryInfo ("F:\\sms\\data\\filetosend");
Database record variables
String Rsid;
String Rsphonenum;
String Rssmstext;
String strSQL;
First, enumerate all the current SMS files in the current directory
foreach (FileInfo filesend in CD. GetFiles ())
{
Try
{
Open each file in turn to read the contents of the file
FileStream fs = new FileStream (CD. FullName + "\" + Filesend.name, FileMode.Open,FileAccess.Read);
StreamReader SR;
sr = new StreamReader (fs,unicodeencoding.getencoding ("GB2312"));
Rsid = Filesend.name. ToString ();
Rsid = Rsid.replace (". SMS", "");
Rsid = Rsid.trim ();
Rsphonenum = Sr. ReadLine ();
Rsphonenum = Rsphonenum.trim ();
if (Rsphonenum.length >11)
Rsphonenum = rsphonenum.substring (0,10);
Rssmstext = Sr. ReadToEnd ();
Rssmstext = Rssmstext.trim ();
if (Rssmstext.length >50)
Rssmstext.substring (0,49);
Fs. Close ();
Sr. Close ();
Send SMS
Smsx1.sendunicodesmsmessage (Rsphonenum.tostring (), rssmstext.tostring (), 6,false, "");
Backing up and deleting files
Filesend.copyto ("f:\\sms\\data\\hadbeensend\\" + Filesend.name, True);
Filesend.delete ();
}
catch (System.Exception E)
{
Error writing log file
Eventlog1.writeentry (E.message.tostring ());
}
}
Restart Clock
This. Smstimer.enabled =true;
}
}
}
In the Server1.cs switch design screen, under the Properties window click "Add Installer", the system automatically add ProjectInstaller.cs file, click ServiceInstaller1, set "Server Name" set to " Smsserver ", click" ServiceProcessInstaller1 ", set account as" LocalSystem ".
Select "Generate Smsserver" in the menu "generate" to correct possible errors. For the DOS command line, the project directory of the \Bin\Debug directory, the implementation of the "InstallUtil smsserver", if you can not find the InstallUtil program, the first path. At this point, the "smsserver" service can be found under the "services" of the administrative tools. Start the service. Here the default source is directory F:\Sms\Data\FileToSend, if this directory has. Sms files, read the phone number of the first act sent, the second line to the end of the text message content, and then send SMS, and then back up the text to F:\Sms\Data\HadBeenSend\.
Let's look back at the code in Server1.cs. First in the command space to add "using System.IO;" Using System.Text; "Facilitates processing of file and text objects, when naming classes
public class SmsServer:System.ServiceProcess.ServiceBase
{
Private System.Timers.Timer Smstimer;
Private System.Diagnostics.EventLog EventLog1;
Public O2SMSXCONTROL.O2SMSX smsx1;//defines mobile SMS objects
......
Reference the defined SmsX1 object in the oxygen control, and then initialize the mobile object when the service is started
protected override void OnStart (string[) args)
{
Todo:add code here to start your service.
Initializes the phone when the service is started.
SmsX1 = new O2smsxcontrol.o2smsxclass ();
Smsx1.connectionmode = 0; Line type Cable
Smsx1.comnumber = 1; Join port is COM 1
Smsx1.model = 0; Mobile phone Type 3210
Smsx1.open (); Connect mobile phone
Smsx1.setsmscnumber ("+8613800754500");//Information Center number
}
One of the things to note is to initialize the information center number, if not initialized, often have the situation. Then when the clock is triggered, be careful to turn off the clock first, and then enumerate the current directory. SMS files, send them out, and then turn the clock on, while reading the file, pay attention to the encoding of the file "Sr=new StreamReader (fs,unicodeencoding.getencoding (" GB2312 "));" The use of GB2312 encoded reading will not read out garbled, and finally send information, "Smsx1.sendunicodesmsmessage (Rsphonenum.tostring (), rssmstext.tostring (), 6,false ,""); "The meaning of each of these parameters can refer to the help of oxygen." Finally, the Message object "Smsx1.close () is released when the service is stopped" and if an error occurs, the Write Error service log file "Eventlog1.writeentry (e.message.tostring ());" This way, you can see the error information in the Windows Event Viewer.
But here is a little regret, through OCX control issued by the text message in front of a string of the Web site in English, but the registration version will not have this string of words, registration "just" ¥399 can be: (. But overall still good, if have any question, welcome everybody to discuss together, my mailbox is linmin@wocall.com.