基本信息
源码名称:C#SMS短信收发客户端源码(串口)
源码大小:0.15M
文件格式:.zip
开发语言:C#
更新时间:2019-08-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
C# 串口 AT指令实现短信发送与读取等功能 实现了最全的短信通信功能 界面完美 直接可能操作at 指令

C# 串口 AT指令实现短信发送与读取等功能


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace SerialPortTest
{
    public partial class MainForm : Form
    {
        public ComPortClass sp = null; 
    
        public MainForm()
        {
            InitializeComponent();
        }
        private void ReceiveMessage(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                Thread.Sleep(1000);
                string str = sp.ReadCom();
                Thread.Sleep(2000);
                str = str.Replace("\r\n", "").Replace(" ", "");
                if (str.Length > 6)
                {
                    if (str.Substring(0, 5) == " CMTI")
                    {
                        //MessageBox.Show("收到一条短信!");
                        int index = str.IndexOf(",");
                        string location = str.Substring(index   1);
                        value = sp.ReadMsg(location);
                        MessageBox.Show(value, "有新短信", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnConnect_Click(object sender, EventArgs e)
        {
            sp = new  ComPortClass(cmbPortName.Text.Trim(), Convert.ToInt32(cmbPortNum.Text));
            cmbPortName.Enabled = false;
            cmbPortNum.Enabled = false;
            btnConnect.Enabled = false;
            btnClose.Enabled = true;
            if (sp.sp.IsOpen)
            {
                this.rtbDisplay.Text = sp.SetMsgCenter(txtCenterNO.Text.Trim());
            }
            else
            {
                rtbDisplay.Text = " 串口打开失败!";
            }
            sp.sp.DataReceived  = new SerialDataReceivedEventHandler(ReceiveMessage);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            sp.CloseCom();
            cmbPortName.Enabled = true;
            cmbPortNum.Enabled = true;
            btnConnect.Enabled = true;
            btnClose.Enabled = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            btnClose.Enabled = false;
        }

        private void btnATCmd_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text =  sp.ATCommand(txtCmd.Text.Trim());
        }

        private void btnCenterNO_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text = sp.ATCommand("AT CSCA?");
        }

        private void btnSIM_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text = sp.ATCommand("AT CCID");
        }

        private void btnSetCenterNo_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text = sp.ATCommand("AT CSCA="   txtCenterNO.Text.Trim()   ";&W");         
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text = sp.ATCommand("AT CSQ");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text = sp.SendToCom(txtMobile.Text.Trim(),txtCenterNO.Text.Trim(),txtContent.Text.Trim());
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            txtMsgContent.Text = sp.ReadMsg(txtMsgID.Text.Trim());
        }

        private void btnDelMsg_Click(object sender, EventArgs e)
        {
            rtbDisplay.Text = "";
            rtbDisplay.Text = sp.ATCommand("AT CMGD=" txtDelMsg.Text.Trim());
        }
    }
}