C#短信猫连接发送接收等代码.docx_第1页
C#短信猫连接发送接收等代码.docx_第2页
C#短信猫连接发送接收等代码.docx_第3页
C#短信猫连接发送接收等代码.docx_第4页
C#短信猫连接发送接收等代码.docx_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

using System;using System.IO.Ports;using System.Collections.Generic;using System.ComponentModel;using System.Threading;using System.Drawing;using System.Data;using System.Data.OleDb;using System.Text;using System.Windows.Forms;namespace ylModem.Modem public partial class ModemLink : UserControl public ModemLink() InitializeComponent(); txtCenterNO.Text = System.Configuration.ConfigurationManager.AppSettingsCenterNo; public static int TotalNum = Int32.Parse(System.Configuration.ConfigurationManager.AppSettingsTotalNum); public static int UsedNum = Int32.Parse(System.Configuration.ConfigurationManager.AppSettingsUsedNum); public static int LastDay = Int32.Parse(System.Configuration.ConfigurationManager.AppSettingsLastDay); public static int LastHour = Int32.Parse(System.Configuration.ConfigurationManager.AppSettingsLastHour); public static string centerNO = ; public static ComPortClass.ComPortClass sp = null; public static bool loginStatus = false; private void btnConnect_Click(object sender, EventArgs e) sp = new ComPortClass.ComPortClass(cmbPortName.Text.Trim(), Convert.ToInt32(cmbPortNum.Text); cmbPortName.Enabled = false; cmbPortNum.Enabled = false; btnConnect.Enabled = false; btnClose.Enabled = true; if (sp.sp.IsOpen) loginStatus = true; this.rtbDisplay.Text = sp.SetMsgCenter(txtCenterNO.Text.Trim(); centerNO = txtCenterNO.Text.Trim(); sp.sp.DataReceived += new SerialDataReceivedEventHandler(ReceiveMessage); else rtbDisplay.Text = 串口打开失败!; private void btnClose_Click(object sender, EventArgs e) rtbDisplay.Text = ; sp.CloseCom(); loginStatus = false; cmbPortName.Enabled = true; cmbPortNum.Enabled = true; btnConnect.Enabled = true; btnClose.Enabled = false; private void btnCenterNO_Click(object sender, EventArgs e) rtbDisplay.Text = ; if (loginStatus) rtbDisplay.Text = sp.ATCommand(AT+CSCA?); else rtbDisplay.Text = 请先连接短信猫!; private void btnSIM_Click(object sender, EventArgs e) rtbDisplay.Text = ; if (loginStatus) rtbDisplay.Text = sp.ATCommand(AT+CCID); else rtbDisplay.Text = 请先连接短信猫!; private void btnSetCenterNo_Click(object sender, EventArgs e) rtbDisplay.Text = ; if (loginStatus) AppConfig config = new AppConfig(); config.ConfigType = (int)ConfigFileType.AppConfig; config.SetValue(CenterNo, txtCenterNO.Text.Trim(); centerNO = txtCenterNO.Text.Trim(); rtbDisplay.Text = sp.ATCommand(AT+CSCA= + txtCenterNO.Text.Trim() + ;&W); else rtbDisplay.Text = 请先连接短信猫!; private void btnTest_Click(object sender, EventArgs e) rtbDisplay.Text = ; if (loginStatus) rtbDisplay.Text = sp.ATCommand(AT+CSQ); else rtbDisplay.Text = 请先连接短信猫!; private void btnPhoneNum_Click(object sender, EventArgs e) rtbDisplay.Text = ; if (loginStatus) rtbDisplay.Text = sp.ATCommand(AT+CNUM); else rtbDisplay.Text = 请先连接短信猫!; private void btnClear_Click(object sender, EventArgs e) rtbDisplay.Text = ; 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) int index = str.IndexOf(,); string location = str.Substring(index + 1); string value = sp.ReadMsg(location); /MessageBox.Show(value, 有新短信, MessageBoxButtons.OK, MessageBoxIcon.Information); if (value.Substring(0, 3) = 内容:) InsertReceive(value); sp.ATCommand(AT+CMGD= + location); catch (Exception ex) MessageBox.Show(ex.Message); private void InsertReceive(string pdu) String sendmsg = pdu.Substring(pdu.IndexOf(内容:) + 3, pdu.IndexOf(-发送者:) - pdu.IndexOf(内容:) - 3); sendmsg = sendmsg.Replace(/0, ); String sendmobile = pdu.Substring(pdu.IndexOf(-发送者:) + 7, pdu.IndexOf(-接收时间:) - pdu.IndexOf(-发送者:) - 7); String receivetime = pdu.Substring(pdu.IndexOf(-接收时间:) + 8); OleDbConnection myConnection = new OleDbConnection(ylModem.main.DBConfig); myConnection.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand(select phrase.receive from phrase,phrasegroup where phrasegroup.id=phrase.groupid and phrasegroup.status=1 and phrase.phrase= + sendmsg.Trim() + , myConnection); DataSet ds = new DataSet(); adapter.Fill(ds, receive); if (ds.Tablesreceive.Rows.Count = 0) OleDbCommand myCommand = myConnection.CreateCommand(); OleDbTransaction myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted); myCommand.Transaction = myTrans; myCommand.CommandText = INSERT INTO receivemsg (sendmobile,sendmsg,receivetime,receivestatus) values ( + sendmobile + , + sendmsg + , + receivetime + ,无回复); myCommand.ExecuteNonQuery(); myTrans.Commit(); myCommand.Dispose(); myTrans.Dispose(); else String receiveStatus = InsertSend(sendmobile, (String)ds.Tablesreceive.Rows0receive); OleDbCommand myCommand = myConnection.CreateCommand(); OleDbTransaction myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted); myCommand.Transaction = myTrans; myCommand.CommandText = INSERT INTO receivemsg (sendmobile,sendmsg,receivetime,receivestatus) values ( + sendmobile + , + sendmsg + , + receivetime + , + receiveStatus + ); myCommand.ExecuteNonQuery(); myTrans.Commit(); myCommand.Dispose(); myTrans.Dispose(); ds.Dispose(); adapter.Dispose(); myConnection.Close(); private string InsertSend(string sendMobile, string sendMsg) if (LastDay != System.DateTime.Now.Day | LastHour != System.DateTime.Now.Hour) UsedNum = 0; LastDay = System.DateTime.Now.Day; LastHour = System.DateTime.Now.Hour; if (UsedNum TotalNum) string sendresult = sp.SendToCom(sendMobile.Trim(), centerNO.Trim(), sendMsg.Trim(); if (sendresult.Contains(OK) UsedNum+; OleDbConnection myConnection = new OleDbConnection(ylModem.main.DBConfig); myConnection.Open(); OleDbCommand myCommand = myConnection.CreateCommand(); OleDbTransaction myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted); myCommand.Transaction = myTrans; myCommand.CommandText = INSERT INTO sendreport (sendmobile,sendmsg,sendstatus) values ( + sendMobile + , + sendMsg + ,发送成功); myCommand.ExecuteNonQuery(); myTrans.Commit(); myCommand.Dispose(); myTrans.Dispose(); myConnection.Close(); return 回复成功; else OleDbConnection myConnection = new OleDbConnection(ylModem.main.DBConfig); myConnection.Open(); OleDbCommand myCommand = myConnection.CreateCommand(); OleDbTransaction myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted); myCommand.Transaction = myTrans; myCommand.CommandText = INSERT INTO sendreport (sendmobile,sendmsg,sendstatus) values ( + sendMobile + , + sendMsg + ,发送失败); myCommand.ExecuteNonQuery(); myTrans.Commit(); myCommand.Dispose(); myTrans.Dispose(); myConnection.Close(); return 回复失败; else return 未回复; 本文来自CSDN博客,转载请标明出处:/long102/archive/2007/10/31/1859309.aspx如今手机已成为大众的交流工具。有关手机的程序开发越来越广泛,本节通过几个典型实例介绍如何利用短信猫发送、接收短信、远程控制计算机、业务员销售数据采集和短信息娱乐互动平台。实例431利用短信猫收发短信息实例说明短信猫是利用SIM卡发送短信的硬件设备,通过串口或USB接口(根据设备型号而定)与计算机相连。在程序中可以利用短信猫发送或接收短信。本例实现了利用短信猫收发短信息的功能。实例运行结果如图13.15所示。技术要点本例使用的是北京人大金仓信息技术有限公司的串口短信猫。在购买短信猫时会附带包括SDK的开发包,其中提供了操作短信猫的函数(封装在dllforvc.dll动态库中)。下面介绍操作短信猫的主要函数。(1)GSMModemGetSnInfoNew函数该函数获取短信猫注册需要的信息,代码如下: DllImport(dllforvc.dll, EntryPoint = GSMModemGetSnInfoNew, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall) public static extern string GSMModemGetSnInfoNew(string device, string baudrate);参数说明如下。l device:通信端口,为null时系统会自动检测。l baudrate:通讯波特率,为null时系统会自动检测。(2)GSMModemGetDevice函数该函数获取当前的通讯端口,代码如下: DllImport(dllforvc.dll, EntryPoint = GSMModemGetDevice, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall) public static extern string GSMModemGetDevice();(3)GSMModemGetBaudrate函数该函数获取当前的通讯波特率,代码如下: DllImport(dllforvc.dll, EntryPoint = GSMModemGetBaudrate, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall) public static extern string GSMModemGetBaudrate();(4)GSMModemInitNew函数该函数用于初始化短信猫。语法如下: 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);参数说明如下。l device:标识通信端口,如果为NULL,系统会自动检测。l baudrate:标识通讯波特率,如果为NULL,系统会自动检测。l initstring:标识初始化命令,为NULL即可。l charset:标识通讯字符集,为NULL即可。l swHandshake:标识是否进行软件握手,为False即可。l sn:标识短信猫的授权号,需要根据实际情况填写。(5)GSMModemSMSsend函数该函数用于发送手机短信。语法如下: 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);参数说明如下。l serviceCenterAddress:标识短信中心号码,为NULL即可。l encodeval:标识短信息编码格式,如果为8,表示中文短信编码。l text:标识短信内容。l textlen:标识短信内容的长度。l phonenumber:标识接收短信的电话号码。l requestStatusReport:标识状态报告。(6)GSMModemSMSReadAll函数该函数取得所有短信息,包括SIM卡和手机中的短信息。返回的短信内容格式为电话号码1|短信内容1|电话号码2|短信内容2|: /接收短信息返回字符串格式为:手机号码|短信内容|手机号码|短信内容| /RD_opt为1表示接收短信息后不做任何处理,为0表示接收后删除信息 DllImport(dllforvc.dll, EntryPoint = GSMModemSMSReadAll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall) public static extern string GSMModemSMSReadAll(int RD_opt);参数说明如下。l RD_opt:对读取后的短信息进行处理,0表示删除,1表示不做处理。实现过程(1)新建一个项目,命名为Ex13_14,默认窗体为Form1。(2)在Form1窗体中,主要添加TextBox控件和Label控件,控件的数量及用途如图13.15所示,添加两个Button控件,分别用于发送短信息和接收短信息。(3)主要程序代码。将所使用的函数封装在GMS类中。代码如下:class GMS /初始化gsm modem,并连接gsm modem 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); /获取短信猫新的标识号码 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(); /发送短信息 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为接收短信息后不做任何处理,为接收后删除信息 DllImport(dllforvc.dll, EntryPoint = GSMModemSMSReadAll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall) public static extern string GSMModemSMSReadAll(int RD_opt);在装载Form1窗体时,获取设备信息。代码如下: private void Form1_Load(object sender, EventArgs e) /机器号码,当参数为空时,函数自动获取设备信息 txtJQHM.Text = GMS.GSMModemGetSnInfoNew(txtCOM.Text, txtBTL.Text); txtCOM.Text = GMS.GSMModemGetDevice();/ txtBTL.Text= GMS.GSMModemGetBaudrate();/波特率 发送短信息。代码如下: private void btnSend_Click(object sender, EventArgs e) if(txtSJHM.Text = ) MessageBox.Show(手机号码不能为空!,提示, MessageBoxButtons.OK); txtSJHM.Focus(); return; if(txtContent.Text=) MessageBox.Show(短信内容不能为空!, 提示, MessageBoxButtons.OK); txtContent.Focus(); return; /连接设备 if(GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text)=false) MessageBox.Show(设备连接失败! + GMS.GSMModemGetErrorMsg(),提示, MessageBoxButtons.OK); return; / 发送短信 if (GMS.GSMModemSMSsend(null, 8, txtContent.Text, Encoding.Default.GetByteCount(txtContent.Text),txtSJHM.Text, false) = true) MessageBox.Show(短信发送成功!, 提示, MessageBoxButtons.OK); else MessageBox.Show(短信发送失败! + GMS.GSMModemGetErrorMsg(), 提示, MessageBoxButtons.OK); 接收短信息。代码如下: private void btnResvice_Click(object sender, EventArgs e) /1)连接设备 if (GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text) = false) MessageBox.Show(连接失败! + GMS.GSMModemGetErrorMsg(), 提示, MessageBoxButtons.OK); return; /2)接收短信 txtContent.Text = GMS.GSMModemSMSReadAll(1); txtSJHM.Text = txtContent.Text.Substring(0, 13); txtContent.Text = txtCont

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论