vb.net 从 GSM 调制解调器发送和接收短信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23179052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:25:27  来源:igfitidea点击:

Sending And Receiving SMS From GSM modem

vb.netserial-portgsmmodem

提问by kinkajou

I am trying to send message from GSM modem. I can submit AT commands the response is OKwithout any ERRORS. But the problem is I can't send message or read message.

我正在尝试从 GSM 调制解调器发送消息。我可以提交 AT 命令,响应正常,没有任何错误。但问题是我无法发送消息或阅读消息。

I have implemented 3 functions:

我已经实现了 3 个功能:

  1. Connect to port
  2. Read SMS
  3. Send SMS
  4. Handles
  1. 连接到端口
  2. 阅读短信
  3. 发简讯
  4. 把手

1. Connect To Port:

1. 连接到端口:

    Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
            If SerialPort1.IsOpen Then
                SerialPort1.Close()
                BtnConnect.Text = "Connect"
            Else
                Try
                    With SerialPort1
                        .PortName = Trim(Mid(ComboBox1.Text, 1, 5))
                        .BaudRate = 9600
                        .Parity = IO.Ports.Parity.None
                        .DataBits = 8
                        .StopBits = Ports.StopBits.One
                        .Handshake = Ports.Handshake.None
                        .RtsEnable = True
                        .DtrEnable = True
                        .Open()
                        .WriteLine("AT+CNMI=1,2,0,0,0" & vbCrLf) 'send whatever data that it receives to serial port  
                    End With
                    BtnConnect.Text = "Disconnect"
                Catch ex As Exception
                    BtnConnect.Text = "Connect"
                    MsgBox(ex.Message)
                End Try
            End If

        End Sub

2. Read SMS

2.阅读短信

      Private Sub btn_read_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_read.Click
            Try
                SerialPort1.WriteLine("AT" & vbCrLf) 'is modem okay?
                Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CMGF=1" & vbCrLf) 'To format SMS as a TEXT message
                Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CPMS=""SM""" & vbCrLf) ' Select SIM storage
                Threading.Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CMGL=""REC UNREAD""" & vbCrLf) 'read unread messages
                Threading.Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CMGL=""ALL""" & vbCrLf) 'print all message
                Threading.Thread.Sleep(1000)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

3. Send SMS

3. 发送短信

 Private Sub btn_send_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_send.Click
        Try
            With SerialPort1
                .WriteLine("AT" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .WriteLine("AT+CMGF=1" & vbCrLf) 'Instruct the GSM / GPRS modem to operate in SMS text mode
                Threading.Thread.Sleep(1000)
                .WriteLine("AT+CMGS=""9802100355""" & vbCr) 'sender ko no. rakhne ho tyo txtnumber ma 
                Threading.Thread.Sleep(1000) 'thapeko
                .WriteLine("This is test message" & vbCrLf & Chr(26)) 'txtmessage automatic huna parchha haina?

            End With


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

4. Handles for Data received in Serial Port

4. 串口接收数据的句柄

Private Sub serialport1_datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    'Pause while all data is read
    System.Threading.Thread.Sleep(300)
    rcvdata = SerialPort1.ReadExisting()
    MsgBox(rcvdata, , "Response From AT")
    rcvdata = ""

End Sub

Where did I miss anything? While sending SMS I get CMS 500 error. With the software from the modem I am able to read and send sms. But I need to implement my own in my software.

我在哪里错过了什么?发送短信时,我收到 CMS 500 错误。使用调制解调器中的软件,我可以读取和发送短信。但是我需要在我的软件中实现我自己的。

回答by Ashok singh

There may several cause of this error. First check your network. Second set Message service center number using AT commands and save this setting. Hope this will help you

此错误可能有多种原因。首先检查您的网络。第二次使用AT命令设置短信服务中心号码并保存此设置。希望能帮到你

回答by Tchelo Barbosa

In your second function, you can try declaring a string variable to receive the data, like this:

在您的第二个函数中,您可以尝试声明一个字符串变量来接收数据,如下所示:

With serialport1
    rcvdata=""
    .Write(All AT commands)
    Threading.Thread.Sleep(1000)
    Msgbox(rcvdata.Tostring)
End With

You can add a handler to datareceived to read all the bytes:

您可以向 datareceived 添加处理程序以读取所有字节:

Dim entrada As String = " "
Dim numeros As Integer = SerialPort1.BytesToRead
For i As Integer = 1 To numeros
    entrada&= Chr(SerialPort1.ReadChar)
Next
chama(entrada)
Private Sub chama(ByVal dados As String)
    rcvdata &= dados
End Sub

回答by Mij

AT+CMGS=""9802100355"

Your Phone number is wrong thats why you getting error 500, you need to enter full phone number including 0at the front.

您的电话号码错误,这就是您收到错误 500 的原因,您需要输入完整的电话号码,包括0在前面。