gsm调制解调器接收消息 [英] gsm modem to receive messages

查看:80
本文介绍了gsm调制解调器接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定。这是我写的发送短信的代码。我想添加在同一个调制解调器上接收短信的功能。我无法继续。请有人帮忙。





ok. this is the code i have written to send an sms. i want to add the function to receive a sms on the same modem. i am unable to go on. somebody help please.


'class code
Imports System.IO.Ports
Imports System.Threading
 
Public Class SMSCOMMS
Private WithEvents SMSPort As SerialPort
 
Private SMSThread As Thread
Private ReadThread As Thread
Shared _Continue As Boolean = False
Shared _ContSMS As Boolean = False
Private _Wait As Boolean = False
Shared _ReadPort As Boolean = False
Public Event Sending(ByVal Done As Boolean)
Public Event DataReceived(ByVal Message As String)
 
Public Sub New(ByRef COMMPORT As String)
SMSPort = New SerialPort
With SMSPort
.PortName = COMMPORT
.BaudRate = 9600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.Handshake = Handshake.RequestToSend
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
End With

ReadThread = New Thread(AddressOf ReadPort)
End Sub
 
Public Sub SendSMS(ByVal CellNumber As String, ByVal SMSMessage As String)
Dim MyMessage As String = Nothing
If SMSMessage.Length <= 160 Then
MyMessage = SMSMessage
Else
MyMessage = Mid(SMSMessage, 1, 160)
End If
If IsOpen = True Then
SMSPort.WriteLine("AT")
Thread.Sleep(1000)
SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'setting modem for sms mode
Thread.Sleep(1000)
'SMSPort.WriteLine("AT+CSCA=""+254722500059" & vbCrLf) 'setting the message service center number
SMSPort.WriteLine("AT+CMGS=""" & CellNumber & """" & vbCrLf)
Thread.Sleep(1000)
'SMSPort.WriteLine("AT+CMGS=" & CellNumber & vbCrLf)
_ContSMS = False
SMSPort.WriteLine(MyMessage & vbCrLf & Chr(26))
Thread.Sleep(1000)
_Continue = False
RaiseEvent Sending(False)
'MessageBox.Show("Successfully sent")
End If
End Sub
 
Private Sub ReadPort()
Dim SerialIn As String = Nothing
Dim RXBuffer(SMSPort.ReadBufferSize) As Byte
Dim SMSMessage As String = Nothing
Dim Strpos As Integer = 0
Dim TmpStr As String = Nothing
Try
 
Catch ex As Exception
 

While SMSPort.IsOpen = True
If (SMSPort.BytesToRead <> 0) And (
SMSPort.IsOpen = True) Then
While SMSPort.BytesToRead <> 0
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)
SerialIn =
SerialIn & System.Text.Encoding.ASCII.GetString(
RXBuffer)
If SerialIn.Contains(">") = True Then
_ContSMS = True
End If
If SerialIn.Contains("+CMGS:") = True Then
_Continue = True
RaiseEvent Sending(True)
_Wait = False
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
RaiseEvent DataReceived(SerialIn)
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
End Try
End Sub
 
Public ReadOnly Property IsOpen() As Boolean
Get
If SMSPort.IsOpen = True Then
IsOpen = True
Else
IsOpen = False
End If
End Get
End Property
 
Public Sub Open()
If IsOpen = False Then
SMSPort.Open()
ReadThread.Start()
End If
End Sub
 
Public Sub Close()
If IsOpen = True Then
SMSPort.Close()
End If
End Sub
 
End Class
 
'window form
'Form Code
Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.Ports
Imports mordemsms.SMSCOMMS
 
Public Class Form1
 
Private Sub sendbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendbtn.Click
Dim sms As SMSCOMMS
sms = New SMSCOMMS("COM12")
sms.Open()
sms.SendSMS(TextBox1.Text, TextBox2.Text)
sms.Close()
End Sub
 


End Class

推荐答案

无需花费大量时间查看您的接收程序你的整个想法都有问题。你的阅读不能像你做过一样,一旦有一两个角色进入你可能会通过阅读程序来实现这一点



Without spending huge time looking at your receive routines you have a problem with your whole idea. Your read can't work like you have done it once one or two characters come in you are likely to come thru the read routine you then do this

SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)





所以说+ CG到了你就读了它们然后比较+ CMSG:并且它不匹配,因为其余的角色还没有到达那里。



记住这是一个串行设备,所以你必须看到一条整线的终点,然后才能让它消失。


您可以以编程方式为这些模块设置终结器,但如果您没有设置它,则默认情况下使用CR或CR + LF作为终止线。



您必须将接收数据保存在缓冲区中,直到获得终结符,并且只有在获得终止符后再运行解析。



So lets say "+CG" has arrived you read them then compare to "+CMSG:" and it doesn't match because the rest of the characters haven't got there yet.

REMEMBER THIS IS A SERIAL DEVICE SO YOU HAVE TO LOOK FOR THE END OF A WHOLE LINE AND THEN PARSE IT.

You can programatically set the terminator for these modules but if you haven't set it most use CR or CR+LF as termination of line by default.

You have to hold the receive data in a buffer until you get the terminator and only once you get the terminator then run the parse.


这篇关于gsm调制解调器接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆