⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 vc实现串口收发短信
💻 CS
字号:
//////////////////////////////////////////////////////////////////////////
//                  My CSharp demo program                              //
// Use at your own risk                                                 //
// www.hesicong.net                                                     //
// Copyright Dreamworld. You can use this program freely but no in      //
// commerial use without my permission.                                 //
// Authoer: hesicong                                                    //
// Date: 2007-2-25                                                      //
//////////////////////////////////////////////////////////////////////////



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Windows.Forms;
using Dreamworld.Mobile.PhoneControllerSDK;

namespace Code_Demo_for_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Find all plug-in. Plug-ins impliments the Dreamworld.Mobile.PhoneControllerSDK.IPhoneController interface will be found.
            //PluginServices are in PlugInService.vb
            PluginServices.AvailablePlugin[] ap;
            ap = PluginServices.FindPlugins(Path.GetDirectoryName(Application.ExecutablePath), "Dreamworld.Mobile.PhoneControllerSDK.IPhoneController");
            //Connect phone on port and load CommPhone plug-in.
            //This plug-in execute AT+CGMM to get model ID. Then loop through every available plug-in, if Model ID is in the list
            //of SupportedModel, then this plug-in index is returned.
            string port = "COM3";
            int baudrate = 115200;
            Console.WriteLine("Finding your phone");
            int index = SelectPlugin(port, baudrate, ap, "Dreamworld.Mobile.CommPhone");
            //Directcast select plugin to a IPhoneController. Then you can use 'phone' to control your phone
            IPhoneController phone = (IPhoneController)(PluginServices.CreateInstance(ap[index]));
            //Connect phone.
            Console.WriteLine("Connecting phone...");
            phone.Connect(port, baudrate, 10000);
            //Set phone to standard. This will help to execute AT commands accurately.
            phone.SetStandard();
            //Then you can use all functions.
            phone.ServiceCenterNumber = phone.GetServiceCenterNumber(); //If failed, you can fill it manually

            //Read all your messages.
            Console.WriteLine("Reading All your message...");
            SMSRead[] mySMS=phone.ReadSMSByStatus(ATSMSStatus.ALL); //Read all. You can change it
            Console.WriteLine("I have read: " + mySMS.Length + " Messages");
            //Print out first message
            Console.WriteLine("From: "+mySMS[0].DestNumber+"\r\nContent: "+mySMS[0].Content);

            //Read by index
            Console.WriteLine("Read out my first read message...");
            SMSRead mySMSByIndex = phone.ReadSMSByIndex(mySMS[0].Index);
            Console.WriteLine("Content: " + mySMSByIndex.Content);

            //Make a message to send
            SMSSend smsToSend;
            smsToSend.DestinationNumber = "1008611";    //Num to send. For phone, please write like +86xxxxxxxxxxxx
            smsToSend.Content = "HF";   //What you want to send
            smsToSend.RequireStatusReport = false;  //No status report required
            smsToSend.ValidPeriod = SMSSend.TP_VALID_PERIOD.Maximum;    //Maximum valid period
            //Sending a message
            phone.SendSMS(smsToSend);

            Console.WriteLine("OK");
            Console.ReadLine();
            //Del a message
            //Test my yourself!!!!
            //phone.DelSMS(0);
        }

        static int SelectPlugin(string port, int baudrate, PluginServices.AvailablePlugin[] ap, string CommPhoneClassName)
        {
            if (ap == null)
            {
                throw new Exception("Not plugin found");
            }
            string ModelID = string.Empty;
            for (int i = 0; i <= ap.Length - 1; i++)
            {
                if (ap[i].ClassName == CommPhoneClassName)
                {
                    IPhoneController objPlugin;
                    objPlugin = ((IPhoneController)(PluginServices.CreateInstance(ap[i])));
                    objPlugin.Connect(port, baudrate, 1000);
                    objPlugin.SetStandard();
                    ModelID = objPlugin.GetModelID();
                    objPlugin.Disconnect();
                }
            }
            for (int i = 0; i <= ap.Length - 1; i++)
            {
                IPhoneController objPlugin;
                objPlugin = ((IPhoneController)(PluginServices.CreateInstance(ap[i])));
                string[] supportedModel = objPlugin.SupportedModel;
                for (int j = 0; j <= supportedModel.Length - 1; j++)
                {
                    if (ModelID == supportedModel[j])
                    {
                        return i;
                    }
                }
            }
            throw new NotSupportedException("Your phone are not supported");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -