PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc

上传人:仙人指路1688 文档编号:4135820 上传时间:2023-04-07 格式:DOC 页数:16 大小:78.50KB
返回 下载 相关 举报
PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc_第1页
第1页 / 共16页
PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc_第2页
第2页 / 共16页
PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc_第3页
第3页 / 共16页
PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc_第4页
第4页 / 共16页
PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc》由会员分享,可在线阅读,更多相关《PC机串口控制GSM模块收发短信系统设计(流程图+源程序)论文.doc(16页珍藏版)》请在三一办公上搜索。

1、PC机串口控制GSM模块收发短信系统设计(流程图+源程序)-论文PC机串口控制GSM模块收发短信系统设计(流程图+源程序)Abstract:The SMS(Short Message Service) which has more and more been much accounted of the system developers and system operators is the value that a basic business of the GSM network. We can develop various applications that foreground is

2、 considerable using the GSM network as the network to transmit datas wireless . This text just discusses the programmer of that PCs serial port controls the GSM module to realize Chinese or English short messages sending and receiving, which can realize measuer datas sending or receiving, memory and

3、 dealing on the personal computer through the GSM network. And bat around the message format of PDU mode and programmer of Chinese messages coding and decoding.Key words: Short Message Service;PCs serial port;GSM module;AT commands;PDU coding and decoding-全局变量声明-Dim portno As Integer 端口号Dim baudrate

4、 As Long 波特率Const prex = 0891 定义全局静态变量Const midx = 0011000D91Const sufx = 000801Private Const EM_LINESCROLL = &HB6-发送信息Private Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long-Sleep函数(延时功能)Private Declare

5、 Sub Sleep Lib kernel32 (ByVal dwMilliseconds As Long)GetWindowTextW 指明这是一个 Unicode API (ASSIIunicode)Private Declare Function GetWindowText Lib user32 Alias GetWindowTextW (ByVal hwnd As Long, ByVal lpString As Long, ByVal cch As Long) As LongText2.Text = StrConv(StrConv(Text1.Text, vbUnicode), vbF

6、romUnicode)Private Sub NewSMSeting() 检测新信息If Mobcomm.PortOpen = False Then Mobcomm.PortOpen = TrueEnd If Mobcomm.Output = AT+CNMI=1,1,0,0,1 + vbCr Timer2.Enabled = TrueEnd SubPrivate Function telc(num As String) As String 把电话号码转换成Unicode码Dim tl As IntegerDim ltem, rtem, ttem As StringDim ti As Integ

7、er ttem = tl = Len(num)If tl 11 And tl 13 Then MsgBox 电话号码有误! & tl Exit FunctionEnd IfIf tl = 11 Then tl = tl + 2 num = 86 & numEnd IfFor ti = 1 To tl Step 2 ltem = Mid(num, ti, 1) rtem = Mid(num, ti + 1, 1) As IntegerDim r As StringFor i = 1 To Len(s) Step 4r = r + ChrB(&H & Mid(s, i + 2, 2) & ChrB

8、(&H & Mid(s, i, 2)NextUnicode2AscII = rEnd FunctionPublic Function AscII2Unicode(ByVal CS As String) 把信息转换成Unicode码Dim Length As IntegerDim temp As StringDim i As IntegerDim tempO As StringDim tempN As StringtempN = tempO = CSLength = Len(tempO)For i = 1 To LengthIf AscW(Mid(CS, i, 1) = 0 And AscW(M

9、id(CS, i, 1) 0 Then Backstring = Mobcomm.Input Mobcomm.InBufferCount = 0 Mobcomm.OutBufferCount = 0 1116PC机串口控制GSM模块收发短信系统设计(流程图+源程序)If InStr(Backstring, OK) Then Mobcomm.Output = AT+CMGS= + Str(ML) + Chr(13) + Chr(10) Else GoTo err Exit Sub End If Backstring = End IfSleep (400)If Mobcomm.InBufferCo

10、unt 0 Then Backstring = Mobcomm.Input Mobcomm.InBufferCount = 0 Mobcomm.OutBufferCount = 0 If InStr(Backstring, ) Then Mobcomm.Output = MS + Chr(26) Else GoTo err Exit Sub End If Backstring = End IfSleep (2000)If Mobcomm.InBufferCount 0 Then Backstring = Mobcomm.Input Mobcomm.InBufferCount = 0 Mobco

11、mm.OutBufferCount = 0 If InStr(Backstring, OK) Then MsgBox 信息发送成功!, vbOKOnly, 系统提示 Else GoTo err Exit Function End If Backstring = End IfSleep (1000)Exit Suberr:MsgBox 信息发送成功!, vbOKOnly, 系统提示MsgBox 传送出现错误,请重新发送!, vbOKOnly, 系统提示End SubPrivate Function Sendsms(num As String, msg As String) As Boolean

12、短信发送 Dim PDU, psmsc, pnum, pmsg, buf As String Dim Leng As String Dim Length As Integer Length = Len(msg) Length = 2 * Length Leng = Hex(Length) If Length 0 Then Mobcomm.InputLen = 0 buf = Mobcomm.Input If InStr(buf, ) Then Mobcomm.Output = PDU & Chr$(26) buf = End If End If If Mobcomm.InBufferCount

13、 0 Then Mobcomm.InputLen = 0 buf = Mobcomm.Input If InStr(buf, OK) Then buf = Sendsms = True End If End IfEnd FunctionPrivate Sub readsms(rnum As String) 接收信息Mobcomm.Output = AT+CMGF=1 + vbCrMobcomm.Output = AT+CMGR= & rnum + vbCrEnd SubPrivate Sub Command1_Click() myActiveX.Active = False myActiveX

14、.ComPort = Combo1(1).Text myActiveX.baudrate = Int(Val(Combo2(1).Text) MsgBox 设置端口号为: & Combo1(1).Text & ,波特率为: & Combo2(1).Text & tl myActiveX.Active = TrueEnd SubPrivate Sub Command2_Click()msg.Text = End SubPrivate Sub Command3_Click() DebugText = SIM卡信息 = DebugText SIM卡IMSI: + myActiveX.IMSI Deb

15、ugText 短信中心: + myActiveX.SCA DebugText 所属网络: + myActiveX.Provider DebugText 短信数目: + myActiveX.SMSMemUsed DebugText 短信容量: + myActiveX.SMSMemTotal = 0Mobcomm.InputLen = 0Sleep (100)Mobcomm.Output = AT+CMGF=1 + vbCrSleep (400)PC机串口控制GSM模块收发短信系统设计(流程图+源程序)Mobcomm.InBufferCount = 0Mobcomm.Output = AT+CMG

16、D= + index.Text + Chr(13) + Chr(10)Sleep (400)mobcomm.OutBufferCount = 0mobcomm.Output = 13138529082 + vbCrIf Mobcomm.InBufferCount 0 ThenInstring = Mobcomm.InputIf InStr(Instring, OK) Then MsgBox 成功删除第 + index.Text + 条信息!, vbOKOnly, 提示 Else MsgBox 信息删除不成功!End IfEnd Ifrmsg.Text = Mid(Instring.Text,

17、1, InStr(Instring, Chr(13) + Chr(10)rmsg.Text = Unicode2AscII(stringdeal(Instring)rmsg.Text = Mid(Instring, 11)Sleep (100)mobcomm.OutBufferCount = 0mobcomm.Output = Text1.Text + Chr(26)Sleep (100)End IfIf InStr(Instring, OK) ThenEnd IfIf Mobcomm.PortOpen = True Then Mobcomm.PortOpen = False End Ifmy

18、ActiveX.Active = TrueIf Mobcomm.PortOpen = False Then Mobcomm.PortOpen = TrueEnd If Mobcomm.Output = AT+CMGD= & index.Text + vbCr Sleep (300) If Mobcomm.InBufferCount 0 Then Mobcomm.InputLen = 0 temp$ = Mobcomm.Input Mobcomm.InBufferCount = 0 If InStr(buf, OK) Then MsgBox 该信息已删除! Else MsgBox 信息删除不成功

19、! End If End IfEnd SubPrivate Sub Form_Load()Combo1(1).ListIndex = 0 选择端口和波特率 Combo2(1).ListIndex = 4 myActiveX.Active = True 打开模块 End SubPrivate Sub msgsend_Click()Dim MsgAdr As StringDim TelNo As StringDim Smsg As StringDim PDU As StringDim Leng As StringDim Length As IntegerDim AllLength As Integ

20、erLength = 2 * Len(msg.Text)Leng = Hex(Length)If Len(Leng) = 1 ThenLeng = 0 & LengEnd IfAllLength = 15 + LengthMsgAdr = Trim(telc(8613800758500) 肇庆动感地带短信信中心地址TelNo = Trim(telc(teln.Text) 手机号码Smsg = Trim(AscII2Unicode(msg.Text) 短信息内容PDU = midx & TelNo & sufx & Leng & SmsgPDU = 0011000D91683167006059F

21、100080108828265E55FEB4E50AllLength = 23myActiveX.Active = FalseIf Mobcomm.PortOpen = False Then Mobcomm.PortOpen = True SMSend AllLength, PDU 调用短信息发送程序End IfIf Mobcomm.PortOpen = True Then Mobcomm.PortOpen = False myActiveX.Active = TrueEnd If myActiveX.SendTextSMS Trim(teln.Text), Trim(msg.Text)If

22、Sendsms(Trim(teln.Text), Trim(msg.Text) = True ThenIf Sendsms = True Then MsgBox 信息发送成功!End IfEnd SubPrivate Function stringdeal(ByVal st As String)stringdeal = Mid(st, InStr(st, Chr(10) + 16)End FunctionPrivate Sub rm_Click() 阅读信息Dim N As IntegerDim Instring As String= 0Mobcomm.InputLen = 0Sleep (1

23、00)Instring = Mobcomm.Output = AT+CMGF=0 + vbCrSleep (400)If Mobcomm.InBufferCount 0 Then Instring = Mobcomm.Input Mobcomm.InBufferCount = 0 Mobcomm.OutBufferCount = 0 If InStr(Instring, OK) Then Mobcomm.Output = AT+CMGR= + index.Text + Chr(13) + Chr(10) End If Instring = End IfSleep (500)If Mobcomm

24、.InBufferCount 0 ThenInstring = Mobcomm.InputPC机串口控制GSM模块收发短信系统设计(流程图+源程序)SMSdeal InstringSleep (100) If InStr(Instring, OK) Then MsgBox 成功读取第 + index.Text + 条信息!, vbOKOnly, 系统提示 Else MsgBox 该信息为空!, vbOKOnly, 系统提示 End If Instring = End IfIf Mobcomm.PortOpen = True Then Mobcomm.PortOpen = False myAct

25、iveX.Active = TrueEnd IfEnd SubPrivate Function SMSdeal(SMS As String)On Error Resume NextDim SM As StringDim SN As StringDim STIME As StringDim SMG As StringDim SLONG, N As IntegerIf InStr(SMS, 0891) ThenSM = Mid(SMS, InStr(SMS, 0891)SN = ConTelc(Mid(SM, 25, 14)STIME = ConTimec(Mid(SM, 43, 14)SLONG

26、 = CInt(Mid(SM, 57, 2)N = SLONG * 4SMG = Unicode2AscII(Mid(SM, 59, N) rmsg.Text = 短信类型: + 已读信息 + vbCr + vbLf + 对方号码: + SN + vbCr + vbLf + 发送时间: _ + STIME + vbCr + vbLf + 短信长度: + Str(SLONG / 2) + 字节 + vbCr + vbLf + 信息内容: + SMG + vbCr + vbLfElseIf InStr(SMS, 018) Then SM = Mid(SMS, InStr(SMS, 018) SLO

27、NG = CInt(Mid(SM, 17, 2) N = SLONG * 4 SMG = Unicode2AscII(Mid(SM, 19, N) rmsg.Text = 短信类型: + 存储信息 + vbCr + vbLf + 信息内容: + SMG + vbCr + vbLfEnd IfEnd FunctionPrivate Sub rpb_Click()myActiveX.ReadPhoneBook 1, myActiveX.PhoneBookCountEnd SubPrivate Sub Timer1_Timer()Dim buf As StringIf Mobcomm.InBuffe

28、rCount 0 Then Timer1.Enabled = False Mobcomm.InputLen = 0 buf = Mobcomm.Input If InStr(buf, OK) = 0 Then MsgBox 端口打开有误! & tl End IfEnd IfEnd SubPrivate Sub Timer2_Timer()If Mobcomm.InBufferCount 0 Then MsgBox 你有新的信息,请查收! If IntChk$ = vbOK Then Mobcomm.InputLen = 0 buf$ = Mobcomm.Input Timer2.Enabled

29、 = False remsg$ = StrConv(buf, vbFromUnicode) DebugText remsg 信息显示函数 End IfEnd IfEnd SubPrivate Sub DebugText(Str) 设定输出格式 rmsg.Text = rmsg.Text + Str + Chr(13) + Chr(10) 输出消息 SendMessage rmsg.hwnd, EM_LINESCROLL, 0, 1 向下卷行End SubPrivate Sub myActiveX_OnNewSMS(ByVal ReadState As String, ByVal sDateTi

30、me As String, ByVal sNum As String, ByVal sText As String, ByVal sSCA As String) 收到新消息 DebugText = 收到新消息 = DebugText 时间日期: + sDateTime DebugText 电话号码: + sNum DebugText 消息内容: + sText DebugText = MsgBox 收到新消息: + sNum End SubPrivate Sub myActiveX_OnPhoneBookList(ByVal PhoneBook As String)phbook.Text =

31、PhoneBook 显示电话本内容 End SubPrivate Sub Portopen_Test() 打开端口并测试 If Mobcomm.PortOpen = False Then Mobcomm.CommPort = True End IfMobcomm.Output = AT + vbCrTimer1.Enabled = TrueEnd SubPC机串口控制GSM模块收发短信系统设计(流程图+源程序)摘 要 SMS短信息服务作为GSM网络的一项基本业务已得到越来越多的系统运营商和系统开发商的重视,以GSM网络作为数据无线传输网络,可以开发出多种前景极其乐观的应用。本文只讨论通过PC机

32、串口控制GSM终端模块实现中英文短信息收发的程序设计,以实现测控数据通过GSM网络在个人计算机上的收发、存储和处理,并详细讨论PDU模式的短信息格式和中文短信息软件编解码的设计。 关键词 SMS;PC机串口;GSM模块;AT指令;PDU编码解码1 引言SMS(Short Message Service)短信息服务是GSM(Global System for Mobile Communication)系统中提供的一种GSM终端(手机)之间,通过短信服务中心(Service Center)进行文本信息收发的应用增殖服务,其中服务中心完成信息的存储和转发功能。短信息服务作为GSM网络的一种基本业务,

33、已得到越来越多的系统运营商和系统开发商的重视,基于这种业务的各种应用也蓬勃发展起来。以GSM网络作为数据无线传输网络,可以开发出多种前景极其乐观的应用,如无线数据的双向传送、无线远程检测和控制等。典型的应用有:变电站、电表、水塔、水库或环保监测点等监测数据的无线传输和无线自动警报;远程无线控制高压线路断电器、加热系统、防洪拦阻系统或其它机电系统的启动和关闭;车队交通管理和控制指挥系统;控制和监测香烟、食品和饮料自动售货机的运行状态和存货水平等等。本文选用GSM模块BenQ M22,通过建立PC机的串行口程序,对该模块进行控制,以实现短信息的收发,在PC和应用系统(手机或各种监测系统)之间以GS

34、M网络为纽带,即可实现远距离的数据传输。由于GSM网络在全国范围内实现了联网和漫游,具有网络能力强的特点,用户无需另外组网,在极大提高网络覆盖范围的同时为客户节省了昂贵建网费用和维护费用。同时,它对用户的数量也没有限制,克服了传统的专网通信系统投资成本大,维护费用高,且网络监控的覆盖范围和用户数量有限的缺陷。比起传统的集群系统在无线网络覆盖上具有无法比拟的优势,加上GSM的SMS本身具备的数据传送功能,都使得这些应用得到迅速的普及。利用GSM短信息系统进行无线通信还具有双向数据传输功能,性能稳定,为远程数据传送和监控设备的通信提供了一个强大的支持平台。2 系统总体框图设计与原理概述2.1 系统

35、总体框图整个通信系统分为传输网络和终端两个大部分,传输网络则利用了现有的GSM网络,而终端可以是手机终端或者是具有手机短信功能的模块终端,为了实现对数据的存储和处理,在终端加入了功能强大的个人计算机作为系统的控制中心,计算机与模块终端则通过串口进行通信。系统的总体框图如图1所示:图1 系统总体框图2.2 程序设计语言概述以及系统的界面设计Visual Basic是一种可视化的、面向对象和采用事件驱动方式的结构化高级程序设计语言,可用于开发Windows环境下的种类应用程序。在Visual Basic环境下,利用事件驱动的编程机制、新颖易用的可视化设计工具,使用Visual Basic COM组

36、件和Windows内部的应用程序接口(API)函数等,可以高效、快速地开发出Windows环境下功能强大、图形界面丰富的应用程序软件系统。在Visual Basic环境下设计的系统界面如图2所示: 图2 系统界面2.3 AT指令介绍2.3.1 AT指令概述PC机串口和GSM引擎之间采用AT指令实现相互之间的通信,因此有必要先了解一下AT指令的格式和一些与本设计系统有关的常用指令。PC机串口发出的AT指令用来建立通信链路,AT指令集的命令格式帧都以AT开头,“AT”或者“at”的前缀必须出现在每条命令行的开始。值为键入的设置值,是命令语法要求的一部分。如果该值可选,那么用方括号加以区分。当用AT

37、指令命令设置了一个参数,那么这个值在它被改变之前一直是有效的。2. 3. 2 BenQ M22模块中与SMS有关的GSM AT指令介绍GSM引擎模块提供的命令接口符合GSM07.05和GSM07.07规范。GSM07.07中定义的AT Command接口,提供了一种移动平台与数据终端设备之间的通用接口;GSM07.05对短消息作了详细的规定。在短消息模块收到网络发来的短消息时,能够通过串口发送指示消息,数据终端设备可以向GSM模块发送各种命令。GSM AT指令集,是由诺基亚、爱立信、摩托罗拉和HP等厂家共同为GSM系统研制的,其中包含了对SMS(Short Message Service)的控制。与SMS有关的GSM AT指令如表1所示:表1 与SMS相关的若干GSM AT指令AT指令

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号