如今手机已成为大众交流的主要工具。有关手机的程序开发越来越广泛,本节通过典型实例介绍如何利用短信猫发送、接收短信。


1.方案分析

发短信的一种方法是利用短信猫发短信,本例中使用的是生产的串口短信猫。

所谓短信猫,其实是一种工业级GSM MODEM,通过串口与计算机连接,可以通过AT指令控制进行短信收发的设备。国内目前应用较多的短信猫,都是以SIEMENS或WAVECOM模块为核心组装而成的,与普通手机相比更为稳定高效。

短信猫是利用SIM卡发送短信的硬件设备,通过串口或USB接口(根据设备型号而定)与计算机相连。在程序中可以利用短信猫发送或接收短信。本例使用的是的串口短信猫。在购买短信猫时会附带有SDK的短信猫开发包,其中提供了操作短信猫的函数(封装在dllforvc.dll动态库中)。


2.实施过程
下面利用短信猫发送短信,单击【发送短信】按钮就可以完成对指定手机号码的短信发送,当有新的短信发送过来的时候,单击【接收短信】按钮就可以将短信显示出来。


程序实现具体步骤:

(1)新建一个网站,默认主页为Default.aspx。

(2)在页面中添加6个TextBox文本框,分别用于显示短信猫的COM端口、波特率、机器号码、短信猫的授权号码、输入接收短信的手机号码和要发送的短信内容。

(3)在页面中添加2个Button按钮,分别用于发送短信和接收短信。

(4)程序主要代码如下。

首先建立一个类库,将其命名为Baisc,然后在此类库中建立一个名为GMS的公共类,用于获取短信猫附带的动态库dllforvc.dll中的一些函数,之后将此类库编译成Basic.dll文件,在项目中将其引用,具体代码如下。

导入必要的命名空间,代码如下。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
通过调用dllforvc.dll中的一些函数,获取其中的各种方法供Default.aspx页面中调用,具体的代码如下。


namespace Basic
{
    public class GMS
    {
        //初始化gsm modem,并连接gsm modem
//参数说明:
//device:标识通信端口,如果为NULL,系统会自动检测。
//baudrate:标识通讯波特率,如果为NULL,系统自动检测。
//initstring:标识初始化命令,为NULL即可。
//charset:标识通讯字符集,为NULL即可。
//swHandshake:标识是否进行软件握手,为FALSE即可。
//sn:标识短信猫的授权号,需要根据实际情况填写。
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemInitNew",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern bool GSMModemInitNew(
            string device,
            string baudrate,
            string initstring,
            string charset,
            bool swHandshake,
            string sn);
        //获取短信猫新的标识号码
//参数说明:
//device :通讯端口,为null时系统会自动检测。
//baudrate :通讯波特率,为null时系统会自动检测。
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemGetSnInfoNew",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern string GSMModemGetSnInfoNew(string device, string baudrate);
        //获取当前通讯端口
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemGetDevice",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern string GSMModemGetDevice();
        //获取当前通讯波特率
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemGetBaudrate",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern string GSMModemGetBaudrate();
        //断开连接并释放内存空间     
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemRelease",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern void GSMModemRelease();
        //取得错误信息  
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemGetErrorMsg",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern string GSMModemGetErrorMsg();
        //发送短信息
//参数说明:
//serviceCenterAddress:标识短信中心号码,为NULL即可。
//encodeval:标识短信息编码格式,如果为8,表示中文短信编码。
//text:标识短信内容。
//textlen:标识短信内容的长度。
//phonenumber:标识接收短信的电话号码。
//requestStatusReport:标识状态报告。
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemSMSsend",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern bool GSMModemSMSsend(
            string serviceCenterAddress,
            int encodeval,
            string text,
            int textlen,
            string phonenumber,
            bool requestStatusReport);
        //接收短信息返回字符串格式为:手机号码|短信内容||手机号码|短信内容||
        //RD_opt为1接收短信息后不做任何处理,0为接收后删除信息
        [DllImport("dllforvc.dll",
             EntryPoint = "GSMModemSMSReadAll",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern string GSMModemSMSReadAll(int RD_opt);
    }
}
在Default.aspx页中,导入命名空间using System.Text,具体代码如下。
using System.Text;


当页面加载时,通过调用GMS 类中的GSMModemGetSnInfoNew方法、GSMModemGetDevice方法和GSMModemGetBaudrate方法分别用于显示机器号码、COM端口和波特率,具体代码如下。

    protected void Page_Load(object sender, EventArgs e)
    {
        //机器号码
        this.txtJQHM.Text = Basic.GMS.GSMModemGetSnInfoNew(txtCOM.Text, txtBTL.Text);
        this.txtCOM.Text = Basic.GMS.GSMModemGetDevice();  //COM1
        this.txtBTL.Text = Basic.GMS.GSMModemGetBaudrate();  //波特率
    }
当各项信息填写完毕之后,单击【发送短信】按钮,程序首先通过GMS类中的GSMModemInitNew方法连接设备,如果连接成功,程序会通过GMS类中的GSMModemSMSsend方法将输入的短信内容发送到指定的手机号码中,如果连接失败,程序会通过GMS类中的GSMModemGetErrorMsg方法输出错误信息,【发送短信】按钮的 Click事件中的代码如下。


    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtSJHM.Text == "")
        {
            Page.RegisterStartupScript("","<script>alert('手机号码不能为空!')</script>");
            txtSJHM.Focus();
            return;
        }
        if (txtContent.Text == "")
        {
            Page.RegisterStartupScript("", "<script>alert('短信内容不能为空!')</script>");
            txtContent.Focus();
            return;
        }
        //连接设备
        if (Basic.GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text) == false)
        {
            Page.RegisterStartupScript("","<script>alert('设备连接失败!'" + Basic.GMS.GSMModemGetErrorMsg()+"')</script>");
            return;
        }
        // 发送短信
        if (Basic.GMS.GSMModemSMSsend(null, 8, txtContent.Text, Encoding.Default.GetByteCount(txtContent.Text), txtSJHM.Text, false) == true)
            Page.RegisterStartupScript("","<script>alert('短信发送成功!')</script>");
        else
            Page.RegisterStartupScript("","<script>alert('短信发送失败!'" + Basic.GMS.GSMModemGetErrorMsg()+"')</script>");
    }
当单击【接收短信】按钮时,程序通过GSM类中的GSMModemSMSReadAll方法将发送到手机卡中的短信读取出来,【接受短信】按钮的Click事件中的代码如下。


    protected void btnResvice_Click(object sender, EventArgs e)
    {
        //连接设备
        if (Basic.GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text) == false)
        {
            Page.RegisterStartupScript("","<script>alert('连接失败!'" + Basic.GMS.GSMModemGetErrorMsg()+"')</script>");
            return;
        }
        //接收短信
        txtContent.Text = Basic.GMS.GSMModemSMSReadAll(1);
        if (txtContent.Text.Length <= 0)
        {
            Page.RegisterStartupScript("","<script>alert('暂时没有新的信息!')</script>");
            return;
        }
        txtSJHM.Text = txtContent.Text.Substring(0, 13);
        txtContent.Text = txtContent.Text.Substring(13, txtContent.Text.Length - 13);
    }

3.补充说明
因为短信猫是串行通讯设备,必须串行提交短信发送,而且提交后必须等到有回应后才能提交下一条,否则会造成短信猫死机。如果存在多线程同时并发操作短信猫,也会造成短信猫死机。即使是针对同一短信猫的收发,也必须为一前一后串行,而不能通过收发两个并发线程来操作。

posted on 2011-09-15 15:11  freedom831215  阅读(526)  评论(2编辑  收藏  举报