Use C # to write text messages in Chinese mobile phones

Source: Internet
Author: User
Tags command line join log connect tostring trim visual studio
The Chinese recently bought a NOKIA3210 data line in the computer city, played several days to change the logo, changed the bell to also throw the data line on one side. 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 there are a lot of places in your unit where you send SMS messages, you might be from a Web page, a form in Outlook, or a system that may be a host of a non-Windows operating system, so after thinking about it, think the best solution is to use 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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.