I recently bought a NOKIA3210 data cable on the computer city, playing a few days to change LOGO, and then throw the data line aside after the ringing. Until a few days, I saw the secondary development control with a mobile phone short message on http://oxygensoftware.com, I remembered how many data cables don't have to be used, and I have recently learned C #, I feel that a short message is made with C #. Normal, after a multi-day test, finally realize the mode of using computer data cable mobile phone, and realize the short message on the local area network platform.
Since there are many places where the mobile phone short message is used, it may be from the web page, it may be a form in Outlook, or some system of a host of a non-Windows operating system, so after thinking, I feel the most A good solution is to use Windows "Services", and the corresponding information is read from the fixed format text file in a directory and sent it. Other clients only need to write text messages to this directory. Let us start after thinking down!
Let's explain the development platform first: Windows 2000 Advance Server operating system, Visual Studio .NET, Oxygen SMS ActiveX Control V2.3 (Share Ware), NOKIA 3210 mobile phone is connected to COM1 through the data cable. Run Visual Studio .NET, create a new C # project, select the "Windows Server" type project, named "SMSServer". At the design screen of Server1, "ServerName" is named "SMSServer". Click the "View Designer button" to switch to the design screen, drag the clock control in the "Windows Forms" toolbox, name "SmStimer", drag a "EventLog" control in the "Components" toolbox. Name "EventLog1". Click "Add Reference" in the Project menu, select the "COM" page, browse to the directory where the Oxygen SMS ActiveX Control V2.3 program is installed, and find SMSControl.ocx to add to "Selected Components".
Replace Server1.cs code to
Using system.collections; 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; // define the object SMS
///
Public smsserver () {// this call is required by the windows.forms component design; initializecomponent ();
// Todo: add anyinitization after the initcomponent call} // the main entry point () {system.serviceProcess.serviceBase [] service [] serviceournal
// 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);
///
///
///
Private void smstimer_elapsed (Object sender, system.timers.elapsendeventargs e) {// When F: / SMS / DATA / FileTOnd is file, turn off the clock first, send it out and remove the file and start the clock this.smstimer. Enabled = false;
// Directory object DirectoryInfo CD = new system.io.directoryInfo ("f: // sms // data // filetosend"); // Database record variable string rsid; string rsphonenum; string rssmstext;
String strsql;
// First, listen to all of the current SMS files in the current directory. FORET ()) {Try {// Open each file read file content 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 = r r .Replace (".sms", "); rsid = rsid.trim (); rsphonenum = sr.readline (); rsphonenum = rsphonenenum.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 short information SMSX1.sendunicodesmsMsmessage (RSSMSTEXT.TOSTRING (), 6, FALSE, "");
// Back up and delete file fiesend.copyto ("f: // sms //" filesend.name, true); fiesend.delete ();} catch (system.exception e) {////// Wrong to write log file EventLog1.writeentry (E.MESSAGE.TOSTRING ());}} // Restart the clock this.smstimer.enabled = true;}}}
In the server1.cs to switch the design screen, click "Add Installer" under the Properties window, the system automatically adds the ProjectInstaller.cs file, click ServiceInstaller1, set "Server Name" to "SMSServer", click "ServiceProcessInstaller1", set Accent as "Localsystem ".
Select "Generate SMSServer" in the menu "Generate" to correct possible errors. Perform a DOS command line, perform the INSTALLUTIL SMSSERVER, and if the installutil program is not found, you can't find the installutil program. At this time, "SMSServer" service can be found under "service" of the management tool. Start the service. The default source is directory f: / sms / data / filetosend, if this directory has a .sms file, read the mobile phone number sent by its first behavior, the second line to the text end as a short message content, and then send short information, Then back up the text to F: / SMS / DATA / HADBEENSEND /. Let's look back at the code in Server1.cs. First, in order space to add "useing system.io; use system.text;" for handling files and text objects, when named
public class SmsServer: System.ServiceProcess.ServiceBase {private System.Timers.Timer SmsTimer; private System.Diagnostics.EventLog eventLog1; public O2SMSXControl.O2SMSX SmsX1; // define the object SMS ...... Oxygen control in reference to the definition of SMSX1 object, then initialize the phone object Protected Override void onstart (String [] args) {// Todo: add code here to start your service. SMSX1 = New o2smsxControl.O2SMSXClass () SMSX1.ConnectionMode = 0; // Online type Cable smsx1.comNumber = 1; // The coupling port is COM 1 SMSX1.MODEL = 0; // Mobile phone type 3210 SMSX1.Open (); // Joint mobile phone SMSX1.SetsMscnumber (" 8613800754500"); // Information center number}
It should be noted that the information center number is to initialize, if not initialized, often there is no way. Then when the clock is triggered, pay attention to turn it off first, listen to the .sms file in the current directory, send out one by one, open the clock, and pay attention to the encoding of the file "SR = New StreamReader (fs, UnicodeEncoding.Getencoding ("GB2312")); "Use GB2312 encoding reading not to read garbled," SMSX1.SndunicodesMsmessage (RSX1.Tostring (), rsSMSText.toString (), 6, false , ""); "The meaning of each parameter can refer to Oxygen's help. Finally, the short message object "SMSX1.Close ();" If an error, write an error service log file "EventLog1.WriteEntry (E.MESSAGE.TOSTRING ());" This, in Windows "event viewer "You can see the error message.