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

📄 form1.cs

📁 用c#写的GSM短信示例程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CSSMS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            int com = int.Parse(this.comboBox1.Text.Replace("COM", ""));
            uint baud = uint.Parse(this.comboBox2.Text);
            int suc = 0;
            if (this.textBox_csca.Text == "")
            {
                suc=SMS.SMSStartService(com, baud, 2, 8, 0, 0, "card");
            }
            else
            {
                 suc=SMS.SMSStartService(com, baud, 2, 8, 0, 0, this.textBox_csca.Text);
            }

            if (suc != 0)
            {
                this.richTextBox_sd.AppendText("启动成功\r\n");
                this.button_start.Enabled = false;
                this.button_send.Enabled = true;
                this.button_stop.Enabled = true;
                this.timer1.Start();
            }
            else
            {
                this.button_start.Enabled = true;
                this.button_send.Enabled = false;
                this.button_stop.Enabled = false;

                byte[] err = new byte[1024];
                SMS.SMSGetLastError(err);
                this.richTextBox_sd.AppendText(string.Format("启动失败:{0}",System.Text.Encoding.Default.GetString(err)));
                this.richTextBox_sd.AppendText("\r\n");
                this.timer1.Stop();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.Text = "COM1";
            this.comboBox2.Text = "57600";
            this.button_start.Enabled = true;
            this.button_send.Enabled = false;
            this.button_stop.Enabled = false;
            this.Text = "Saro C# SMS Demo V3.9";
        }

        private void button_stop_Click(object sender, EventArgs e)
        {
            SMS.SMSStopSerice();
            this.button_start.Enabled = true;
            this.button_send.Enabled = false;
            this.button_stop.Enabled = false;
            this.richTextBox_sd.AppendText("停止成功\r\n");
            this.timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SMS.SMSReportStruct rept = new SMS.SMSReportStruct();
            SMS.SMSMessageStruct smg = new SMS.SMSMessageStruct();
            if (SMS.SMSReport(ref rept) != 0)
            {
               
                if (rept.Success != 0)
                {
                    this.richTextBox_sd.AppendText("发往" + System.Text.Encoding.Default.GetString(rept.PhoneNo));
                    this.richTextBox_sd.AppendText("的短信发送成功,内容:" + System.Text.Encoding.Default.GetString(rept.Msg));
                }
                else
                {
                    this.richTextBox_sd.AppendText("发往" + System.Text.Encoding.Default.GetString(rept.PhoneNo));
                    this.richTextBox_sd.AppendText("的短信发送失败,内容:" + System.Text.Encoding.Default.GetString(rept.Msg));
                }
                this.richTextBox_sd.AppendText("\r\n");
            }
            if (SMS.SMSGetNextMessage(ref smg) != 0)
            {

                this.richTextBox_rec.AppendText("收到短信:<" + System.Text.Encoding.Default.GetString(smg.ReceTime));
                this.richTextBox_rec.AppendText("> <"+System.Text.Encoding.Default.GetString(smg.PhoneNo));
                this.richTextBox_rec.AppendText(">:\r\n  "+System.Text.Encoding.Default.GetString(smg.Msg));
                this.richTextBox_rec.AppendText("\r\n");
            }
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            this.richTextBox_sd.Clear();
            this.richTextBox_rec.Clear();

        }

        private void button_send_Click(object sender, EventArgs e)
        {
            if (this.richTextBox_send.Text == "" || this.textBox_phone.Text == "")
            {
                MessageBox.Show("内容和号码不能为空");
                return;
            }
            SMS.SMSSendMessage(this.richTextBox_send.Text, this.textBox_phone.Text);
        }
    }
}

⌨️ 快捷键说明

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